-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbytes_client.go
66 lines (51 loc) · 2.14 KB
/
bytes_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
package httpclient
import "github.com/go-resty/resty/v2"
type (
BytesClient interface {
Get(urlPath string, opts ...RequestOption) ([]byte, error)
Post(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error)
Put(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error)
Patch(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error)
Delete(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error)
Execute(method, urlPath string, body interface{}, opts ...RequestOption) ([]byte, error)
}
defaultBytesClient struct {
client Client
}
)
var _ BytesClient = (*defaultBytesClient)(nil)
func NewBytesClient(addr string, opts ...RequestOption) BytesClient {
return NewBytesClientRaw(NewClient(addr, opts...))
}
func NewBytesClientRaw(cli Client) BytesClient {
return &defaultBytesClient{
client: cli,
}
}
func (c *defaultBytesClient) Get(urlPath string, opts ...RequestOption) ([]byte, error) {
return c.convertResponse(c.client.Get(urlPath, opts...))
}
func (c *defaultBytesClient) Post(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) {
return c.convertResponse(c.client.Post(urlPath, body, opts...))
}
func (c *defaultBytesClient) Put(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) {
return c.convertResponse(c.client.Put(urlPath, body, opts...))
}
func (c *defaultBytesClient) Patch(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) {
return c.convertResponse(c.client.Patch(urlPath, body, opts...))
}
func (c *defaultBytesClient) Delete(urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) {
return c.convertResponse(c.client.Delete(urlPath, body, opts...))
}
func (c *defaultBytesClient) Execute(method, urlPath string, body interface{}, opts ...RequestOption) ([]byte, error) {
return c.convertResponse(c.client.Execute(method, urlPath, body, opts...))
}
func (*defaultBytesClient) convertResponse(resp *resty.Response, err error) ([]byte, error) {
if err != nil {
return nil, err
}
if err = NewResponseErrorNotSuccess(resp); err != nil {
return nil, err
}
return resp.Body(), err
}