Skip to content

Commit

Permalink
add top stories endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Oct 24, 2022
1 parent be75384 commit 6fbcd89
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions news/handler/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Headlines struct {
Data map[string][]*Article `json:"data"`
}

type TopStories struct {
Data []*Article `json:"data"`
}

var (
apiURL = "https://api.thenewsapi.com"
)
Expand Down Expand Up @@ -150,19 +154,13 @@ func (n *News) TopStories(ctx context.Context, req *pb.TopStoriesRequest, rsp *p
vals.Set("language", language)

uri := fmt.Sprintf("%s%s?%s", apiURL, path, vals.Encode())
var resp *Headlines
var resp *TopStories
if err := api.Get(uri, &resp); err != nil {
return errors.InternalServerError("news.top", err.Error())
}

for _, articles := range resp.Data {
for _, v := range articles {
rsp.Articles = append(rsp.Articles, toProto(v))

for _, a := range v.Similar {
rsp.Articles = append(rsp.Articles, toProto(a))
}
}
rsp.Articles = append(rsp.Articles, toProto(articles))
}

return nil
Expand Down

0 comments on commit 6fbcd89

Please sign in to comment.