/
cbd.go
79 lines (68 loc) · 1.61 KB
/
cbd.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
package upapi
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"github.com/google/go-querystring/query"
)
type RequestBuilder interface {
BuildRequest(ctx context.Context, method string, endpoint string, args any, data any) (*http.Request, error)
}
type ResponseDecoder interface {
DecodeResponse(rs *http.Response, data any) error
}
type Doer interface {
Do(rq *http.Request) (*http.Response, error)
}
// CBD is http Client, request Build and response Decoder bundle.
type CBD interface {
Doer
RequestBuilder
ResponseDecoder
}
type requestBuilderImpl struct{}
func (r *requestBuilderImpl) BuildRequest(ctx context.Context, method string, endpoint string, args any, data any) (*http.Request, error) {
var body io.ReadCloser
if data != nil {
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(data)
if err != nil {
return nil, err
}
body = io.NopCloser(buf)
}
rq, err := http.NewRequestWithContext(ctx, method, endpoint, body)
if err != nil {
return nil, err
}
if args != nil {
qs, err := query.Values(args)
if err != nil {
return nil, err
}
rq.URL.RawQuery = qs.Encode()
}
rq.Header.Set("Content-Type", "application/json")
return rq, nil
}
type responseDecoderImpl struct{}
func (r *responseDecoderImpl) DecodeResponse(rs *http.Response, data any) error {
if data == nil {
return nil
}
dec := json.NewDecoder(rs.Body)
return dec.Decode(data)
}
func applyOptions(cbd CBD, opts ...Option) (_ CBD, err error) {
for i := range opts {
cbd, err = opts[i](cbd)
if err != nil {
return nil, err
}
}
return cbd, nil
}