Update code

This commit is contained in:
Ayooluwa Isaiah
2020-11-18 11:02:11 +01:00
parent 1909256285
commit 4ac2d8c782
6 changed files with 214 additions and 153 deletions

71
news/news.go Normal file
View File

@@ -0,0 +1,71 @@
package news
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
type Article struct {
Source struct {
ID interface{} `json:"id"`
Name string `json:"name"`
} `json:"source"`
Author string `json:"author"`
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
URLToImage string `json:"urlToImage"`
PublishedAt time.Time `json:"publishedAt"`
Content string `json:"content"`
}
func (a *Article) FormatPublishedDate() string {
year, month, day := a.PublishedAt.Date()
return fmt.Sprintf("%v %d, %d", month, day, year)
}
type Results struct {
Status string `json:"status"`
TotalResults int `json:"totalResults"`
Articles []Article `json:"articles"`
}
type Client struct {
http *http.Client
key string
PageSize int
}
func (c *Client) FetchEverything(query, page string) (*Results, error) {
endpoint := fmt.Sprintf("https://newsapi.org/v2/everything?q=%s&pageSize=%d&page=%s&apiKey=%s&sortBy=publishedAt&language=en", url.QueryEscape(query), c.PageSize, page, c.key)
resp, err := c.http.Get(endpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(string(body))
}
res := &Results{}
return res, json.Unmarshal(body, res)
}
func NewClient(httpClient *http.Client, key string, pageSize int) *Client {
if pageSize > 100 {
pageSize = 100
}
return &Client{httpClient, key, pageSize}
}