-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
165 lines (127 loc) · 3.44 KB
/
client.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
package gomoex
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"sync"
"github.com/tidwall/gjson"
)
const (
_payloadPath = `1.`
_cursorPath = `history\.cursor.`
_cursorIndex = _payloadPath + _cursorPath + `INDEX`
_cursorPage = _payloadPath + _cursorPath + `PAGESIZE`
_cursorTotal = _payloadPath + _cursorPath + `TOTAL`
)
var errLastBlock = errors.New("last block")
// ISSClient клиент для осуществления запросов к MOEX ISS.
type ISSClient struct {
client *http.Client
buffers sync.Pool
}
// NewISSClient создает клиент для осуществления запросов к MOEX ISS.
func NewISSClient(client *http.Client) *ISSClient {
iss := ISSClient{client: client}
iss.buffers.New = func() interface{} { return new(bytes.Buffer) }
return &iss
}
func (iss *ISSClient) rowsGen(ctx context.Context, query issQuery) chan interface{} {
rows := make(chan interface{})
go func(query issQuery) {
defer close(rows)
for query.err == nil {
query = iss.parseBlock(ctx, query, rows)
}
if !errors.Is(query.err, errLastBlock) {
rows <- query.err
}
}(query)
return rows
}
func (iss *ISSClient) parseBlock(ctx context.Context, query issQuery, out chan<- interface{}) issQuery {
buffer := iss.buffers.Get().(*bytes.Buffer) //nolint:forcetypeassert
defer func() {
buffer.Reset()
iss.buffers.Put(buffer)
}()
json, err := iss.getJSON(ctx, query.URL(), buffer)
if err != nil {
query.err = err
return query
}
return sendTable(json, query, out)
}
func (iss *ISSClient) getJSON(ctx context.Context, url string, buffer *bytes.Buffer) (json []byte, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, newWarpedErr("can't create request", err)
}
resp, err := iss.client.Do(req)
if err != nil {
return nil, newWarpedErr("can't make request", err)
}
defer func() {
err = resp.Body.Close()
if err != nil {
json = nil
err = newWarpedErr("can't close request", err)
}
}()
if resp.StatusCode != http.StatusOK {
return nil, newWarpedErr("got request status", err)
}
_, err = buffer.ReadFrom(resp.Body)
if err != nil {
return nil, newWarpedErr("can't read request", err)
}
return buffer.Bytes(), nil
}
func sendTable(json []byte, query issQuery, out chan<- interface{}) issQuery {
results := gjson.GetManyBytes(json, _payloadPath+query.table, _cursorIndex, _cursorPage, _cursorTotal)
table := results[0]
index := results[1]
page := results[2]
total := results[3]
if !table.IsArray() {
query.err = newErrWithMsg(fmt.Sprintf("can't find table %s", query.table))
return query
}
cursorExists := index.Exists() && page.Exists() && total.Exists()
count, err := sendRows(table, query.rowConverter, out)
query.start += count
query.err = err
switch {
case count == 0:
query.err = errLastBlock
case !query.multipart:
query.err = errLastBlock
case cursorExists:
if index.Int()+page.Int() >= total.Int() {
query.err = errLastBlock
}
}
return query
}
func sendRows(table gjson.Result, converter converter, out chan<- interface{}) (count int, err error) {
table.ForEach(
func(_, rawRow gjson.Result) bool {
if !rawRow.IsObject() {
count = 0
err = newErrWithMsg("can't parse row")
return false
}
var row interface{}
row, err = converter(rawRow)
if err != nil {
count = 0
return false
}
out <- row
count++
return true
},
)
return count, nil
}