Skip to content

Commit

Permalink
fix: pass context
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse0Michael committed Dec 14, 2022
1 parent e2d13a4 commit e41c904
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/server/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *Server) feed() http.HandlerFunc {
return
}

feed, err := s.fetcher.Feeds(req)
feed, err := s.fetcher.Feeds(r.Context(), req)
if err != nil {
s.log.WithError(err).Error("failed to get feed")
writeError(w, http.StatusInternalServerError, err)
Expand Down
3 changes: 2 additions & 1 deletion internal/server/feed_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"context"
"errors"
"fmt"
"net/http"
Expand All @@ -19,7 +20,7 @@ type MockFetcher struct {
err error
}

func (m *MockFetcher) Feeds(req service.FetcherRequest) (*service.FeedItems, error) {
func (m *MockFetcher) Feeds(ctx context.Context, req service.FetcherRequest) (*service.FeedItems, error) {
if req != m.expected {
return nil, fmt.Errorf("unexpected req")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"context"
"fmt"
"net/http"

Expand All @@ -11,7 +12,7 @@ import (
)

type Fetcher interface {
Feeds(service.FetcherRequest) (*service.FeedItems, error)
Feeds(ctx context.Context, req service.FetcherRequest) (*service.FeedItems, error)
}

type Config struct {
Expand Down
9 changes: 7 additions & 2 deletions internal/service/fetcher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"context"
"fmt"
"io"
"strconv"
Expand All @@ -20,6 +21,10 @@ import (
"github.com/tidwall/gjson"
)

type Feeder interface {
Feed(ctx context.Context, id string) ([]FeedItem, error)
}

type FetcherRequest struct {
TwitterID string `query:"twitterID"`
InstagramID string `query:"instagramID"`
Expand Down Expand Up @@ -53,8 +58,8 @@ func NewFetcher(log *logrus.Entry, cfg Config, twitterClient *twitter.Client, in
}
}

// GetFeed - Get feed
func (f *Fetcher) Feeds(req FetcherRequest) (*FeedItems, error) {
// Feeds retrieves the feed items based on the request parameters
func (f *Fetcher) Feeds(ctx context.Context, req FetcherRequest) (*FeedItems, error) {
items := []FeedItem{}
var wg sync.WaitGroup

Expand Down

0 comments on commit e41c904

Please sign in to comment.