-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresty.go
42 lines (33 loc) · 866 Bytes
/
resty.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
package resty
import (
"github.com/go-resty/resty/v2"
"github.com/lexcao/genapi"
)
var DefaultClient = New(resty.New())
func New(client *resty.Client) *HttpClient {
return &HttpClient{client: client}
}
type HttpClient struct {
client *resty.Client
}
func (c *HttpClient) SetConfig(config genapi.Config) {
c.client.SetBaseURL(config.BaseURL)
c.client.Header = config.Header
}
func (c *HttpClient) Do(req *genapi.Request) (*genapi.Response, error) {
restyReq := c.client.NewRequest().
SetDoNotParseResponse(true).
SetPathParams(req.PathParams).
SetContext(req.Context).
SetBody(req.Body)
for key, value := range req.Header {
for _, v := range value {
restyReq.Header.Add(key, v)
}
}
restyReq.QueryParam = req.Queries
restyReq.URL = req.Path
restyReq.Method = req.Method
resp, err := restyReq.Send()
return resp.RawResponse, err
}