-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
171 lines (137 loc) · 3.73 KB
/
client.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package devtogo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// Client makes all the API calls to dev.to.
type Client struct {
apiKey string
baseURL string
http *http.Client
}
// Arguments are used for passing query parameters to the dev.to api.
type Arguments map[string]string
// Defaults returns an empty map of arguments.
func Defaults() Arguments {
return make(map[string]string)
}
func (a Arguments) toQueryParams() url.Values {
res := make(url.Values)
for k, v := range a {
res.Add(k, v)
}
return res
}
// Option allows the client to be configured with different options.
type Option func(*Client)
func withBaseURL(url string) Option {
return func(c *Client) {
c.baseURL = url
}
}
// WithApiKey sets the dev.to api key to use for this client.
// see https://docs.dev.to/api/#section/Authentication for how to set one up.
func WithApiKey(apiKey string) Option {
return func(c *Client) {
c.apiKey = apiKey
}
}
// NewClient creates a dev.to client with the provided options.
func NewClient(opts ...Option) *Client {
res := &Client{
baseURL: "https://dev.to/api",
http: &http.Client{},
}
for _, o := range opts {
o(res)
}
return res
}
func (c *Client) getRequest(method, url string, payload interface{}) (*http.Request, error) {
b := bytes.NewBuffer(nil)
if method == http.MethodPost || method == http.MethodPut {
j, err := json.Marshal(payload)
if err != nil {
return nil, err
}
b = bytes.NewBuffer(j)
}
req, err := http.NewRequest(method, url, b)
if err != nil {
return nil, err
}
if c.apiKey != "" {
req.Header.Add("api-key", c.apiKey)
}
return req, err
}
// get returns an error if the http client cannot perform a HTTP GET for the provided URL.
func (c *Client) get(url string, target interface{}) error {
req, err := c.getRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New("error from dev.to api")
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(b, &target)
}
// save returns an error if the http client cannot save the request to dev.to..
func (c *Client) save(httpMethod string, url string, payload interface{}, target interface{}) error {
req, err := c.getRequest(httpMethod, url, payload)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return errors.New(fmt.Sprintf("error from dev.to api. httpCode: %d, response: %s", resp.StatusCode, b))
}
return json.Unmarshal(b, &target)
}
// put returns an error if the http client cannot perform a HTTP PUT for the provided URL.
func (c *Client) put(url string, payload interface{}, target interface{}) error {
return c.save(http.MethodPut, url, payload, target)
}
// post returns an error if the http client cannot perform a HTTP POST for the provided URL.
func (c *Client) post(url string, payload interface{}, target interface{}) error {
return c.save(http.MethodPost, url, payload, target)
}
// delete returns an error if the http client cannot perform a HTTP DELETE for the provided URL.
func (c *Client) delete(url string, payload interface{}) error {
req, err := c.getRequest(http.MethodDelete, url, nil)
if err != nil {
return err
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New("error from dev.to api")
}
return nil
}