This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
rpc_types.go
191 lines (159 loc) · 3.86 KB
/
rpc_types.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package deribit
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"time"
"github.com/go-openapi/runtime"
)
const rpcVersion = "2.0"
var forceSliceKeys = []string{"channels"}
// RPCRequest is what we send to the remote
// Implements runtime.ClientRequest
type RPCRequest struct {
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
ID uint64 `json:"id"`
Params map[string]interface{} `json:"params,omitempty"`
}
func NewRPCRequest(method string) *RPCRequest {
return &RPCRequest{
JsonRpc: rpcVersion,
Method: method,
Params: make(map[string]interface{}),
}
}
func (RPCRequest) SetHeaderParam(string, ...string) error {
return nil
}
func (RPCRequest) GetHeaderParams() http.Header {
return http.Header{}
}
func (r *RPCRequest) SetQueryParam(key string, vals ...string) error {
if len(vals) > 1 || r.forceSlice(key) {
r.Params[key] = vals
} else {
r.Params[key] = vals[0]
}
return nil
}
func (RPCRequest) SetFormParam(string, ...string) error {
return nil
}
func (RPCRequest) SetPathParam(string, string) error {
return nil
}
func (RPCRequest) GetQueryParams() url.Values {
return url.Values{}
}
func (RPCRequest) SetFileParam(string, ...runtime.NamedReadCloser) error {
return nil
}
func (RPCRequest) SetBodyParam(interface{}) error {
return nil
}
func (RPCRequest) SetTimeout(time.Duration) error {
return nil
}
func (RPCRequest) GetMethod() string {
return "GET"
}
func (r *RPCRequest) GetPath() string {
return r.Method
}
func (RPCRequest) GetBody() []byte {
return []byte{}
}
func (RPCRequest) GetBodyParam() interface{} {
return nil
}
func (RPCRequest) GetFileParam() map[string][]runtime.NamedReadCloser {
return nil
}
func (RPCRequest) forceSlice(key string) bool {
for _, k := range forceSliceKeys {
if k == key {
return true
}
}
return false
}
// RPCResponse is what we receive from the remote
// Implements runtime.ClientResponse
type RPCResponse struct {
JsonRpc string `json:"jsonrpc"`
ID uint64 `json:"id,omitempty"`
Result json.RawMessage `json:"result"`
Error *RPCError `json:"error,omitempty"`
}
func (r *RPCResponse) Code() int {
if r.Error != nil {
return r.Error.Code
}
return 200
}
func (r *RPCResponse) Message() string {
if r.Error != nil {
return r.Error.Message
}
return "ok"
}
func (RPCResponse) GetHeader(string) string {
return ""
}
func (r *RPCResponse) Body() io.ReadCloser {
raw := []byte(fmt.Sprintf(`{"result": %s}`, r.Result))
// Here is where we handle Deribit's idiosyncratic response types
b := r.preFilter(raw)
return ioutil.NopCloser(bytes.NewReader(b))
}
func (RPCResponse) preFilter(src []byte) []byte {
re := regexp.MustCompile(`"market_price"`)
return re.ReplaceAllFunc(src, func(in []byte) []byte {
r := regexp.MustCompile(`"average_price":([0-9.]+)`)
matches := r.FindAllSubmatch(src, 1)
if len(matches) > 0 {
return matches[0][1]
}
return in
})
}
// RPCError error object
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}
// RPCCall represents the entire call from request to response
type RPCCall struct {
Req *RPCRequest
Res *RPCResponse
Error error
Done chan bool
}
// NewRPCCall returns a new RPCCall initialised with a done channel and request
func NewRPCCall(req *RPCRequest) *RPCCall {
done := make(chan bool)
return &RPCCall{
Req: req,
Done: done,
}
}
// RPCSubscription is a subscription to an event type to receive notifications about
type RPCSubscription struct {
Data chan *RPCNotification
Channel string
}
// RPCNotification is a notification which we have subscribed to
type RPCNotification struct {
JsonRpc string
Method string `json:"method"`
Params struct {
Data json.RawMessage `json:"data"`
Channel string `json:"channel"`
} `json:"params,omitempty"`
}