Total Lines | 52 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package devto |
||
2 | |||
3 | import ( |
||
4 | "context" |
||
5 | "errors" |
||
6 | "net/http" |
||
7 | ) |
||
8 | |||
9 | //Configuration constants |
||
10 | const ( |
||
11 | BaseURL string = "https://dev.to/api" |
||
12 | APIVersion string = "0.5.1" |
||
13 | APIKeyHeader string = "api-key" |
||
14 | ) |
||
15 | |||
16 | //devto client errors |
||
17 | var ( |
||
18 | ErrMissingConfig = errors.New("missing configuration") |
||
19 | ) |
||
20 | |||
21 | type httpClient interface { |
||
22 | Do(req *http.Request) (res *http.Response, err error) |
||
23 | } |
||
24 | |||
25 | //Client is the main data structure for performing actions |
||
26 | //against dev.to API |
||
27 | type Client struct { |
||
28 | Context context.Context |
||
29 | HTTPClient httpClient |
||
30 | Config *Config |
||
31 | } |
||
32 | |||
33 | //NewClient takes a context, a configuration pointer and optionally a |
||
34 | //base http client (bc) to build an Client instance. |
||
35 | func NewClient(ctx context.Context, conf *Config, bc httpClient) (dev *Client, err error) { |
||
36 | if bc == nil { |
||
37 | bc = http.DefaultClient |
||
38 | } |
||
39 | |||
40 | if ctx == nil { |
||
41 | ctx = context.Background() |
||
42 | } |
||
43 | |||
44 | if conf == nil { |
||
45 | return nil, ErrMissingConfig |
||
46 | } |
||
47 | |||
48 | return &Client{ |
||
49 | Context: ctx, |
||
50 | HTTPClient: bc, |
||
51 | Config: conf, |
||
52 | }, nil |
||
53 | } |
||
54 |