package apiclient import ( "encoding/json" "fmt" "net/http" "git.jamestombleson.com/jtom38/newsbot-api/domain" ) const ( ArticlesBaseRoute = "api/v1/articles" ) type Articles interface { List(jwt string, page int) (domain.ArticleResponse, error) } type articlesClient struct { serverAddress string client http.Client } func newArticleService(serverAddress string) articlesClient { return articlesClient{ serverAddress: serverAddress, client: http.Client{}, } } func (c articlesClient) List(jwt string, page int) (domain.ArticleResponse, error) { endpoint := fmt.Sprintf("%s/%s?page=%U", c.serverAddress, ArticlesBaseRoute, page) req, err := http.NewRequest(http.MethodGet, endpoint, nil) if err != nil { return domain.ArticleResponse{}, err } //req.Header.Set(HeaderContentType, ApplicationJson) req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", jwt)) resp, err := c.client.Do(req) if err != nil { return domain.ArticleResponse{}, err } defer resp.Body.Close() var bind domain.ArticleResponse decoder := json.NewDecoder(resp.Body) err = decoder.Decode(&bind) if err != nil { return domain.ArticleResponse{}, err } return bind, nil }