-
Notifications
You must be signed in to change notification settings - Fork 121
/
util.go
72 lines (63 loc) · 1.83 KB
/
util.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"
"fmt"
"strconv"
"time"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
)
func floatFromString(raw interface{}) (float64, error) {
str, ok := raw.(string)
if !ok {
return 0, errors.New(fmt.Sprintf("unable to parse, value not string: %T", raw))
}
flt, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, errors.Wrap(err, fmt.Sprintf("unable to parse as float: %s", str))
}
return flt, nil
}
func intFromString(raw interface{}) (int, error) {
str, ok := raw.(string)
if !ok {
return 0, errors.New(fmt.Sprintf("unable to parse, value not string: %T", raw))
}
n, err := strconv.Atoi(str)
if err != nil {
return 0, errors.Wrap(err, fmt.Sprintf("unable to parse as int: %s", str))
}
return n, nil
}
func timeFromUnixTimestampString(raw interface{}) (time.Time, error) {
str, ok := raw.(string)
if !ok {
return time.Time{}, errors.New(fmt.Sprintf("unable to parse, value not string"))
}
ts, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return time.Time{}, errors.Wrap(err, fmt.Sprintf("unable to parse as int: %s", str))
}
return time.Unix(0, ts*int64(time.Millisecond)), nil
}
func timeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
ts, ok := raw.(float64)
if !ok {
return time.Time{}, errors.New(fmt.Sprintf("unable to parse, value not int64: %T", raw))
}
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
}
func unixMillis(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
func recvWindow(d time.Duration) int64 {
return int64(d) / int64(time.Millisecond)
}
func (as *apiService) handleError(textRes []byte) error {
err := &Error{}
level.Info(as.Logger).Log("errorResponse", textRes)
if err := json.Unmarshal(textRes, err); err != nil {
return errors.Wrap(err, "error unmarshal failed")
}
return err
}