-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
methods.go
88 lines (69 loc) · 1.63 KB
/
methods.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
package rek
import (
"net/http"
"net/url"
)
// GET request
func Get(url string, opts ...Option) (*Response, error) {
return do(http.MethodGet, url, opts...)
}
// POST request
func Post(url string, opts ...Option) (*Response, error) {
return do(http.MethodPost, url, opts...)
}
// PUT request
func Put(url string, opts ...Option) (*Response, error) {
return do(http.MethodPut, url, opts...)
}
// DELETE request
func Delete(url string, opts ...Option) (*Response, error) {
return do(http.MethodDelete, url, opts...)
}
// PATCH request
func Patch(url string, opts ...Option) (*Response, error) {
return do(http.MethodPatch, url, opts...)
}
// HEAD request
func Head(url string, opts ...Option) (*Response, error) {
options, err := buildOptions(opts...)
if err != nil {
return nil, err
}
cl := buildClient(options)
res, err := cl.Head(url)
if err != nil {
return nil, err
}
return buildResponse(res)
}
// Make a request with an arbitrary HTTP method, i.e. not GET, POST, PUT, DELETE, etc.
func Do(method, url string, opts ...Option) (*Response, error) {
return do(method, url, opts...)
}
func do(method, endpoint string, opts ...Option) (*Response, error) {
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
options, err := buildOptions(opts...)
if err != nil {
return nil, err
}
cl := buildClient(options)
req, err := buildRequest(method, u.String(), options)
if err != nil {
return nil, err
}
res, err := cl.Do(req)
if err != nil {
return nil, err
}
resp, err := buildResponse(res)
if err != nil {
return nil, err
}
if options.callback != nil {
options.callback(resp)
}
return resp, nil
}