-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiv2.go
138 lines (112 loc) · 3.34 KB
/
apiv2.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
package app
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
)
func (m *apiResponse) parseApiResponse(schema interface{}) (e error) {
if m.Err() != nil {
return m.Err()
}
return json.Unmarshal(m.payload, &schema)
}
func (*ApiClient) debugHttpHandshake(data interface{}, withBody ...bool) {
if !gCli.Bool("http-debug") {
return
}
var body bool
if len(withBody) != 0 {
body = withBody[0]
}
var dump []byte
var err error
switch v := data.(type) {
case *http.Request:
dump, err = httputil.DumpRequestOut(data.(*http.Request), body)
case *http.Response:
dump, _ = httputil.DumpResponse(data.(*http.Response), body)
default:
gLog.Error().Msgf("there is an internal application error; undefined type - %T", v)
}
if err != nil {
gLog.Warn().Err(err).Msg("got an error in http debug proccess")
}
fmt.Println(string(dump))
}
func (*ApiClient) setApiRequestHeaders(req *http.Request) *http.Request {
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0")
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Language", "en,ru;q=0.5")
// req.Header.Set("Accept-Encoding", "gzip, deflate, br")
// req.Header.Set("Connection", "keep-alive") // !!
req.Header.Set("DNT", "1")
req.Header.Set("Upgrade-Insecure-Requests", "1")
req.Header.Set("Sec-Fetch-Dest", "document")
req.Header.Set("Sec-Fetch-Mode", "navigate")
req.Header.Set("Sec-Fetch-Site", "none")
req.Header.Set("Sec-Fetch-User", "?1")
req.Header.Set("Sec-GPC", "1")
req.Header.Set("Pragma", "no-cache")
req.Header.Set("Cache-Control", "no-cache")
return req
}
func (m *ApiClient) getApiResponse(hmethod string, amethod ApiRequestMethod, params ...interface{}) (arsp *apiResponse) {
arsp = &apiResponse{}
rrl := *m.apiBaseUrl
rrl.Path = rrl.Path + string(amethod)
rgs := &url.Values{}
rgs.Add("filter", defaultApiMethodFilter)
// panic avoid
var body io.Reader
params = append(params, nil)
for _, param := range params {
switch param := param.(type) {
case []string:
rgs.Add(param[0], param[1])
case io.Reader:
body = param
}
}
rrl.RawQuery = rgs.Encode()
fmt.Println(rrl.RawQuery)
var req *http.Request
if req, arsp.err = http.NewRequest(hmethod, rrl.String(), body); arsp.Err() != nil {
return
}
m.setApiRequestHeaders(req)
var rsp *http.Response
if rsp, arsp.err = m.http.Do(req); arsp.Err() != nil {
return
}
defer func() {
if e := rsp.Body.Close(); e != nil {
gLog.Warn().Err(e)
}
}()
m.debugHttpHandshake(req)
m.debugHttpHandshake(rsp)
if arsp.payload, arsp.err = io.ReadAll(rsp.Body); arsp.Err() != nil {
return
}
switch rsp.StatusCode {
case http.StatusOK:
gLog.Debug().Str("api_method", string(amethod)).Msg("api reqiest has been completed with response 200 OK")
default:
gLog.Warn().Str("api_method", string(amethod)).Int("api_response_code", rsp.StatusCode).
Msg("abnormal api response; trying to parse api error object...")
var aerr *apiError
if err := arsp.parseApiResponse(&aerr); err != nil {
gLog.Error().Err(arsp.Err()).Msg("got an error in parsing reponse error object")
arsp.err = errApiAbnormalResponse
return
}
gLog.Warn().Int("error_code", aerr.Error.Code).Str("error_desc", aerr.Error.Message).
Msg("api error object has been parse")
arsp.err = errApiAbnormalResponse
return
}
return
}