-
Notifications
You must be signed in to change notification settings - Fork 0
/
turnover_request_options.go
94 lines (76 loc) · 2.68 KB
/
turnover_request_options.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
package moexiss
import (
"net/url"
"time"
)
//turnoverBlock represents a type of turnovers block of MoEx ISS API
type turnoverBlock string
const (
turnoversBlockUndefined turnoverBlock = ""
turnoversBlock turnoverBlock = "turnovers"
turnoversPrevDateBlock turnoverBlock = "turnoversprevdate"
)
//string representations of turnoverBlock values
func (tb turnoverBlock) String() string {
return string(tb)
}
//TurnoverRequestOptions contains options which can be used as arguments
//for building requests to get current turnover on all the markets.
//MoEx ISS API docs: https://iss.moex.com/iss/reference/24
type TurnoverRequestOptions struct {
lang Language //`lang` query parameter in url.URL
isTonightSession bool //`is_tonight_session` query parameter in url.URL
date time.Time //`date` query parameter in url.URL
}
//TurnoverReqOptionsBuilder represents a builder of TurnoverRequestOptions struct
type TurnoverReqOptionsBuilder struct {
options *TurnoverRequestOptions
}
//NewTurnoverReqOptionsBuilder is a constructor for TurnoverReqOptionsBuilder
func NewTurnoverReqOptionsBuilder() *TurnoverReqOptionsBuilder {
return &TurnoverReqOptionsBuilder{options: &TurnoverRequestOptions{}}
}
//Build builds TurnoverRequestOptions from TurnoverReqOptionsBuilder
func (b *TurnoverReqOptionsBuilder) Build() *TurnoverRequestOptions {
return b.options
}
//Lang sets 'lang' parameter to a request of the current turnover
func (b *TurnoverReqOptionsBuilder) Lang(lang Language) *TurnoverReqOptionsBuilder {
b.options.lang = lang
return b
}
//IsTonightSession sets 'is_tonight_session' parameter to a request of the current turnover
func (b *TurnoverReqOptionsBuilder) IsTonightSession(isTonightSession bool) *TurnoverReqOptionsBuilder {
b.options.isTonightSession = isTonightSession
return b
}
//Date sets 'date' parameter to a request of the current turnover
func (b *TurnoverReqOptionsBuilder) Date(date time.Time) *TurnoverReqOptionsBuilder {
b.options.date = date
return b
}
//addTurnoverRequestOptions sets parameters into *url.URL
//from TurnoverRequestOptions struct and returns it back
func addTurnoverRequestOptions(url *url.URL, options *TurnoverRequestOptions, onlyBlock turnoverBlock) *url.URL {
q := url.Query()
q.Set("iss.meta", "off")
if onlyBlock != turnoversBlockUndefined {
q.Set("iss.only", onlyBlock.String())
}
q.Set("iss.json", "extended")
if options == nil {
url.RawQuery = q.Encode()
return url
}
if options.lang != LangUndefined {
q.Set("lang", options.lang.String())
}
if options.isTonightSession {
q.Set("is_tonight_session", "1")
}
if !options.date.IsZero() {
q.Set("date", options.date.Format("2006-01-02"))
}
url.RawQuery = q.Encode()
return url
}