-
Notifications
You must be signed in to change notification settings - Fork 0
/
req_resp.go
125 lines (110 loc) · 2.79 KB
/
req_resp.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
118
119
120
121
122
123
124
125
package httpx
import (
"encoding/json"
"encoding/xml"
"github.com/dba3306/stardust/encodingx/base64x"
"github.com/dba3306/stardust/encodingx/jsonx/freejson"
"net/http"
)
type Request struct {
Method string `json:"method"`
URL string `json:"url"`
Header map[string]string `json:"header"`
Base64Body string `json:"body,omitempty"`
Retries int `json:"retries,omitempty"`
TimeoutMS int64 `json:"timeout_ms,omitempty"`
}
type Response struct {
StatusCode int `json:"status_code"`
URL string `json:"url"`
Header map[string]string `json:"header"`
Base64Body string `json:"body,omitempty"`
Ex map[string]*Response `json:"ex,omitempty"`
}
type ResponseWithError struct {
*Response
Err error
}
func NewGetReq(url string, retries int, timeoutMS int64) *Request {
return &Request{
Method: http.MethodGet,
URL: url,
Retries: retries,
TimeoutMS: timeoutMS,
}
}
func NewPostWithHeader(url string, body []byte, header map[string]string, retries int, timeoutMS int64) *Request {
req := &Request{
Method: http.MethodPost,
URL: url,
Header: header,
Retries: retries,
TimeoutMS: timeoutMS,
}
req.SetBody(body)
return req
}
func NewGetReqWithHeader(url string, header map[string]string, retries int) *Request {
return &Request{
Method: http.MethodGet,
URL: url,
Header: header,
Retries: retries,
}
}
func NewGetReqs(urls []string, retries int, timeoutMS int64) []*Request {
nUrl := len(urls)
if nUrl == 0 {
return nil
}
reqs := make([]*Request, 0, nUrl)
for _, url := range urls {
reqs = append(reqs, NewGetReq(url, retries, timeoutMS))
}
return reqs
}
func (req *Request) SetBody(data []byte) {
if len(data) == 0 {
return
}
req.Base64Body = base64x.EncodeString(data)
}
func (req *Request) BodyAsBytes() ([]byte, error) {
if req.Base64Body == "" {
return nil, nil
}
return base64x.DecodeString(req.Base64Body)
}
func (resp *Response) SetBody(data []byte) {
if len(data) == 0 {
return
}
resp.Base64Body = base64x.EncodeString(data)
}
func (resp *Response) AsBytes() ([]byte, error) {
if resp.Base64Body == "" {
return nil, nil
}
return base64x.DecodeString(resp.Base64Body)
}
func (resp *Response) ParseJSON(v interface{}) error {
bodyBytes, err := resp.AsBytes()
if err != nil {
return err
}
return json.Unmarshal(bodyBytes, v)
}
func (resp *Response) ParseXML(v interface{}) error {
bodyBytes, err := resp.AsBytes()
if err != nil {
return err
}
return xml.Unmarshal(bodyBytes, v)
}
func (resp *Response) ParseFreeJSON(valDec freejson.ValueDecoder) (interface{}, error) {
bodyBytes, err := resp.AsBytes()
if err != nil {
return nil, err
}
return freejson.Unmarshal(bodyBytes, valDec)
}