-
Notifications
You must be signed in to change notification settings - Fork 0
/
resty_client.go
179 lines (151 loc) · 5.06 KB
/
resty_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package sdk
import (
"bytes"
"github.com/go-resty/resty"
"io"
"net"
"net/http"
"os"
"time"
)
//var (
// //token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlLZXkiOiJaT0dKNVI0QTZPU1JIWUlOTUlMQSIsImV4cCI6MTY4NDgzNDUxNSwiaWRlbnRpdHkiOnsiSWQiOjYsIk5pY2tOYW1lIjoidXNlcjEiLCJQcm9maWxlSW1hZ2UiOiIiLCJXYWxsZXRBZGRyIjoidXNlcjEiLCJ0ZXJtaW5hbCI6InBjL3NkayJ9LCJvcmlnX2lhdCI6MTY4MjI0MjUyNH0.w0lQ-RGufy5j3yMfbnL94hOvLeeCSccZbSIwd6kXvbI"
// token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlLZXkiOiJaT0dKNVI0QTZPU1JIWUlOTUlMQSIsImV4cCI6MTY4NDgzNDUxNSwiaWRlbnRpdHkiOnsiSWQiOjYsIk5pY2tOYW1lIjoidXNlcjEiLCJQcm9maWxlSW1hZ2UiOiIiLCJXYWxsZXRBZGRyIjoidXNlcjEiLCJ0ZXJtaW5hbCI6InBjL3NkayJ9LCJvcmlnX2lhdCI6MTY4MjI0MjUyNH0.w0lQ-RGufy5j3yMfbnL94hOvLeeCSccZbSIwd6kXvbI"
// userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
// headers = map[string]string{
// "Accept": "application/json",
// "Authorization": token,
// "User-Agent": userAgent,
// }
//)
type RestyClient struct {
Config *Configuration
//logger *golog.Logger
}
func (r *RestyClient) getHeaders() *map[string]string {
apiToken := r.Config.ChainStorageApiToken
httpRequestUserAgent := r.Config.HttpRequestUserAgent
return &map[string]string{
"Accept": "application/json",
"Authorization": apiToken,
"User-Agent": httpRequestUserAgent,
}
}
func (r *RestyClient) RestyGet(url string) (httpStatus int, body []byte, err error) {
resp, err := resty.
SetTimeout(time.Duration(60) * time.Second).
SetHeaders(*r.getHeaders()).
R().Get(url)
if err != nil {
if perr, ok := err.(net.Error); ok && perr.Timeout() {
return http.StatusGatewayTimeout, nil, err
} else {
return http.StatusServiceUnavailable, nil, err
}
}
defer resp.RawResponse.Body.Close()
body = resp.Body()
return resp.StatusCode(), body, nil
}
func (r *RestyClient) RestyPut(url string, data interface{}) (httpStatus int, body []byte, err error) {
resp, err := resty.
SetTimeout(time.Duration(60) * time.Second).
SetHeaders(*r.getHeaders()).
R().SetBody(data).Put(url)
if err != nil {
if perr, ok := err.(net.Error); ok && perr.Timeout() {
return http.StatusGatewayTimeout, nil, err
} else {
return http.StatusServiceUnavailable, nil, err
}
}
defer resp.RawResponse.Body.Close()
body = resp.Body()
return resp.StatusCode(), body, nil
}
func (r *RestyClient) RestyPost(url string, data interface{}) (httpStatus int, body []byte, err error) {
resp, err := resty.
SetTimeout(time.Duration(60) * time.Second).
SetHeaders(*r.getHeaders()).
R().SetBody(data).Post(url)
if err != nil {
if perr, ok := err.(net.Error); ok && perr.Timeout() {
return http.StatusGatewayTimeout, nil, err
} else {
return http.StatusServiceUnavailable, nil, err
}
}
defer resp.RawResponse.Body.Close()
body = resp.Body()
return resp.StatusCode(), body, nil
}
func (r *RestyClient) RestyPostForm(filename string, filePath string, formData map[string]string, url string) (httpStatus int, body []byte, err error) {
fileb, err := os.ReadFile(filePath)
if err != nil {
return http.StatusInternalServerError, nil, err
}
resp, err := resty.
SetTimeout(time.Duration(60)*time.Second).
SetHeaders(*r.getHeaders()).
R().SetFileReader("file", filename, bytes.NewReader(fileb)).
SetFormData(formData).
Post(url)
if err != nil {
if perr, ok := err.(net.Error); ok && perr.Timeout() {
return http.StatusGatewayTimeout, nil, err
} else {
return http.StatusServiceUnavailable, nil, err
}
}
defer resp.RawResponse.Body.Close()
body = resp.Body()
return resp.StatusCode(), body, err
}
func (r *RestyClient) RestyDelete(url string, data interface{}) (httpStatus int, body []byte, err error) {
resp, err := resty.
SetTimeout(time.Duration(60) * time.Second).
SetHeaders(*r.getHeaders()).
R().
SetBody(data).
Delete(url)
if err != nil {
if perr, ok := err.(net.Error); ok && perr.Timeout() {
return http.StatusGatewayTimeout, nil, err
} else {
return http.StatusServiceUnavailable, nil, err
}
}
defer resp.RawResponse.Body.Close()
body = resp.Body()
return resp.StatusCode(), body, nil
}
type ExtReader func(reader *io.Reader) io.Reader
func (r *RestyClient) RestyPostFormExt(filename string, filePath string, formData map[string]string, url string, extReader io.Reader) (httpStatus int, body []byte, err error) {
var reader io.Reader
if extReader != nil {
//reader = bytes.NewReader(fileb)
reader = extReader
} else {
fileb, err := os.ReadFile(filePath)
if err != nil {
return http.StatusInternalServerError, nil, err
}
reader = bytes.NewReader(fileb)
}
resp, err := resty.
SetTimeout(time.Duration(60)*time.Second).
SetHeaders(*r.getHeaders()).
R().SetFileReader("file", filename, reader).
SetFormData(formData).
Post(url)
if err != nil {
if perr, ok := err.(net.Error); ok && perr.Timeout() {
return http.StatusGatewayTimeout, nil, err
} else {
return http.StatusServiceUnavailable, nil, err
}
}
defer resp.RawResponse.Body.Close()
body = resp.Body()
return resp.StatusCode(), body, err
}