-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
195 lines (165 loc) · 4.21 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
package freee
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"strings"
)
type opt struct {
HTTPClient *http.Client
beforeRequest []func(*http.Request) (*http.Request, error)
afterResponse []func(*http.Response) (*http.Response, error)
}
type OptFunc func(*opt)
func WithHTTPClient(httpClient *http.Client) func(*opt) {
return func(o *opt) {
o.HTTPClient = httpClient
}
}
type Hooks struct {
BeforeRequest func(*http.Request) (*http.Request, error)
AfterResponse func(*http.Response) (*http.Response, error)
}
func WithHooks(hooks Hooks) func(*opt) {
return func(o *opt) {
if hooks.BeforeRequest != nil {
o.beforeRequest = append(o.beforeRequest, hooks.BeforeRequest)
}
if hooks.AfterResponse != nil {
o.afterResponse = append(o.afterResponse, hooks.AfterResponse)
}
}
}
func New(clientID string, clientSecret string, accessToken *AccessToken, opts ...OptFunc) (*Client, error) {
if accessToken == nil {
return nil, errors.New("access token is nil")
}
o := &opt{
HTTPClient: http.DefaultClient,
}
for _, of := range opts {
of(o)
}
c := &Client{
httpClient: o.HTTPClient,
Token: newTokenManager(clientID, clientSecret, accessToken, o.HTTPClient),
beforeRequest: o.beforeRequest,
afterResponse: o.afterResponse,
}
return c, nil
}
type Client struct {
httpClient *http.Client
Token *tokenManager
beforeRequest []func(*http.Request) (*http.Request, error)
afterResponse []func(*http.Response) (*http.Response, error)
}
type response struct {
*http.Response
}
func (r *response) Close() error {
_, err := io.Copy(io.Discard, r.Body)
if err1 := r.Body.Close(); err == nil {
err = err1
}
return err
}
func (r *response) Parse(v any) error {
defer r.Close()
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
return fmt.Errorf("invalid body: %v", err)
}
return nil
}
func (c *Client) do(method string, targetURL string, query url.Values, payload any) (*response, error) {
u, err := url.Parse(targetURL)
if err != nil {
return nil, fmt.Errorf("invalid request url: %v", err)
}
if query != nil {
u.RawQuery = query.Encode()
}
var body io.Reader
if payload != nil {
buf, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("invalid request payload: %v", err)
}
body = bytes.NewReader(buf)
}
accessToken, err := c.Token.GetAccessToken()
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+accessToken.AccessToken)
if payload != nil {
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
}
for _, hook := range c.beforeRequest {
req, err = hook(req)
if err != nil {
return nil, err
}
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
for _, hook := range c.afterResponse {
resp, err = hook(resp)
if err != nil {
return nil, err
}
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return &response{resp}, nil
}
return nil, handleError(&response{resp})
}
func handleError(r *response) error {
defer r.Close()
invalidStatusCode := func() error {
return errors.New("invalid status code: " + r.Status)
}
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return invalidStatusCode()
}
switch mediaType {
case "application/problem+json":
problem := struct {
StatusCode int `json:"status_code"`
Errors []struct {
Type string `json:"type"`
Messages []string `json:"messages"`
} `json:"errors"`
}{}
if err := json.NewDecoder(r.Body).Decode(&problem); err != nil {
return invalidStatusCode()
}
errs := make([]error, 0, len(problem.Errors))
for _, err := range problem.Errors {
errs = append(errs, errors.New(strings.Join(err.Messages, " ")+" ("+err.Type+")"))
}
return errors.Join(errs...)
case "application/json":
errResponse := struct {
Error string `json:"error"`
Message string `json:"message"`
}{}
if err := json.NewDecoder(r.Body).Decode(&errResponse); err != nil {
return invalidStatusCode()
}
return errors.New(errResponse.Message + " (" + errResponse.Error + ")")
}
return invalidStatusCode()
}