-
Notifications
You must be signed in to change notification settings - Fork 6
/
ws_responce.go
106 lines (90 loc) · 2.1 KB
/
ws_responce.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
package bybitv5
import (
"encoding/json"
"github.com/msw-x/moon/ufmt"
"github.com/msw-x/moon/ulog"
)
type WsResponse interface {
IsOperateion() bool
OperationIs(string) bool
Ok() bool
Log(*ulog.Log)
}
type WsBaseResponse struct {
Operation string `json:"op"`
ConnectionId string `json:"conn_id"`
Success bool
Message string `json:"ret_msg"`
Args []string
}
func (o WsBaseResponse) IsOperateion() bool {
return o.Operation != ""
}
func (o WsBaseResponse) OperationIs(v string) bool {
return o.Operation == v
}
func (o WsBaseResponse) Ok() bool {
return o.Success
}
func (o WsBaseResponse) Log(log *ulog.Log) {
switch o.Operation {
case "ping":
case "pong":
case "subscribe":
if o.Success {
log.Info("subscribe: success")
} else {
log.Error("subscribe:", o.Message)
}
case "unsubscribe":
log.Info("unsubscribe:", ufmt.SuccessFailure(o.Success))
default:
log.Error("invalid response:", o.Operation)
}
}
type WsOrderData struct {
OrderId string
OrderLinkId string
}
type WsResponseHeader struct {
Limit string `json:"X-Bapi-Limit"`
LimitStatus string `json:"X-Bapi-Limit-Status"`
LimitResetTimestamp string `json:"X-Bapi-Limit-Reset-Timestamp"`
TraceId string
TimeNow string
}
type WsTradeResponse struct {
Operation string `json:"op"`
ConnectionId string `json:"connId"`
Code int `json:"retCode"`
Message string `json:"retMsg"`
Data json.RawMessage
Header WsResponseHeader
}
func (o WsTradeResponse) OrderData() (v WsOrderData, err error) {
err = json.Unmarshal(o.Data, &v)
return
}
func (o WsTradeResponse) IsOperateion() bool {
return o.Operation != ""
}
func (o WsTradeResponse) OperationIs(v string) bool {
return o.Operation == v
}
func (o WsTradeResponse) Ok() bool {
return o.Code == 0
}
func (o WsTradeResponse) Log(log *ulog.Log) {
switch o.Operation {
case "ping":
case "pong":
case "order.create":
if o.Code == 0 {
log.Info("order-create")
} else {
log.Error("order-create:", o.Message)
}
default:
log.Error("invalid response:", o.Operation)
}
}