-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
derivatives_request_requestgen.go
196 lines (154 loc) · 4.5 KB
/
derivatives_request_requestgen.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Code generated by "requestgen -method GET -type DerivativesRequest -url /v1/metrics/derivatives/:metric -responseType Response"; DO NOT EDIT.
package glassnodeapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"regexp"
)
func (d *DerivativesRequest) SetAsset(Asset string) *DerivativesRequest {
d.Asset = Asset
return d
}
func (d *DerivativesRequest) SetSince(Since int64) *DerivativesRequest {
d.Since = Since
return d
}
func (d *DerivativesRequest) SetUntil(Until int64) *DerivativesRequest {
d.Until = Until
return d
}
func (d *DerivativesRequest) SetInterval(Interval Interval) *DerivativesRequest {
d.Interval = Interval
return d
}
func (d *DerivativesRequest) SetFormat(Format Format) *DerivativesRequest {
d.Format = Format
return d
}
func (d *DerivativesRequest) SetTimestampFormat(TimestampFormat string) *DerivativesRequest {
d.TimestampFormat = TimestampFormat
return d
}
func (d *DerivativesRequest) SetMetric(Metric string) *DerivativesRequest {
d.Metric = Metric
return d
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (d *DerivativesRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check Asset field -> json key a
Asset := d.Asset
// TEMPLATE check-required
if len(Asset) == 0 {
return nil, fmt.Errorf("a is required, empty string given")
}
// END TEMPLATE check-required
// assign parameter of Asset
params["a"] = Asset
// check Since field -> json key s
Since := d.Since
// assign parameter of Since
params["s"] = Since
// check Until field -> json key u
Until := d.Until
// assign parameter of Until
params["u"] = Until
// check Interval field -> json key i
Interval := d.Interval
// assign parameter of Interval
params["i"] = Interval
// check Format field -> json key f
Format := d.Format
// assign parameter of Format
params["f"] = Format
// check TimestampFormat field -> json key timestamp_format
TimestampFormat := d.TimestampFormat
// assign parameter of TimestampFormat
params["timestamp_format"] = TimestampFormat
query := url.Values{}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (d *DerivativesRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (d *DerivativesRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := d.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (d *DerivativesRequest) GetParametersJSON() ([]byte, error) {
params, err := d.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (d *DerivativesRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check Metric field -> json key metric
Metric := d.Metric
// assign parameter of Metric
params["metric"] = Metric
return params, nil
}
func (d *DerivativesRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for k, v := range slugs {
needleRE := regexp.MustCompile(":" + k + "\\b")
url = needleRE.ReplaceAllString(url, v)
}
return url
}
func (d *DerivativesRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := d.GetSlugParameters()
if err != nil {
return slugs, nil
}
for k, v := range params {
slugs[k] = fmt.Sprintf("%v", v)
}
return slugs, nil
}
func (d *DerivativesRequest) Do(ctx context.Context) (Response, error) {
// no body params
var params interface{}
query, err := d.GetQueryParameters()
if err != nil {
return nil, err
}
apiURL := "/v1/metrics/derivatives/:metric"
slugs, err := d.GetSlugsMap()
if err != nil {
return nil, err
}
apiURL = d.applySlugsToUrl(apiURL, slugs)
req, err := d.Client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := d.Client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse Response
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
return apiResponse, nil
}