-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
263 lines (209 loc) · 6.02 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
var (
ErrInvalidUrl = errors.New("invalid url")
ErrInvalidToken = errors.New("invalid token")
ErrNotFound = errors.New("not found")
)
type Post struct {
Title string `json:"title,omitempty"`
Slug string `json:"slug,omitempty"`
Body string `json:"body,omitempty"`
PublishedAt string `json:"published_at,omitempty"`
URL string `json:"url,omitempty"`
}
type PostsCreateResquest struct {
Body string `json:"body,omitempty"`
PublishedAt string `json:"published_at,omitempty"`
Title string `json:"title,omitempty"`
}
type PostsBaseResponse struct {
OK bool `json:"ok,omitempty"`
Error string `json:"error,omitempty"`
}
type PostsCreateResponse struct {
PostsBaseResponse
Slug string `json:"slug,omitempty"`
URL string `json:"url,omitempty"`
}
type PostsUpdateResponse struct {
PostsBaseResponse
Slug string `json:"slug,omitempty"`
URL string `json:"url,omitempty"`
}
type PostsDeleteResponse struct {
PostsBaseResponse
}
type PostsListResponse struct {
PostsBaseResponse
PostList []Post `json:"post_list,omitempty"`
}
type PostsGetResponse struct {
PostsBaseResponse
Post
}
type Client struct {
baseUrl *url.URL
token string
httpClient *http.Client
}
func NewClient(baseUrl string, token string) (*Client, error) {
if baseUrl == "" {
return &Client{}, ErrInvalidUrl
}
if token == "" {
return &Client{}, ErrInvalidToken
}
u, err := url.Parse(baseUrl)
if err != nil {
return &Client{}, ErrInvalidUrl
}
httpClient := &http.Client{Timeout: time.Second * 10}
return &Client{
baseUrl: u, token: token, httpClient: httpClient,
}, nil
}
func (c *Client) newRequest(ctx context.Context, method, url string, body io.Reader) (*http.Response, error) {
if !strings.HasSuffix(url, "/") {
url += "/"
}
req, err := http.NewRequestWithContext(ctx, method, fmt.Sprintf("%s/%s", c.baseUrl, url), body)
if err != nil {
return &http.Response{}, fmt.Errorf("error creating request: %s", err)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.token))
response, err := c.httpClient.Do(req)
if err != nil {
return response, fmt.Errorf("error making request: %s", err)
}
return response, nil
}
func (c *Client) ListAll(ctx context.Context) (PostsListResponse, error) {
var response PostsListResponse
resp, err := c.newRequest(ctx, "GET", "posts", nil)
if err != nil {
return response, fmt.Errorf("error listing posts: %s", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(io.Reader(resp.Body))
if err != nil {
return response, fmt.Errorf("error reading response body: %s", err)
}
err = json.Unmarshal(body, &response)
if err != nil {
return response, fmt.Errorf("error unmarshaling json: %s", err)
}
if !response.OK {
return response, fmt.Errorf("error: %s", response.Error)
}
return response, nil
}
func (c *Client) Get(ctx context.Context, slug string) (PostsGetResponse, error) {
var response PostsGetResponse
resp, err := c.newRequest(ctx, "GET", fmt.Sprintf("posts/%s", slug), nil)
if err != nil {
return response, fmt.Errorf("error fetching post: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
return response, fmt.Errorf("error: %w", ErrNotFound)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return response, fmt.Errorf("error reading response body: %s", err)
}
err = json.Unmarshal(body, &response)
if err != nil {
return response, fmt.Errorf("error unmarshaling json: %s", err)
}
if !response.OK {
return response, fmt.Errorf("error: %s", response.Error)
}
return response, nil
}
func (c *Client) Delete(ctx context.Context, slug string) (PostsDeleteResponse, error) {
var response PostsDeleteResponse
resp, err := c.newRequest(ctx, "DELETE", fmt.Sprintf("posts/%s", slug), nil)
if err != nil {
return response, fmt.Errorf("error deleting post: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
return response, ErrNotFound
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return response, fmt.Errorf("error reading response body: %s", err)
}
err = json.Unmarshal(body, &response)
if err != nil {
return response, fmt.Errorf("error unmarshaling json: %s", err)
}
if !response.OK {
return response, fmt.Errorf("error: %s", response.Error)
}
return response, nil
}
func (c *Client) Update(ctx context.Context, slug string, post Post) (PostsUpdateResponse, error) {
var response PostsUpdateResponse
body, err := json.Marshal(post)
if err != nil {
return response, fmt.Errorf("error updating post: %s", err)
}
resp, err := c.newRequest(ctx, "PATCH", fmt.Sprintf("posts/%s", slug), bytes.NewBuffer(body))
if err != nil {
return response, fmt.Errorf("error updating post: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
return response, ErrNotFound
}
body, err = io.ReadAll(resp.Body)
if err != nil {
return response, fmt.Errorf("error reading response body: %s", err)
}
err = json.Unmarshal(body, &response)
if err != nil {
return response, fmt.Errorf("error unmarshaling json: %s", err)
}
if !response.OK {
return response, fmt.Errorf("error: %s", response.Error)
}
return response, nil
}
func (c *Client) Create(ctx context.Context, post Post) (PostsCreateResponse, error) {
var response PostsCreateResponse
body, err := json.Marshal(post)
if err != nil {
return response, fmt.Errorf("error marshaling post: %s", err)
}
resp, err := c.newRequest(ctx, "POST", "posts", bytes.NewBuffer(body))
if err != nil {
return response, fmt.Errorf("error creating post: %s", err)
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
if err != nil {
return response, fmt.Errorf("error reading response body: %s", err)
}
err = json.Unmarshal(body, &response)
if err != nil {
return response, fmt.Errorf("error unmarshaling json: %s", err)
}
if !response.OK {
return response, fmt.Errorf("error: %s", response.Error)
}
return response, nil
}