-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.go
176 lines (143 loc) · 3.99 KB
/
security.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
package moexiss
import (
"bufio"
"bytes"
"context"
"fmt"
"github.com/buger/jsonparser"
)
// Security represent a security
type Security struct {
Id int64 //"0"
SecId string //"1"
ShortName string //"2"
RegNumber string //"3"
Name string //"4"
Isin string //"5"
IsTraded bool //"6"
EmitentId string //"7"
EmitentTitle string //"8"
EmitentInn string //"9"
EmitentOkpo string //"10"
GosReg string //"11"
Type string //"12"
Group string //"13"
PrimaryBoardId string //"14"
MarketPriceBoardId string //"15"
}
// SecuritiesService provides access to the security related functions
// in the MoEx ISS API.
//
// MoEx ISS API docs: https://iss.moex.com/iss/reference/5
type SecuritiesService service
// List allows to get a list of securities
func (s *SecuritiesService) List(ctx context.Context) (*[]Security, error) {
var u string = "securities.json"
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
securities := make([]Security, 0, 100)
var b bytes.Buffer
w := bufio.NewWriter(&b)
_, err = s.client.Do(ctx, req, w)
if err != nil {
return nil, err
}
err = parseSecuritiesResponse(&securities, b.Bytes())
if err != nil {
return nil, err
}
return &securities, nil
}
func parseSecuritiesResponse(securities *[]Security, byteData []byte) (err error) {
bytesSec, dataType, _, err := jsonparser.Get(byteData, "securities")
if err != nil {
return
}
if dataType != jsonparser.Object {
return fmt.Errorf("unknown type of 'securities'")
}
err = parseSecurities(securities, bytesSec)
return
}
func parseSecurities(securities *[]Security, bytesSec []byte) (err error) {
var arrayEachErr error = nil
_, err = jsonparser.ArrayEach(bytesSec, func(secItemBytes []byte, dataType jsonparser.ValueType, offset int, errCb error) {
if errCb != nil {
arrayEachErr = errCb
return
}
secItem := &Security{}
arrayEachErr = parseSecurityItem(secItem, secItemBytes)
if arrayEachErr != nil {
return
}
*securities = append(*securities, *secItem)
}, "data")
if err == nil && arrayEachErr != nil {
err = arrayEachErr
return
}
return
}
func parseSecurityItem(s *Security, secItemBytes []byte) (err error) {
if s == nil {
return fmt.Errorf("<nil> pointer passed instead of *Security")
}
counter := 0
var errInArr error
var cb = func(fieldData []byte, dataType jsonparser.ValueType, offset int, errCb error) {
if errCb != nil {
errInArr = errCb
return
}
switch counter {
case 0:
s.Id, errInArr = jsonparser.ParseInt(fieldData)
case 1:
s.SecId, errInArr = parseStringWithDefaultValue(fieldData)
case 2:
s.ShortName, errInArr = parseStringWithDefaultValue(fieldData)
case 3:
s.RegNumber, errInArr = parseStringWithDefaultValue(fieldData)
case 4:
s.Name, errInArr = parseStringWithDefaultValue(fieldData)
case 5:
s.Isin, errInArr = parseStringWithDefaultValue(fieldData)
case 6:
var res int64
if res, errInArr = jsonparser.ParseInt(fieldData); res >= 0 {
s.IsTraded = res == 1
}
case 7:
s.EmitentId, errInArr = parseStringWithDefaultValue(fieldData)
case 8:
s.EmitentTitle, errInArr = parseStringWithDefaultValue(fieldData)
case 9:
s.EmitentInn, errInArr = parseStringWithDefaultValue(fieldData)
case 10:
s.EmitentOkpo, errInArr = parseStringWithDefaultValue(fieldData)
case 11:
s.GosReg, errInArr = parseStringWithDefaultValue(fieldData)
case 12:
s.Type, errInArr = parseStringWithDefaultValue(fieldData)
case 13:
s.Group, errInArr = parseStringWithDefaultValue(fieldData)
case 14:
s.PrimaryBoardId, errInArr = parseStringWithDefaultValue(fieldData)
case 15:
s.MarketPriceBoardId, errInArr = parseStringWithDefaultValue(fieldData)
}
if errInArr != nil {
return
}
counter++
}
_, err = jsonparser.ArrayEach(secItemBytes, cb)
if err == nil && errInArr != nil {
err = errInArr
return
}
return
}