-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_request.go
227 lines (202 loc) · 6.35 KB
/
http_request.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
package golden
import (
"context"
"github.com/go-resty/resty/v2"
"github.com/opentracing/opentracing-go"
)
type HttpRequest struct {
impl *resty.Request
}
func newHttpRequestFromContextAndSpan(ctx context.Context, span opentracing.Span, request *resty.Request) *HttpRequest {
ctxWithSpan := opentracing.ContextWithSpan(ctx, span)
request.SetContext(ctxWithSpan)
return &HttpRequest{
impl: request,
}
}
func NewHttpRequestWithHttpContext(ctx context.Context, c *HttpContext) *HttpRequest {
return newHttpRequestFromContextAndSpan(ctx, c.GetSpan(), c.client.R())
}
func NewHttpRequest(ctx context.Context) *HttpRequest {
return newHttpRequestFromContextAndSpan(ctx, opentracing.StartSpan("clientSpan"), resty.New().R())
}
func (r *HttpRequest) SetImpl(impl *resty.Request) {
r.impl = impl
}
func (r *HttpRequest) GetHeader(header string) string {
return r.impl.Header.Get(header)
}
// SetHeader("Content-Type", "application/json").
// SetHeader("Accept", "application/json")
func (r *HttpRequest) SetHeader(header, value string) *HttpRequest {
r.impl.SetHeader(header, value)
return r
}
// SetHeaders(map[string]string{
// "Content-Type": "application/json",
// "Accept": "application/json",
// })
func (r *HttpRequest) SetHeaders(headers map[string]string) *HttpRequest {
r.impl.SetHeaders(headers)
return r
}
// SetQueryParam("search", "kitchen papers").
// SetQueryParam("size", "large")
func (r *HttpRequest) SetQueryParam(param, value string) *HttpRequest {
r.impl.SetQueryParam(param, value)
return r
}
// SetQueryParams(map[string]string{
// "search": "kitchen papers",
// "size": "large",
// })
func (r *HttpRequest) SetQueryParams(params map[string]string) *HttpRequest {
r.impl.SetQueryParams(params)
return r
}
// SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more")
func (r *HttpRequest) SetQueryString(query string) *HttpRequest {
r.impl.SetQueryString(query)
return r
}
// SetFormData(map[string]string{
// "access_token": "BC594900-518B-4F7E-AC75-BD37F019E08F",
// "user_id": "3455454545",
// })
func (r *HttpRequest) SetFormData(data map[string]string) *HttpRequest {
r.impl.SetFormData(data)
return r
}
// SetBody method sets the request body for the request. It supports various realtime needs as easy.
// We can say its quite handy or powerful. Supported request body data types is `string`,
// `[]byte`, `struct`, `map`, `slice` and `io.Reader`. Body value can be pointer or non-pointer.
func (r *HttpRequest) SetBody(body interface{}) *HttpRequest {
r.impl.SetBody(body)
return r
}
// SetResult method is to register the response `Result` object for automatic unmarshalling in the RESTful mode
// if response status code is between 200 and 299 and content type either JSON or XML.
//
// Note: Result object can be pointer or non-pointer.
// request.SetResult(&AuthToken{})
// // OR
// request.SetResult(AuthToken{})
//
// Accessing a result value
// response.Result().(*AuthToken)
//
func (r *HttpRequest) SetResult(res interface{}) *HttpRequest {
r.impl.SetResult(res)
return r
}
// SetError method is to register the request `Error` object for automatic unmarshalling in the RESTful mode
// if response status code is greater than 399 and content type either JSON or XML.
//
// Note: Error object can be pointer or non-pointer.
// request.SetError(&AuthError{})
// // OR
// request.SetError(AuthError{})
//
// Accessing a error value
// response.Error().(*AuthError)
//
func (r *HttpRequest) SetError(err interface{}) *HttpRequest {
r.impl.SetError(err)
return r
}
// SetBasicAuth method sets the basic authentication header in the current HTTP request.
// For Header example:
// Authorization: Basic <base64-encoded-value>
//
// To set the header for username "go-resty" and password "welcome"
// request.SetBasicAuth("go-resty", "welcome")
//
// This method overrides the credentials set by method `resty.SetBasicAuth`.
//
func (r *HttpRequest) SetBasicAuth(username, password string) *HttpRequest {
r.impl.SetBasicAuth(username, password)
return r
}
// SetAuthToken method sets bearer auth token header in the current HTTP request. Header example:
// Authorization: Bearer <auth-token-value-comes-here>
//
// Example: To set auth token BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F
//
// request.SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F")
//
// This method overrides the Auth token set by method `resty.SetAuthToken`.
//
func (r *HttpRequest) SetAuthToken(token string) *HttpRequest {
r.impl.SetAuthToken(token)
return r
}
// Get method does GET HTTP request. It's defined in section 4.3.1 of RFC7231.
func (r *HttpRequest) Get(url string) (*HttpResponse, error) {
resp, err := r.impl.Get(url)
if err != nil {
return nil, err
}
return &HttpResponse{
impl: resp,
}, nil
}
// Head method does HEAD HTTP request. It's defined in section 4.3.2 of RFC7231.
func (r *HttpRequest) Head(url string) (*HttpResponse, error) {
resp, err := r.impl.Head(url)
if err != nil {
return nil, err
}
return &HttpResponse{
impl: resp,
}, nil
}
// Post method does POST HTTP request. It's defined in section 4.3.3 of RFC7231.
func (r *HttpRequest) Post(url string) (*HttpResponse, error) {
resp, err := r.impl.Post(url)
if err != nil {
return nil, err
}
return &HttpResponse{
impl: resp,
}, nil
}
// Put method does PUT HTTP request. It's defined in section 4.3.4 of RFC7231.
func (r *HttpRequest) Put(url string) (*HttpResponse, error) {
resp, err := r.impl.Put(url)
if err != nil {
return nil, err
}
return &HttpResponse{
impl: resp,
}, nil
}
// Delete method does DELETE HTTP request. It's defined in section 4.3.5 of RFC7231.
func (r *HttpRequest) Delete(url string) (*HttpResponse, error) {
resp, err := r.impl.Delete(url)
if err != nil {
return nil, err
}
return &HttpResponse{
impl: resp,
}, nil
}
// Options method does OPTIONS HTTP request. It's defined in section 4.3.7 of RFC7231.
func (r *HttpRequest) Options(url string) (*HttpResponse, error) {
resp, err := r.impl.Options(url)
if err != nil {
return nil, err
}
return &HttpResponse{
impl: resp,
}, nil
}
// Patch method does PATCH HTTP request. It's defined in section 2 of RFC5789.
func (r *HttpRequest) Patch(url string) (*HttpResponse, error) {
resp, err := r.impl.Patch(url)
if err != nil {
return nil, err
}
return &HttpResponse{
impl: resp,
}, nil
}