-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
73 lines (59 loc) · 1.62 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
package hasaki
import (
"context"
"fmt"
"net/http"
"strings"
)
type Client struct {
config *config
}
// NewClient 新建一个客户端
// Create a new client
func NewClient(options ...Option) (*Client, error) {
var conf = new(config)
for _, f := range options {
f(conf)
}
withInitialize()(conf)
var client = &Client{config: conf}
return client, nil
}
func (c *Client) Get(url string, args ...any) *Request {
return c.Request(http.MethodGet, url, args...)
}
func (c *Client) Post(url string, args ...any) *Request {
return c.Request(http.MethodPost, url, args...)
}
func (c *Client) Put(url string, args ...any) *Request {
return c.Request(http.MethodPut, url, args...)
}
func (c *Client) Delete(url string, args ...any) *Request {
return c.Request(http.MethodDelete, url, args...)
}
func (c *Client) Head(url string, args ...any) *Request {
return c.Request(http.MethodHead, url, args...)
}
func (c *Client) Options(url string, args ...any) *Request {
return c.Request(http.MethodOptions, url, args...)
}
func (c *Client) Patch(url string, args ...any) *Request {
return c.Request(http.MethodPatch, url, args...)
}
func (c *Client) Request(method string, url string, args ...any) *Request {
if len(args) > 0 {
url = fmt.Sprintf(url, args...)
}
r := &Request{
ctx: context.Background(),
client: c.config.HTTPClient,
method: strings.ToUpper(method),
url: url,
before: c.config.BeforeFunc,
after: c.config.AfterFunc,
headers: http.Header{},
reuseBodyEnabled: c.config.ReuseBodyEnabled,
}
r.SetEncoder(JsonCodec)
return r
}