-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
164 lines (131 loc) · 3.08 KB
/
httpclient.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
package modernmt
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"
)
func createHttpClient(baseUrl string, headers map[string]string) *httpClient {
return &httpClient{
baseUrl: baseUrl,
headers: headers,
client: &http.Client{},
}
}
func (re *httpClient) _createMultipartRequest(path string, data map[string]interface{},
files map[string]*os.File) (*http.Request, error) {
var body bytes.Buffer
w := multipart.NewWriter(&body)
for param, file := range files {
fw, err := w.CreateFormFile(param, file.Name())
if err != nil {
return nil, err
}
_, err = io.Copy(fw, file)
if err != nil {
return nil, err
}
err = file.Close()
if err != nil {
return nil, err
}
}
for key, val := range data {
var s string
switch val.(type) {
case []string:
s = strings.Join(val.([]string), ",")
case []int64:
s = strings.Trim(strings.Join(strings.Fields(fmt.Sprint(val)), ","), "[]")
case int:
s = strconv.Itoa(val.(int))
default:
s = val.(string)
}
err := w.WriteField(key, s)
if err != nil {
return nil, err
}
}
err := w.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", re.baseUrl+path, &body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
return req, nil
}
func (re *httpClient) _createJsonRequest(path string, data map[string]interface{}) (*http.Request, error) {
var body bytes.Buffer
if data != nil {
jsonBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
body = *bytes.NewBuffer(jsonBytes)
}
req, err := http.NewRequest("POST", re.baseUrl+path, &body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
return req, nil
}
func (re *httpClient) _createRequest(path string, data map[string]interface{},
files map[string]*os.File) (*http.Request, error) {
if files != nil {
return re._createMultipartRequest(path, data, files)
}
return re._createJsonRequest(path, data)
}
func (re *httpClient) send(method string, path string, data map[string]interface{}, files map[string]*os.File, headers map[string]string) (interface{}, error) {
req, err := re._createRequest(path, data, files)
if err != nil {
return nil, err
}
req.Header.Add("X-HTTP-Method-Override", method)
if re.headers != nil {
for key, val := range re.headers {
req.Header.Add(key, val)
}
}
if headers != nil {
for key, val := range headers {
req.Header.Add(key, val)
}
}
res, err := re.client.Do(req)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
status := int(result["status"].(float64))
if status >= 300 || status < 200 {
e := result["error"].(map[string]interface{})
return nil, APIError{
Status: status,
Type: e["type"].(string),
Message: e["message"].(string),
}
}
return result["data"], nil
}