forked from hirokisan/bybit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
151 lines (132 loc) · 3.59 KB
/
response.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
package bybit
import (
"encoding/json"
"errors"
"fmt"
"time"
)
type checkResponseBodyFunc func([]byte) error
func checkResponseBody(body []byte) error {
var commonResponse CommonResponse
if err := json.Unmarshal(body, &commonResponse); err != nil {
return err
}
switch {
case commonResponse.RetCode == 10006:
rateLimitError := &RateLimitError{}
if err := json.Unmarshal(body, rateLimitError); err != nil {
return err
}
return rateLimitError
case commonResponse.RetCode != 0:
return &ErrorResponse{
RetCode: commonResponse.RetCode,
RetMsg: commonResponse.RetMsg,
}
default:
return nil
}
}
func checkV3ResponseBody(body []byte) error {
var commonResponse CommonV3Response
if err := json.Unmarshal(body, &commonResponse); err != nil {
return err
}
switch {
case commonResponse.RetCode != 0:
return &ErrorResponse{
RetCode: commonResponse.RetCode,
RetMsg: commonResponse.RetMsg,
}
default:
return nil
}
}
func checkV5ResponseBody(body []byte) error {
var commonResponse CommonV5Response
if err := json.Unmarshal(body, &commonResponse); err != nil {
return err
}
switch {
case commonResponse.RetCode == 10006, commonResponse.RetCode == 10018:
rateLimitError := &RateLimitV5Error{}
if err := json.Unmarshal(body, rateLimitError); err != nil {
return err
}
return rateLimitError
case commonResponse.RetCode != 0:
return &ErrorResponse{
RetCode: commonResponse.RetCode,
RetMsg: commonResponse.RetMsg,
}
default:
return nil
}
}
// CommonResponse :
type CommonResponse struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
ExtCode string `json:"ext_code"`
ExtInfo string `json:"ext_info"`
TimeNow string `json:"time_now"`
RateLimitStatus int `json:"rate_limit_status"`
RateLimitResetMs int `json:"rate_limit_reset_ms"`
RateLimit int `json:"rate_limit"`
}
// CommonV3Response :
type CommonV3Response struct {
RetCode int `json:"retCode"`
RetMsg string `json:"retMsg"`
RetExtInfo interface{} `json:"retExtInfo"`
Time int `json:"time"`
}
// CommonV5Response :
type CommonV5Response struct {
RetCode int `json:"retCode"`
RetMsg string `json:"retMsg"`
RetExtInfo interface{} `json:"retExtInfo"`
Time int `json:"time"`
}
// ErrorResponse :
type ErrorResponse struct {
RetCode int `json:"ret_code"`
RetMsg string `json:"ret_msg"`
}
// Error :
func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%d, %s", r.RetCode, r.RetMsg)
}
// RateLimitError :
type RateLimitError struct {
*CommonResponse `json:",inline"`
}
func (r *RateLimitError) Error() string {
return fmt.Sprintf("%s, %s", r.RetMsg, time.Until(time.Unix(int64(r.RateLimitResetMs/1000), 0)))
}
type RateLimitV5Error struct {
*CommonV5Response `json:",inline"`
}
func (r *RateLimitV5Error) Error() string {
return fmt.Sprintf(r.RetMsg)
}
var (
// ErrBadRequest :
// Need to send the request with GET / POST (must be capitalized)
ErrBadRequest = errors.New("bad request")
// ErrInvalidRequest :
// 1. Need to use the correct key to access;
// 2. Need to put authentication params in the request header
ErrInvalidRequest = errors.New("authentication failed")
// ErrForbiddenRequest :
// Possible causes:
// 1. IP rate limit breached;
// 2. You send GET request with an empty json body;
// 3. You are using U.S IP
ErrForbiddenRequest = errors.New("access denied")
// ErrPathNotFound :
// Possible causes:
// 1. Wrong path;
// 2. Category value does not match account mode
ErrPathNotFound = errors.New("path not found")
)