-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
157 lines (141 loc) · 4 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
package requests
import (
"context"
"net/http"
)
type Client struct {
Options Options
RawClient *http.Client
hooks []Hook
}
func NewClient(options ...Option) *Client {
config := &config{}
for _, option := range options {
option(config)
}
if config.timeout == nil {
config.timeout = &defaultRequestTimeout
}
if config.transport == nil {
config.transport = defaultTransport
}
if config.Options.DefaultHeaders == nil {
config.Options.DefaultHeaders = defaultRequestHeaders
}
return &Client{
Options: config.Options,
RawClient: &http.Client{
Timeout: *config.timeout,
Transport: config.transport,
},
}
}
func (cli *Client) Use(hook Hook) {
cli.hooks = append(cli.hooks, hook)
}
func (cli *Client) prepareRequest(ctx context.Context, req *http.Request) error {
for _, globalHook := range globalHooks {
if err := globalHook.PrepareRequest(ctx, req); err != nil {
if err := globalHook.OnRequestError(ctx, req, err); err != nil {
return err
}
}
}
for _, hook := range cli.hooks {
if err := hook.PrepareRequest(ctx, req); err != nil {
if err := hook.OnRequestError(ctx, req, err); err != nil {
return err
}
}
}
for _, globalHook := range lastGlobalHooks {
if err := globalHook.PrepareRequest(ctx, req); err != nil {
if err := globalHook.OnRequestError(ctx, req, err); err != nil {
return err
}
}
}
return nil
}
func (cli *Client) processResponse(ctx context.Context, req *http.Request, resp *http.Response) error {
for i := len(lastGlobalHooks) - 1; i >= 0; i-- {
if err := lastGlobalHooks[i].ProcessResponse(ctx, req, resp); err != nil {
if err := lastGlobalHooks[i].OnResponseError(ctx, req, resp, err); err != nil {
return err
}
}
}
for i := len(cli.hooks) - 1; i >= 0; i-- {
if err := cli.hooks[i].ProcessResponse(ctx, req, resp); err != nil {
if err := cli.hooks[i].OnResponseError(ctx, req, resp, err); err != nil {
return err
}
}
}
for i := len(globalHooks) - 1; i >= 0; i-- {
if err := globalHooks[i].ProcessResponse(ctx, req, resp); err != nil {
if err := globalHooks[i].OnResponseError(ctx, req, resp, err); err != nil {
return err
}
}
}
return nil
}
func (cli *Client) Do(req *http.Request) (resp *http.Response, err error) {
return cli.DoRequest(req.Context(), req)
}
func (cli *Client) DoRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
if err := cli.prepareRequest(ctx, req); err != nil {
return nil, err
}
resp, err := cli.RawClient.Do(req)
if err != nil {
return nil, err
}
if err := cli.processResponse(ctx, req, resp); err != nil {
return nil, err
}
return resp, nil
}
func (cli *Client) Get(ctx context.Context, path string, opts *HTTPOptions) (*http.Response, error) {
req, err := cli.NewRequest(ctx, http.MethodGet, path, opts)
if err != nil {
return nil, err
}
return cli.DoRequest(ctx, req)
}
func (cli *Client) Post(ctx context.Context, path string, opts *HTTPOptions) (*http.Response, error) {
req, err := cli.NewRequest(ctx, http.MethodPost, path, opts)
if err != nil {
return nil, err
}
return cli.DoRequest(ctx, req)
}
func (cli *Client) Put(ctx context.Context, path string, opts *HTTPOptions) (*http.Response, error) {
req, err := cli.NewRequest(ctx, http.MethodPut, path, opts)
if err != nil {
return nil, err
}
return cli.DoRequest(ctx, req)
}
func (cli *Client) Patch(ctx context.Context, path string, opts *HTTPOptions) (*http.Response, error) {
req, err := cli.NewRequest(ctx, http.MethodPatch, path, opts)
if err != nil {
return nil, err
}
return cli.DoRequest(ctx, req)
}
func (cli *Client) Delete(ctx context.Context, path string, opts *HTTPOptions) (*http.Response, error) {
req, err := cli.NewRequest(ctx, http.MethodDelete, path, opts)
if err != nil {
return nil, err
}
return cli.DoRequest(ctx, req)
}
func (cli *Client) Head(ctx context.Context, path string, opts *HTTPOptions) (*http.Response, error) {
req, err := cli.NewRequest(ctx, http.MethodHead, path, opts)
if err != nil {
return nil, err
}
return cli.DoRequest(ctx, req)
}