-
Notifications
You must be signed in to change notification settings - Fork 121
/
service_user_stream.go
72 lines (62 loc) · 1.66 KB
/
service_user_stream.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
package binance
import (
"encoding/json"
"io/ioutil"
"log"
"github.com/pkg/errors"
)
func (as *apiService) StartUserDataStream() (*Stream, error) {
params := make(map[string]string)
res, err := as.request("POST", "api/v1/userDataStream", params, true, false)
if err != nil {
return nil, err
}
textRes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "unable to read response from userDataStream.post")
}
defer res.Body.Close()
log.Println(string(textRes))
if res.StatusCode != 200 {
return nil, as.handleError(textRes)
}
var s Stream
if err := json.Unmarshal(textRes, &s); err != nil {
return nil, errors.Wrap(err, "stream unmarshal failed")
}
return &s, nil
}
func (as *apiService) KeepAliveUserDataStream(s *Stream) error {
params := make(map[string]string)
params["listenKey"] = s.ListenKey
res, err := as.request("PUT", "api/v1/userDataStream", params, true, false)
if err != nil {
return err
}
textRes, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "unable to read response from userDataStream.put")
}
defer res.Body.Close()
if res.StatusCode != 200 {
return as.handleError(textRes)
}
return nil
}
func (as *apiService) CloseUserDataStream(s *Stream) error {
params := make(map[string]string)
params["listenKey"] = s.ListenKey
res, err := as.request("DELETE", "api/v1/userDataStream", params, true, false)
if err != nil {
return err
}
textRes, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "unable to read response from userDataStream.delete")
}
defer res.Body.Close()
if res.StatusCode != 200 {
return as.handleError(textRes)
}
return nil
}