/
client.go
228 lines (190 loc) · 6.41 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
package fetch
import (
"context"
"io"
"net"
"net/http"
"time"
"golang.org/x/net/context/ctxhttp"
)
// DefaultTimeout defined timeout default for any request
const DefaultTimeout = time.Duration(30 * time.Second)
// Options default for any request in client
type Options struct {
Header http.Header
Timeout time.Duration
Host string
Transport *http.Transport
}
// DefaultOptions returns options with timeout defined
func DefaultOptions() *Options {
return &Options{
Timeout: DefaultTimeout,
Header: http.Header{},
}
}
// NewDefault get fetcher with netTransport and timeout defined
func NewDefault() *Fetch {
return New(DefaultOptions())
}
// getTransport make transport from options definitions
func getTransport(opt *Options) {
if opt.Timeout.Nanoseconds() == 0 {
opt.Timeout = DefaultTimeout
}
opt.Transport = &http.Transport{
DialContext: (&net.Dialer{
Timeout: opt.Timeout,
}).DialContext,
TLSHandshakeTimeout: opt.Timeout,
}
}
// New get new fetcher and you need to specify the netTransport.
func New(opt *Options) *Fetch {
if opt == nil {
opt = DefaultOptions()
}
if opt.Transport == nil {
getTransport(opt)
}
return &Fetch{
Client: &http.Client{
Timeout: opt.Timeout,
Transport: opt.Transport,
},
Option: opt,
}
}
// Fetch use http default but defined with a timeout.
type Fetch struct {
*http.Client
Option *Options
}
// IsJSON add Content-Type as JSON in header.
func (f *Fetch) IsJSON() *Fetch {
if f.Option.Header == nil {
f.Option.Header = http.Header{}
}
f.Option.Header.Set("Content-Type", "application/json")
return f
}
// makeResponse format response from generic request
func (f Fetch) makeResponse(resp *http.Response, err error) (*Response, error) {
if resp == nil {
resp = &http.Response{
StatusCode: http.StatusGatewayTimeout,
Status: http.StatusText(http.StatusGatewayTimeout),
}
}
return &Response{Response: resp}, err
}
// Do execute any kind of request
func (f *Fetch) Do(req *http.Request) (*Response, error) {
if f.Option.Header != nil {
req.Header = f.Option.Header
}
return f.makeResponse(f.Client.Do(req))
}
// Get do request with HTTP using HTTP Verb GET
func (f *Fetch) Get(url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodGet, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request GET: %s", err)
}
return f.Do(req)
}
// Post do request with HTTP using HTTP Verb POST
func (f *Fetch) Post(url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodPost, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request POST: %s", err)
}
return f.Do(req)
}
// Put do request with HTTP using HTTP Verb PUT
func (f *Fetch) Put(url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodPut, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request PUT: %s", err)
}
return f.Do(req)
}
// Delete do request with HTTP using HTTP Verb DELETE
func (f *Fetch) Delete(url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodDelete, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request DELETE: %s", err)
}
return f.Do(req)
}
// Patch do request with HTTP using HTTP Verb PATCH
func (f *Fetch) Patch(url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodPatch, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request PATCH: %s", err)
}
return f.Do(req)
}
// Options do request with HTTP using HTTP Verb OPTIONS
func (f *Fetch) Options(url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodOptions, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request OPTIONS: %s", err)
}
return f.Do(req)
}
// DoWithContext execute any kind of request passing context
func (f *Fetch) DoWithContext(ctx context.Context, req *http.Request) (*Response, error) {
if f.Option.Header != nil {
req.Header = f.Option.Header
}
return f.makeResponse(ctxhttp.Do(ctx, f.Client, req))
}
// GetWithContext execute DoWithContext but define request to method GET
func (f *Fetch) GetWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodGet, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request GET: %s", err)
}
return f.DoWithContext(ctx, req)
}
// PostWithContext execute DoWithContext but define request to method POST
func (f *Fetch) PostWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodPost, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request POST: %s", err)
}
return f.DoWithContext(ctx, req)
}
// PutWithContext execute DoWithContext but define request to method PUT
func (f *Fetch) PutWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodPut, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request PUT: %s", err)
}
return f.DoWithContext(ctx, req)
}
// DeleteWithContext execute DoWithContext but define request to method DELETE
func (f *Fetch) DeleteWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodDelete, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request DELETE: %s", err)
}
return f.DoWithContext(ctx, req)
}
// PatchWithContext execute DoWithContext but define request to method PATCH
func (f *Fetch) PatchWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodPatch, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request PATCH: %s", err)
}
return f.DoWithContext(ctx, req)
}
// OptionsWithContext execute DoWithContext but define request to method OPTIONS
func (f *Fetch) OptionsWithContext(ctx context.Context, url string, reader io.Reader) (*Response, error) {
req, err := http.NewRequest(http.MethodOptions, url, reader)
if err != nil {
return newErrorResponse(http.StatusNoContent, "couldn't request OPTIONS: %s", err)
}
return f.DoWithContext(ctx, req)
}