-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_builder.go
73 lines (53 loc) · 1.15 KB
/
request_builder.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
package http
import (
"net/http"
)
type RequestBuilder struct {
client *Client
method string
_URL string
header http.Header
body interface{}
}
func (r *RequestBuilder) AddHeader(key, value string) *RequestBuilder {
r.header.Add(key, value)
return r
}
func (r *RequestBuilder) SetHeader(key, value string) *RequestBuilder {
r.header.Set(key, value)
return r
}
func (r *RequestBuilder) Body(body interface{}) *RequestBuilder {
r.body = body
return r
}
func (r *RequestBuilder) Build() (req *Request, err error) {
req, err = NewRequest(r.method, r._URL, r.body)
if err != nil {
return
}
req.Request.Header = r.header
return
}
func (r *RequestBuilder) Send() (res *http.Response, err error) {
req, err := r.Build()
if err != nil {
return
}
res, err = r.client.Do(req)
return
}
func NewRequestBuilder(client *Client, method, URL string) (builder *RequestBuilder) {
builder = &RequestBuilder{}
builder.client = client
builder.method = method
if client.BaseURL != "" {
URL = client.BaseURL + URL
}
builder._URL = URL
builder.header = make(http.Header)
for k, v := range client.Headers {
builder.header.Set(k, v)
}
return
}