-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrest.go
156 lines (131 loc) · 3.41 KB
/
rest.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type JSON map[string]interface{}
type APICallError struct {
StatusCode int
Headers http.Header
err error
}
func (a APICallError) Error() string {
return a.err.Error()
}
// Request struct contains all data for a simple http
// request
type Request struct {
method string
url string
json interface{}
headers map[string]string
to interface{}
failure interface{}
}
func NewRequest() *Request {
return &Request{
method: http.MethodGet,
url: "",
json: JSON{},
headers: map[string]string{
"Content-Type": "application/json",
},
}
}
// Method sets the method and url parameters of the request.
func (r *Request) Method(method, url string) *Request {
r.method = method
r.url = url
return r
}
// Get configures the request to GET the passed URL.
func (r *Request) Get(url string) *Request {
return r.Method(http.MethodGet, url)
}
// Head configures the request to HEAD (HEAD request...) the passed URL.
func (r *Request) Head(url string) *Request {
return r.Method(http.MethodHead, url)
}
// Post configures the request to POST to the passed URL.
func (r *Request) Post(url string) *Request {
return r.Method(http.MethodPost, url)
}
// Put configures the request to PUT to the passed URL.
func (r *Request) Put(url string) *Request {
return r.Method(http.MethodPut, url)
}
// Patch configures the request to PATCH to the passed URL.
func (r *Request) Patch(url string) *Request {
return r.Method(http.MethodPatch, url)
}
// Delete configures the request to DELETE to the passed URL.
func (r *Request) Delete(url string) *Request {
return r.Method(http.MethodDelete, url)
}
// Options configures the request to OPTIONS to the passed URL.
func (r *Request) Options(url string) *Request {
return r.Method(http.MethodOptions, url)
}
// Data sets the body data to be sent in the request
func (r *Request) Data(body interface{}) *Request {
r.json = body
return r
}
// SetHeader is used to set key value pair in the request header
func (r *Request) SetHeader(key, value string) *Request {
r.headers[key] = value
return r
}
// With records the passed variable (pointer) as a destination for any
// response decoding
func (r *Request) With(decoder interface{}) *Request {
r.to = decoder
return r
}
// WithFail records the passed variable (pointer) as a destination for any
// response decoding when the API request fails (based on the status code)
func (r *Request) WithFail(decoder interface{}) *Request {
r.failure = decoder
return r
}
// Do calls the endpoint and decodes the returned response into the
func (r *Request) Do() error {
b := bytes.NewBuffer(nil)
err := json.NewEncoder(b).Encode(r.json)
if err != nil {
return err
}
req, err := http.NewRequest(r.method, r.url, b)
if err != nil {
return err
}
for key, value := range r.headers {
req.Header.Set(key, value)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
if resp.StatusCode > 299 {
errString := fmt.Errorf("Status code error: %d", resp.StatusCode)
if r.failure != nil {
err := dec.Decode(r.failure)
if err != nil {
errString = fmt.Errorf("%s, decode error: %w", errString, err)
}
}
return &APICallError{
StatusCode: resp.StatusCode,
Headers: resp.Header,
err: errString,
}
}
if r.to == nil {
return nil
}
return dec.Decode(r.to)
}