bbgo_origin/pkg/glassnode/lightning_request_requestgen.go

197 lines
4.5 KiB
Go
Raw Normal View History

2022-04-03 04:13:29 +00:00
// Code generated by "requestgen -method GET -type LightningRequest -url /v1/metrics/lightning/:metric -responseType Response"; DO NOT EDIT.
package glassnode
import (
"context"
"encoding/json"
"fmt"
"net/url"
"regexp"
)
func (l *LightningRequest) SetAsset(Asset string) *LightningRequest {
l.Asset = Asset
return l
}
func (l *LightningRequest) SetSince(Since int64) *LightningRequest {
l.Since = Since
return l
}
func (l *LightningRequest) SetUntil(Until int64) *LightningRequest {
l.Until = Until
return l
}
func (l *LightningRequest) SetInterval(Interval Interval) *LightningRequest {
l.Interval = Interval
return l
}
func (l *LightningRequest) SetFormat(Format Format) *LightningRequest {
l.Format = Format
return l
}
func (l *LightningRequest) SetTimestampFormat(TimestampFormat string) *LightningRequest {
l.TimestampFormat = TimestampFormat
return l
}
func (l *LightningRequest) SetMetric(Metric string) *LightningRequest {
l.Metric = Metric
return l
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (l *LightningRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check Asset field -> json key a
Asset := l.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 := l.Since
// assign parameter of Since
params["s"] = Since
// check Until field -> json key u
Until := l.Until
// assign parameter of Until
params["u"] = Until
// check Interval field -> json key i
Interval := l.Interval
// assign parameter of Interval
params["i"] = Interval
// check Format field -> json key f
Format := l.Format
// assign parameter of Format
params["f"] = Format
// check TimestampFormat field -> json key timestamp_format
TimestampFormat := l.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 (l *LightningRequest) 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 (l *LightningRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := l.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 (l *LightningRequest) GetParametersJSON() ([]byte, error) {
params, err := l.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 (l *LightningRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check Metric field -> json key metric
Metric := l.Metric
// assign parameter of Metric
params["metric"] = Metric
return params, nil
}
func (l *LightningRequest) 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 (l *LightningRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := l.GetSlugParameters()
if err != nil {
return slugs, nil
}
for k, v := range params {
slugs[k] = fmt.Sprintf("%v", v)
}
return slugs, nil
}
func (l *LightningRequest) Do(ctx context.Context) (Response, error) {
// no body params
var params interface{}
query, err := l.GetQueryParameters()
if err != nil {
return nil, err
}
apiURL := "/v1/metrics/lightning/:metric"
slugs, err := l.GetSlugsMap()
if err != nil {
return nil, err
}
apiURL = l.applySlugsToUrl(apiURL, slugs)
req, err := l.Client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := l.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
}