-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
226 lines (202 loc) · 4.5 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
package jsonhttpc
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"strings"
)
// Client is a JSON specialized HTTP client.
type Client struct {
u *url.URL
c *http.Client
h http.Header
lReq RequestLogger
lResp ResponseLogger
}
// New creates a new Client with base URL.
// You can pass nil to baseURL, in which case you should pass a complete URL to
// the pathOrURL argument of Do() function.
func New(baseURL *url.URL) *Client {
return &Client{
u: baseURL,
}
}
// Clone create a clone of Client.
func (c *Client) Clone() *Client {
nc := *c
nc.h = nil
if len(c.h) > 0 {
nc.h = c.h.Clone()
}
return &nc
}
// WithBaseURL overrides base URL to use.
func (c *Client) WithBaseURL(u *url.URL) *Client {
c.u = u
return c
}
// WithClient overrides *http.Client to use.
func (c *Client) WithClient(hc *http.Client) *Client {
c.c = hc
return c
}
// WithHeader overrides http.Header to use.
func (c *Client) WithHeader(h http.Header) *Client {
c.h = h
return c
}
// Do makes a HTTP request to the server with JSON body, and parse a JSON in
// response body.
//
// `ctx` can be nil, in that case it use `context.Background()` instead.
//
// This returns an error when `pathOrURL` starts with "jsonhttpc.Parse error: "
// to treat `Path()`'s failure as error.
func (c *Client) Do(ctx context.Context, method, pathOrURL string, body, receiver interface{}) error {
// prepare the request.
req, err := c.newRawRequest(ctx, method, pathOrURL, body)
if err != nil {
return err
}
// make messages round trip.
c.logReq(ctx, req)
resp, err := c.client().Do(req)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
c.logResp(ctx, resp)
// verify status code.
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return c.toError(resp)
}
if resp.ContentLength == 0 {
if resp.StatusCode == 204 {
return nil
}
return ErrEmptyResponse
}
// parse the response.
if receiver == nil {
return ErrReceiverAbsence
}
err = c.isJSONResponse(resp)
if err != nil {
return err
}
err = json.NewDecoder(resp.Body).Decode(receiver)
if err != nil {
return err
}
return nil
}
func (c *Client) newRawRequest(ctx context.Context, method, pathOrURL string, body any) (*http.Request, error) {
u, err := c.parseURL(pathOrURL)
if err != nil {
return nil, err
}
r, err := c.bodyReader(body)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), r)
if err != nil {
return nil, err
}
c.setupHeader(req, body)
return req, nil
}
func (c *Client) parseURL(pathOrURL string) (*url.URL, error) {
if strings.HasPrefix(pathOrURL, "jsonhttpc.Parse error: ") {
return nil, errors.New(pathOrURL)
}
if c.u != nil {
return c.u.Parse(pathOrURL)
}
return url.Parse(pathOrURL)
}
func (c *Client) bodyReader(body interface{}) (io.Reader, error) {
if body == nil {
return nil, nil
}
bb := &bytes.Buffer{}
err := json.NewEncoder(bb).Encode(body)
if err != nil {
return nil, err
}
return bb, nil
}
func (c *Client) setupHeader(req *http.Request, body interface{}) {
if len(c.h) > 0 {
req.Header = c.h.Clone()
}
if body != nil {
req.Header.Set("Content-Type", c.getContentType(body))
}
}
func (c *Client) client() *http.Client {
if c.c != nil {
return c.c
}
return http.DefaultClient
}
type contentTyper interface {
ContentType() string
}
func (c *Client) getContentType(v interface{}) string {
if w, ok := v.(contentTyper); ok {
return w.ContentType()
}
return "application/json"
}
func (c *Client) isJSONResponse(r *http.Response) error {
ct := r.Header.Get("Content-Type")
mt, _, err := mime.ParseMediaType(ct)
if err != nil {
return newSystemError(r, fmt.Errorf("invalid content type: %w", err))
}
if !c.isApplicationJSON(mt) {
return newSystemError(r, fmt.Errorf("not JSON response: %s", ct))
}
return nil
}
func (c *Client) isApplicationJSON(ct string) bool {
if ct == "application/json" {
return true
}
if strings.HasPrefix(ct, "application/") && strings.HasSuffix(ct, "+json") {
return true
}
return false
}
func (c *Client) toError(r *http.Response) error {
err := c.isJSONResponse(r)
if err != nil {
return err
}
er, err := parseError(r)
if err != nil {
return err
}
return er
}
func (c *Client) logReq(ctx context.Context, req *http.Request) {
if c.lReq == nil {
return
}
c.lReq.LogRequest(ctx, req)
}
func (c *Client) logResp(ctx context.Context, resp *http.Response) {
if c.lResp == nil {
return
}
c.lResp.LogResponse(ctx, resp)
}