-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-client.go
104 lines (89 loc) · 2.32 KB
/
http-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
package go_sdk_cargo_sdek
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httputil"
"os"
"strconv"
"time"
)
func (c *Client) get(method string, res, resErr interface{}) (int, error) {
code, body, err := c.http(http.MethodGet, method, nil)
if err != nil {
panic(err)
}
return c.res(code, body, res, resErr)
}
func (c *Client) post(method string, req, res, resErr interface{}) (int, error) {
code, body, err := c.http(http.MethodPost, method, req)
if err != nil {
panic(err)
}
return c.res(code, body, res, resErr)
}
func (c *Client) res(code int, body []byte, res, resErr interface{}) (int, error) {
var err error
if code == 200 || code == 202 {
// Ok
err = json.Unmarshal(body, res)
if err != nil {
panic(err)
}
return code, nil
} else {
// ERR
err = json.Unmarshal(body, resErr)
if err != nil {
panic(err)
}
log.Println("[E] client.POST", string(body))
return code, nil
}
}
func (c *Client) http(httpMethod string, method string, reqJson interface{}) (int, []byte, error) {
var bodyReq []byte
if reqJson != nil {
bodyReq, _ = json.MarshalIndent(reqJson, "", " ")
}
u := c.endPoint + method
req, err := http.NewRequest(httpMethod, u, bytes.NewReader(bodyReq))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json;charset=utf-8")
req.Header.Set("Authorization", "Bearer "+c.token)
if c.DEBUG {
reqDump, _ := httputil.DumpRequest(req, true)
log.Printf("[D] REQ:\n%s\n\n", string(reqDump))
fn, err := os.Create(LOG_DIR + time.Now().Format("2006-01-02_15-04-05.000000000"+"_"+httpMethod+".req"))
if err != nil {
panic(err)
}
defer func() { _ = fn.Close() }()
_, _ = fn.Write(reqDump)
_ = fn.Close()
}
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer func() { _ = res.Body.Close() }()
if c.DEBUG {
resDump, _ := httputil.DumpResponse(res, true)
log.Printf("[D] RES:\n%s\n\n", string(resDump))
fName := time.Now().Format("2006-01-02_15-04-05.000000000") + "_" + httpMethod + "_" + strconv.Itoa(res.StatusCode) + ".res"
fn, err := os.Create(LOG_DIR + fName)
if err != nil {
panic(err)
}
defer func() { _ = fn.Close() }()
_, _ = fn.Write(resDump)
_ = fn.Close()
}
body, _ := io.ReadAll(res.Body)
return res.StatusCode, body, err
}