-
Notifications
You must be signed in to change notification settings - Fork 1
/
sources.go
69 lines (57 loc) · 1.58 KB
/
sources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package newsapi
import (
"context"
)
// SourceParameters are the parameters which can be used in the source request to newsapi
type SourceParameters struct {
Category string `url:"category,omitempty"`
Language string `url:"language,omitempty"`
Country string `url:"country,omitempty"`
}
// Source is a source from newsapi it contains all the information provided by the api
type Source struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
URL string `json:"url"`
Category string `json:"category"`
Language string `json:"language"`
Country string `json:"country"`
UrlsToLogos struct {
Small string `json:"small"`
Medium string `json:"medium"`
Large string `json:"large"`
} `json:"urlsToLogos"`
}
// SourceResponse is the response from the source request
type SourceResponse struct {
Status string `json:"status"`
Sources []Source `json:"sources"`
}
// GetSources returns the sources from newsapi see http://newsapi.org/docs for more information on the parameters
func (c *Client) GetSources(ctx context.Context, params *SourceParameters) (*SourceResponse, error) {
u := sourcesEndpoint
if params != nil {
var err error
u, err = setOptions(u, params)
if err != nil {
return nil, err
}
}
req, err := c.newGetRequest(u)
if err != nil {
return nil, err
}
var response = struct {
*Error
*SourceResponse
}{}
_, err = c.do(ctx, req, &response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.SourceResponse, nil
}