-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
117 lines (105 loc) · 2.69 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
package restgo
import (
"context"
"io"
"net/http"
"net/url"
)
const (
version = "0.1.2"
defaultUA = "RestGO/" + version
headerContentType = "Content-Type"
headerUserAgent = "User-Agent"
)
type Client struct {
baseURL *url.URL
globalHeader http.Header
beforeHooks []BeforeHookFunc
afterHooks []AfterHookFunc
client *http.Client
}
func New(optFns ...OptionFn) *Client {
var o = &option{}
for _, fn := range optFns {
fn(o)
}
if o.transport == nil {
o.transport = &http.Transport{Proxy: http.ProxyFromEnvironment}
}
return &Client{
baseURL: o.baseURL,
globalHeader: o.globalHeader,
beforeHooks: o.beforeHooks,
afterHooks: o.afterHooks,
client: &http.Client{
Jar: o.jar,
Transport: o.transport,
Timeout: o.timeout,
CheckRedirect: o.checkRedirect,
},
}
}
func (c *Client) Do(ctx context.Context, req IRequest) (IResponse, error) {
// run before hooks
c.runBeforeHooks(req)
var rURL, err = req.MakeURL(CloneURL(c.baseURL))
if err != nil {
return nil, err
}
var body io.Reader
body, err = req.MakeRequestBody()
if err != nil {
return nil, err
}
var request *http.Request
request, err = http.NewRequestWithContext(ctx, req.GetMethod(), rURL, body)
if err != nil {
return nil, err
}
for k, vList := range c.globalHeader {
for _, v := range vList {
request.Header.Set(k, v)
}
}
req.WrapperHTTPRequest(request)
var ua = request.Header.Get(headerUserAgent)
if ua == "" {
request.Header.Set(headerUserAgent, defaultUA)
}
var response *http.Response
// nolint: bodyclose
response, err = c.client.Do(request)
if err != nil {
return nil, err
}
// response body is closed by caller
// actually, it's automatically closed by Data access
var rsp = NewResponse(response)
// run after hooks
c.runAfterHooks(req, rsp)
return rsp, nil
}
func (c *Client) Execute(ctx context.Context, method, resource string, params ...IParam) (IResponse, error) {
var req = NewRequest(method, resource)
req.AddParams(params...)
return c.Do(ctx, req)
}
func (c *Client) Get(ctx context.Context, resource string, params ...IParam) (IResponse, error) {
return c.Execute(ctx, "GET", resource, params...)
}
func (c *Client) Post(ctx context.Context, resource string, params ...IParam) (IResponse, error) {
return c.Execute(ctx, "POST", resource, params...)
}
func (c *Client) Put(ctx context.Context, resource string, params ...IParam) (IResponse, error) {
return c.Execute(ctx, "PUT", resource, params...)
}
func (c *Client) runBeforeHooks(req IRequest) {
for _, hook := range c.beforeHooks {
hook(req)
}
}
func (c *Client) runAfterHooks(req IRequest, rsp IResponse) {
for _, hook := range c.afterHooks {
hook(req, rsp)
}
}