use requestgen.BaseAPIClient

This commit is contained in:
なるみ 2022-05-11 17:49:59 +00:00 committed by なるみ
parent 65606b2c66
commit 5d096d39bb
7 changed files with 1079 additions and 405 deletions

View File

@ -1,22 +1,19 @@
package v1
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/c9s/requestgen"
)
const baseURL = "https://pro-api.coinmarketcap.com"
const defaultHTTPTimeout = time.Second * 15
type RestClient struct {
BaseURL *url.URL
Client *http.Client
requestgen.BaseAPIClient
apiKey string
}
@ -28,8 +25,12 @@ func New() *RestClient {
}
return &RestClient{
BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u,
Client: &http.Client{},
HttpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
},
}
}
@ -37,48 +38,6 @@ func (c *RestClient) Auth(apiKey string) {
c.apiKey = apiKey
}
func (c *RestClient) NewRequest(ctx context.Context, method string, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
rel, err := url.Parse(refURL)
if err != nil {
return nil, err
}
if params != nil {
rel.RawQuery = params.Encode()
}
pathURL := c.BaseURL.ResolveReference(rel)
body, err := castPayload(payload)
if err != nil {
return nil, err
}
fmt.Println(pathURL.String())
return http.NewRequestWithContext(ctx, method, pathURL.String(), bytes.NewReader(body))
}
func (c *RestClient) SendRequest(req *http.Request) (*requestgen.Response, error) {
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
response, err := requestgen.NewResponse(resp)
if err != nil {
return response, err
}
// Check error, if there is an error, return the ErrorResponse struct type
if response.IsError() {
return response, errors.New(string(response.Body))
}
return response, nil
}
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
req, err := c.NewRequest(ctx, method, refURL, params, payload)
if err != nil {
@ -87,25 +46,9 @@ func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
// Attach API Key to header. https://coinmarketcap.com/api/documentation/v1/#section/Authentication
req.Header.Add("X-CMC_PRO_API_KEY", c.apiKey)
return req, nil
}
func castPayload(payload interface{}) ([]byte, error) {
if payload != nil {
switch v := payload.(type) {
case string:
return []byte(v), nil
case []byte:
return v, nil
default:
body, err := json.Marshal(v)
return body, err
}
}
return nil, nil
}

View File

@ -1,94 +1,56 @@
package v1
import "github.com/c9s/requestgen"
import (
"github.com/c9s/requestgen"
)
type RequestOptions struct {
Start int `json:"start,omitempty"`
Limit int `json:"limit,omitempty"`
PriceMin float64 `json:"price_min,omitempty"`
PriceMax float64 `json:"price_max,omitempty"`
MarketCapMin float64 `json:"market_cap_min,omitempty"`
MarketCapMax float64 `json:"market_cap_max,omitempty"`
Volume24HMin float64 `json:"volume_24h_min,omitempty"`
Volume24HMax float64 `json:"volume_24h_max,omitempty"`
CirculatingSupplyMin float64 `json:"circulating_supply_min,omitempty"`
CirculatingSupplyMax float64 `json:"circulating_supply_max,omitempty"`
PercentChange24HMin float64 `json:"percent_change_24h_min,omitempty"`
PercentChange24HMax float64 `json:"percent_change_24h_max,omitempty"`
Convert string `json:"convert,omitempty"`
ConvertID string `json:"convert_id,omitempty"`
Sort string `json:"sort,omitempty"`
SortDir string `json:"sort_dir"`
CryptocurrencyType string `json:"cryptocurrency_type,omitempty"`
Tag string `json:"tag,omitempty"`
Aux string `json:"aux,omitempty"`
//go:generate requestgen -method GET -url "/v1/cryptocurrency/listings/historical" -type ListingsHistoricalRequest -responseType Response -responseDataField Data -responseDataType []Data
type ListingsHistoricalRequest struct {
Client requestgen.AuthenticatedAPIClient
Date string `param:"date,query,required"`
Start *int `param:"start,query" default:"1"`
Limit *int `param:"limit,query" default:"100"`
Convert *string `param:"convert,query"`
ConvertID *string `param:"convert_id,query"`
Sort *string `param:"sort,query" default:"cmc_rank" validValues:"cmc_rank,name,symbol,market_cap,price,circulating_supply,total_supply,max_supply,num_market_pairs,volume_24h,percent_change_1h,percent_change_24h,percent_change_7d"`
SortDir *string `param:"sort_dir,query" validValues:"asc,desc"`
CryptocurrencyType *string `param:"cryptocurrency_type,query" default:"all" validValues:"all,coins,tokens"`
Aux *string `param:"aux,query" default:"platform,tags,date_added,circulating_supply,total_supply,max_supply,cmc_rank,num_market_pairs"`
}
//go:generate requestgen -type ListingsRequest -method GET -url "/v1/cryptocurrency/listings/latest" -responseType Response
type ListingsRequest struct {
client requestgen.AuthenticatedAPIClient
//go:generate requestgen -method GET -url "/v1/cryptocurrency/listings/latest" -type ListingsLatestRequest -responseType Response -responseDataField Data -responseDataType []Data
type ListingsLatestRequest struct {
Client requestgen.AuthenticatedAPIClient
endpointType EndpointType `param:"endpointType,slug"`
start int `param:"start,query"`
limit int `param:"limit,query"`
// priceMin float64 `param:"price_min,query"`
// priceMax float64 `param:"price_max,query"`
// marketCapMin float64 `param:"market_cap_min,query"`
// marketCapMax float64 `param:"market_cap_max,query"`
// volume24HMin float64 `param:"volume_24h_min,query"`
// volume24HMax float64 `param:"volume_24h_max,query"`
// circulatingSupplyMin float64 `param:"circulating_supply_min,query"`
// circulatingSupplyMax float64 `param:"circulating_supply_max,query"`
// percentChange24HMin float64 `param:"percent_change_24h_min,query"`
// percentChange24HMax float64 `param:"percent_change_24h_max,query"`
// convert string `param:"convert,query"`
// convertID string `param:"convert_id,query"`
sort string `param:"sort,query"`
sortDir string `param:"sort_dir,query"`
cryptocurrencyType string `param:"cryptocurrency_type,query"`
tag string `param:"tag,query"`
aux string `param:"aux,query"`
Start *int `param:"start,query" default:"1"`
Limit *int `param:"limit,query" default:"100"`
PriceMin *float64 `param:"price_min,query"`
PriceMax *float64 `param:"price_max,query"`
MarketCapMin *float64 `param:"market_cap_min,query"`
MarketCapMax *float64 `param:"market_cap_max,query"`
Volume24HMin *float64 `param:"volume_24h_min,query"`
Volume24HMax *float64 `param:"volume_24h_max,query"`
CirculatingSupplyMin *float64 `param:"circulating_supply_min,query"`
CirculatingSupplyMax *float64 `param:"circulating_supply_max,query"`
PercentChange24HMin *float64 `param:"percent_change_24h_min,query"`
PercentChange24HMax *float64 `param:"percent_change_24h_max,query"`
Convert *string `param:"convert,query"`
ConvertID *string `param:"convert_id,query"`
Sort *string `param:"sort,query" default:"market_cap" validValues:"name,symbol,date_added,market_cap,market_cap_strict,price,circulating_supply,total_supply,max_supply,num_market_pairs,volume_24h,percent_change_1h,percent_change_24h,percent_change_7d,market_cap_by_total_supply_strict,volume_7d,volume_30d"`
SortDir *string `param:"sort_dir,query" validValues:"asc,desc"`
CryptocurrencyType *string `param:"cryptocurrency_type,query" default:"all" validValues:"all,coins,tokens"`
Tag *string `param:"tag,query" default:"all" validValues:"all,defi,filesharing"`
Aux *string `param:"aux,query" default:"num_market_pairs,cmc_rank,date_added,tags,platform,max_supply,circulating_supply,total_supply"`
}
func NewListingsRequest(client requestgen.AuthenticatedAPIClient, options *RequestOptions) *ListingsRequest {
if options.Start == 0 {
options.Start = 1
}
//go:generate requestgen -method GET -url "/v1/cryptocurrency/listings/new" -type ListingsNewRequest -responseType Response -responseDataField Data -responseDataType []Data
type ListingsNewRequest struct {
Client requestgen.AuthenticatedAPIClient
if options.Limit == 0 {
options.Limit = 100
}
if options.Sort == "" {
options.Sort = "market_cap"
}
if options.SortDir == "" {
options.SortDir = "asc"
}
if options.CryptocurrencyType == "" {
options.CryptocurrencyType = "all"
}
if options.Tag == "" {
options.Tag = "all"
}
if options.Aux == "" {
options.Aux = "num_market_pairs,cmc_rank,date_added,tags,platform,max_supply,circulating_supply,total_supply"
}
return &ListingsRequest{
client: client,
start: options.Start,
limit: options.Limit,
sort: options.Sort,
sortDir: options.SortDir,
cryptocurrencyType: options.CryptocurrencyType,
tag: options.Tag,
aux: options.Aux,
}
Start *int `param:"start,query" default:"1"`
Limit *int `param:"limit,query" default:"100"`
Convert *string `param:"convert,query"`
ConvertID *string `param:"convert_id,query"`
SortDir *string `param:"sort_dir,query" validValues:"asc,desc"`
}

View File

@ -0,0 +1,315 @@
// Code generated by "requestgen -method GET -url /v1/cryptocurrency/listings/historical -type ListingsHistoricalRequest -responseType Response -responseDataField Data -responseDataType []Data"; DO NOT EDIT.
package v1
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (l *ListingsHistoricalRequest) SetDate(Date string) *ListingsHistoricalRequest {
l.Date = Date
return l
}
func (l *ListingsHistoricalRequest) SetStart(Start int) *ListingsHistoricalRequest {
l.Start = &Start
return l
}
func (l *ListingsHistoricalRequest) SetLimit(Limit int) *ListingsHistoricalRequest {
l.Limit = &Limit
return l
}
func (l *ListingsHistoricalRequest) SetConvert(Convert string) *ListingsHistoricalRequest {
l.Convert = &Convert
return l
}
func (l *ListingsHistoricalRequest) SetConvertID(ConvertID string) *ListingsHistoricalRequest {
l.ConvertID = &ConvertID
return l
}
func (l *ListingsHistoricalRequest) SetSort(Sort string) *ListingsHistoricalRequest {
l.Sort = &Sort
return l
}
func (l *ListingsHistoricalRequest) SetSortDir(SortDir string) *ListingsHistoricalRequest {
l.SortDir = &SortDir
return l
}
func (l *ListingsHistoricalRequest) SetCryptocurrencyType(CryptocurrencyType string) *ListingsHistoricalRequest {
l.CryptocurrencyType = &CryptocurrencyType
return l
}
func (l *ListingsHistoricalRequest) SetAux(Aux string) *ListingsHistoricalRequest {
l.Aux = &Aux
return l
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (l *ListingsHistoricalRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check Date field -> json key date
Date := l.Date
// TEMPLATE check-required
if len(Date) == 0 {
return nil, fmt.Errorf("date is required, empty string given")
}
// END TEMPLATE check-required
// assign parameter of Date
params["date"] = Date
// check Start field -> json key start
if l.Start != nil {
Start := *l.Start
// assign parameter of Start
params["start"] = Start
} else {
Start := 1
// assign parameter of Start
params["start"] = Start
}
// check Limit field -> json key limit
if l.Limit != nil {
Limit := *l.Limit
// assign parameter of Limit
params["limit"] = Limit
} else {
Limit := 100
// assign parameter of Limit
params["limit"] = Limit
}
// check Convert field -> json key convert
if l.Convert != nil {
Convert := *l.Convert
// assign parameter of Convert
params["convert"] = Convert
} else {
}
// check ConvertID field -> json key convert_id
if l.ConvertID != nil {
ConvertID := *l.ConvertID
// assign parameter of ConvertID
params["convert_id"] = ConvertID
} else {
}
// check Sort field -> json key sort
if l.Sort != nil {
Sort := *l.Sort
// TEMPLATE check-valid-values
switch Sort {
case "cmc_rank", "name", "symbol", "market_cap", "price", "circulating_supply", "total_supply", "max_supply", "num_market_pairs", "volume_24h", "percent_change_1h", "percent_change_24h", "percent_change_7d":
params["sort"] = Sort
default:
return nil, fmt.Errorf("sort value %v is invalid", Sort)
}
// END TEMPLATE check-valid-values
// assign parameter of Sort
params["sort"] = Sort
} else {
Sort := "cmc_rank"
// assign parameter of Sort
params["sort"] = Sort
}
// check SortDir field -> json key sort_dir
if l.SortDir != nil {
SortDir := *l.SortDir
// TEMPLATE check-valid-values
switch SortDir {
case "asc", "desc":
params["sort_dir"] = SortDir
default:
return nil, fmt.Errorf("sort_dir value %v is invalid", SortDir)
}
// END TEMPLATE check-valid-values
// assign parameter of SortDir
params["sort_dir"] = SortDir
} else {
}
// check CryptocurrencyType field -> json key cryptocurrency_type
if l.CryptocurrencyType != nil {
CryptocurrencyType := *l.CryptocurrencyType
// TEMPLATE check-valid-values
switch CryptocurrencyType {
case "all", "coins", "tokens":
params["cryptocurrency_type"] = CryptocurrencyType
default:
return nil, fmt.Errorf("cryptocurrency_type value %v is invalid", CryptocurrencyType)
}
// END TEMPLATE check-valid-values
// assign parameter of CryptocurrencyType
params["cryptocurrency_type"] = CryptocurrencyType
} else {
CryptocurrencyType := "all"
// assign parameter of CryptocurrencyType
params["cryptocurrency_type"] = CryptocurrencyType
}
// check Aux field -> json key aux
if l.Aux != nil {
Aux := *l.Aux
// assign parameter of Aux
params["aux"] = Aux
} else {
Aux := "platform,tags,date_added,circulating_supply,total_supply,max_supply,cmc_rank,num_market_pairs"
// assign parameter of Aux
params["aux"] = Aux
}
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 *ListingsHistoricalRequest) 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 *ListingsHistoricalRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := l.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if l.isVarSlice(_v) {
l.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (l *ListingsHistoricalRequest) 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 *ListingsHistoricalRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (l *ListingsHistoricalRequest) 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 *ListingsHistoricalRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (l *ListingsHistoricalRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (l *ListingsHistoricalRequest) 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 *ListingsHistoricalRequest) Do(ctx context.Context) ([]Data, error) {
// no body params
var params interface{}
query, err := l.GetQueryParameters()
if err != nil {
return nil, err
}
apiURL := "/v1/cryptocurrency/listings/historical"
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
}
var data []Data
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -0,0 +1,457 @@
// Code generated by "requestgen -method GET -url /v1/cryptocurrency/listings/latest -type ListingsLatestRequest -responseType Response -responseDataField Data -responseDataType []Data"; DO NOT EDIT.
package v1
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (l *ListingsLatestRequest) SetStart(Start int) *ListingsLatestRequest {
l.Start = &Start
return l
}
func (l *ListingsLatestRequest) SetLimit(Limit int) *ListingsLatestRequest {
l.Limit = &Limit
return l
}
func (l *ListingsLatestRequest) SetPriceMin(PriceMin float64) *ListingsLatestRequest {
l.PriceMin = &PriceMin
return l
}
func (l *ListingsLatestRequest) SetPriceMax(PriceMax float64) *ListingsLatestRequest {
l.PriceMax = &PriceMax
return l
}
func (l *ListingsLatestRequest) SetMarketCapMin(MarketCapMin float64) *ListingsLatestRequest {
l.MarketCapMin = &MarketCapMin
return l
}
func (l *ListingsLatestRequest) SetMarketCapMax(MarketCapMax float64) *ListingsLatestRequest {
l.MarketCapMax = &MarketCapMax
return l
}
func (l *ListingsLatestRequest) SetVolume24HMin(Volume24HMin float64) *ListingsLatestRequest {
l.Volume24HMin = &Volume24HMin
return l
}
func (l *ListingsLatestRequest) SetVolume24HMax(Volume24HMax float64) *ListingsLatestRequest {
l.Volume24HMax = &Volume24HMax
return l
}
func (l *ListingsLatestRequest) SetCirculatingSupplyMin(CirculatingSupplyMin float64) *ListingsLatestRequest {
l.CirculatingSupplyMin = &CirculatingSupplyMin
return l
}
func (l *ListingsLatestRequest) SetCirculatingSupplyMax(CirculatingSupplyMax float64) *ListingsLatestRequest {
l.CirculatingSupplyMax = &CirculatingSupplyMax
return l
}
func (l *ListingsLatestRequest) SetPercentChange24HMin(PercentChange24HMin float64) *ListingsLatestRequest {
l.PercentChange24HMin = &PercentChange24HMin
return l
}
func (l *ListingsLatestRequest) SetPercentChange24HMax(PercentChange24HMax float64) *ListingsLatestRequest {
l.PercentChange24HMax = &PercentChange24HMax
return l
}
func (l *ListingsLatestRequest) SetConvert(Convert string) *ListingsLatestRequest {
l.Convert = &Convert
return l
}
func (l *ListingsLatestRequest) SetConvertID(ConvertID string) *ListingsLatestRequest {
l.ConvertID = &ConvertID
return l
}
func (l *ListingsLatestRequest) SetSort(Sort string) *ListingsLatestRequest {
l.Sort = &Sort
return l
}
func (l *ListingsLatestRequest) SetSortDir(SortDir string) *ListingsLatestRequest {
l.SortDir = &SortDir
return l
}
func (l *ListingsLatestRequest) SetCryptocurrencyType(CryptocurrencyType string) *ListingsLatestRequest {
l.CryptocurrencyType = &CryptocurrencyType
return l
}
func (l *ListingsLatestRequest) SetTag(Tag string) *ListingsLatestRequest {
l.Tag = &Tag
return l
}
func (l *ListingsLatestRequest) SetAux(Aux string) *ListingsLatestRequest {
l.Aux = &Aux
return l
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (l *ListingsLatestRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check Start field -> json key start
if l.Start != nil {
Start := *l.Start
// assign parameter of Start
params["start"] = Start
} else {
Start := 1
// assign parameter of Start
params["start"] = Start
}
// check Limit field -> json key limit
if l.Limit != nil {
Limit := *l.Limit
// assign parameter of Limit
params["limit"] = Limit
} else {
Limit := 100
// assign parameter of Limit
params["limit"] = Limit
}
// check PriceMin field -> json key price_min
if l.PriceMin != nil {
PriceMin := *l.PriceMin
// assign parameter of PriceMin
params["price_min"] = PriceMin
} else {
}
// check PriceMax field -> json key price_max
if l.PriceMax != nil {
PriceMax := *l.PriceMax
// assign parameter of PriceMax
params["price_max"] = PriceMax
} else {
}
// check MarketCapMin field -> json key market_cap_min
if l.MarketCapMin != nil {
MarketCapMin := *l.MarketCapMin
// assign parameter of MarketCapMin
params["market_cap_min"] = MarketCapMin
} else {
}
// check MarketCapMax field -> json key market_cap_max
if l.MarketCapMax != nil {
MarketCapMax := *l.MarketCapMax
// assign parameter of MarketCapMax
params["market_cap_max"] = MarketCapMax
} else {
}
// check Volume24HMin field -> json key volume_24h_min
if l.Volume24HMin != nil {
Volume24HMin := *l.Volume24HMin
// assign parameter of Volume24HMin
params["volume_24h_min"] = Volume24HMin
} else {
}
// check Volume24HMax field -> json key volume_24h_max
if l.Volume24HMax != nil {
Volume24HMax := *l.Volume24HMax
// assign parameter of Volume24HMax
params["volume_24h_max"] = Volume24HMax
} else {
}
// check CirculatingSupplyMin field -> json key circulating_supply_min
if l.CirculatingSupplyMin != nil {
CirculatingSupplyMin := *l.CirculatingSupplyMin
// assign parameter of CirculatingSupplyMin
params["circulating_supply_min"] = CirculatingSupplyMin
} else {
}
// check CirculatingSupplyMax field -> json key circulating_supply_max
if l.CirculatingSupplyMax != nil {
CirculatingSupplyMax := *l.CirculatingSupplyMax
// assign parameter of CirculatingSupplyMax
params["circulating_supply_max"] = CirculatingSupplyMax
} else {
}
// check PercentChange24HMin field -> json key percent_change_24h_min
if l.PercentChange24HMin != nil {
PercentChange24HMin := *l.PercentChange24HMin
// assign parameter of PercentChange24HMin
params["percent_change_24h_min"] = PercentChange24HMin
} else {
}
// check PercentChange24HMax field -> json key percent_change_24h_max
if l.PercentChange24HMax != nil {
PercentChange24HMax := *l.PercentChange24HMax
// assign parameter of PercentChange24HMax
params["percent_change_24h_max"] = PercentChange24HMax
} else {
}
// check Convert field -> json key convert
if l.Convert != nil {
Convert := *l.Convert
// assign parameter of Convert
params["convert"] = Convert
} else {
}
// check ConvertID field -> json key convert_id
if l.ConvertID != nil {
ConvertID := *l.ConvertID
// assign parameter of ConvertID
params["convert_id"] = ConvertID
} else {
}
// check Sort field -> json key sort
if l.Sort != nil {
Sort := *l.Sort
// TEMPLATE check-valid-values
switch Sort {
case "name", "symbol", "date_added", "market_cap", "market_cap_strict", "price", "circulating_supply", "total_supply", "max_supply", "num_market_pairs", "volume_24h", "percent_change_1h", "percent_change_24h", "percent_change_7d", "market_cap_by_total_supply_strict", "volume_7d", "volume_30d":
params["sort"] = Sort
default:
return nil, fmt.Errorf("sort value %v is invalid", Sort)
}
// END TEMPLATE check-valid-values
// assign parameter of Sort
params["sort"] = Sort
} else {
Sort := "market_cap"
// assign parameter of Sort
params["sort"] = Sort
}
// check SortDir field -> json key sort_dir
if l.SortDir != nil {
SortDir := *l.SortDir
// TEMPLATE check-valid-values
switch SortDir {
case "asc", "desc":
params["sort_dir"] = SortDir
default:
return nil, fmt.Errorf("sort_dir value %v is invalid", SortDir)
}
// END TEMPLATE check-valid-values
// assign parameter of SortDir
params["sort_dir"] = SortDir
} else {
}
// check CryptocurrencyType field -> json key cryptocurrency_type
if l.CryptocurrencyType != nil {
CryptocurrencyType := *l.CryptocurrencyType
// TEMPLATE check-valid-values
switch CryptocurrencyType {
case "all", "coins", "tokens":
params["cryptocurrency_type"] = CryptocurrencyType
default:
return nil, fmt.Errorf("cryptocurrency_type value %v is invalid", CryptocurrencyType)
}
// END TEMPLATE check-valid-values
// assign parameter of CryptocurrencyType
params["cryptocurrency_type"] = CryptocurrencyType
} else {
CryptocurrencyType := "all"
// assign parameter of CryptocurrencyType
params["cryptocurrency_type"] = CryptocurrencyType
}
// check Tag field -> json key tag
if l.Tag != nil {
Tag := *l.Tag
// TEMPLATE check-valid-values
switch Tag {
case "all", "defi", "filesharing":
params["tag"] = Tag
default:
return nil, fmt.Errorf("tag value %v is invalid", Tag)
}
// END TEMPLATE check-valid-values
// assign parameter of Tag
params["tag"] = Tag
} else {
Tag := "all"
// assign parameter of Tag
params["tag"] = Tag
}
// check Aux field -> json key aux
if l.Aux != nil {
Aux := *l.Aux
// assign parameter of Aux
params["aux"] = Aux
} else {
Aux := "num_market_pairs,cmc_rank,date_added,tags,platform,max_supply,circulating_supply,total_supply"
// assign parameter of Aux
params["aux"] = Aux
}
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 *ListingsLatestRequest) 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 *ListingsLatestRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := l.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if l.isVarSlice(_v) {
l.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (l *ListingsLatestRequest) 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 *ListingsLatestRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (l *ListingsLatestRequest) 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 *ListingsLatestRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (l *ListingsLatestRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (l *ListingsLatestRequest) 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 *ListingsLatestRequest) Do(ctx context.Context) ([]Data, error) {
// no body params
var params interface{}
query, err := l.GetQueryParameters()
if err != nil {
return nil, err
}
apiURL := "/v1/cryptocurrency/listings/latest"
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
}
var data []Data
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -0,0 +1,226 @@
// Code generated by "requestgen -method GET -url /v1/cryptocurrency/listings/new -type ListingsNewRequest -responseType Response -responseDataField Data -responseDataType []Data"; DO NOT EDIT.
package v1
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (l *ListingsNewRequest) SetStart(Start int) *ListingsNewRequest {
l.Start = &Start
return l
}
func (l *ListingsNewRequest) SetLimit(Limit int) *ListingsNewRequest {
l.Limit = &Limit
return l
}
func (l *ListingsNewRequest) SetConvert(Convert string) *ListingsNewRequest {
l.Convert = &Convert
return l
}
func (l *ListingsNewRequest) SetConvertID(ConvertID string) *ListingsNewRequest {
l.ConvertID = &ConvertID
return l
}
func (l *ListingsNewRequest) SetSortDir(SortDir string) *ListingsNewRequest {
l.SortDir = &SortDir
return l
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (l *ListingsNewRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check Start field -> json key start
if l.Start != nil {
Start := *l.Start
// assign parameter of Start
params["start"] = Start
} else {
Start := 1
// assign parameter of Start
params["start"] = Start
}
// check Limit field -> json key limit
if l.Limit != nil {
Limit := *l.Limit
// assign parameter of Limit
params["limit"] = Limit
} else {
Limit := 100
// assign parameter of Limit
params["limit"] = Limit
}
// check Convert field -> json key convert
if l.Convert != nil {
Convert := *l.Convert
// assign parameter of Convert
params["convert"] = Convert
} else {
}
// check ConvertID field -> json key convert_id
if l.ConvertID != nil {
ConvertID := *l.ConvertID
// assign parameter of ConvertID
params["convert_id"] = ConvertID
} else {
}
// check SortDir field -> json key sort_dir
if l.SortDir != nil {
SortDir := *l.SortDir
// TEMPLATE check-valid-values
switch SortDir {
case "asc", "desc":
params["sort_dir"] = SortDir
default:
return nil, fmt.Errorf("sort_dir value %v is invalid", SortDir)
}
// END TEMPLATE check-valid-values
// assign parameter of SortDir
params["sort_dir"] = SortDir
} else {
}
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 *ListingsNewRequest) 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 *ListingsNewRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := l.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if l.isVarSlice(_v) {
l.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (l *ListingsNewRequest) 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 *ListingsNewRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
func (l *ListingsNewRequest) 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 *ListingsNewRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (l *ListingsNewRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (l *ListingsNewRequest) 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 *ListingsNewRequest) Do(ctx context.Context) ([]json.RawMessage, error) {
// no body params
var params interface{}
query, err := l.GetQueryParameters()
if err != nil {
return nil, err
}
apiURL := "/v1/cryptocurrency/listings/new"
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
}
var data []json.RawMessage
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
return nil, err
}
return data, nil
}

View File

@ -1,224 +0,0 @@
// Code generated by "requestgen -type ListingsRequest -method GET -url /v1/cryptocurrency/listings/latest -responseType Response"; DO NOT EDIT.
package v1
import (
"context"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
)
func (l *ListingsRequest) Start(start int) *ListingsRequest {
l.start = start
return l
}
func (l *ListingsRequest) Limit(limit int) *ListingsRequest {
l.limit = limit
return l
}
func (l *ListingsRequest) Sort(sort string) *ListingsRequest {
l.sort = sort
return l
}
func (l *ListingsRequest) SortDir(sortDir string) *ListingsRequest {
l.sortDir = sortDir
return l
}
func (l *ListingsRequest) CryptocurrencyType(cryptocurrencyType string) *ListingsRequest {
l.cryptocurrencyType = cryptocurrencyType
return l
}
func (l *ListingsRequest) Tag(tag string) *ListingsRequest {
l.tag = tag
return l
}
func (l *ListingsRequest) Aux(aux string) *ListingsRequest {
l.aux = aux
return l
}
func (l *ListingsRequest) EndpointType(endpointType EndpointType) *ListingsRequest {
l.endpointType = endpointType
return l
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (l *ListingsRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
// check start field -> json key start
start := l.start
// assign parameter of start
params["start"] = start
// check limit field -> json key limit
limit := l.limit
// assign parameter of limit
params["limit"] = limit
// check sort field -> json key sort
sort := l.sort
// assign parameter of sort
params["sort"] = sort
// check sortDir field -> json key sort_dir
sortDir := l.sortDir
// assign parameter of sortDir
params["sort_dir"] = sortDir
// check cryptocurrencyType field -> json key cryptocurrency_type
cryptocurrencyType := l.cryptocurrencyType
// assign parameter of cryptocurrencyType
params["cryptocurrency_type"] = cryptocurrencyType
// check tag field -> json key tag
tag := l.tag
// assign parameter of tag
params["tag"] = tag
// check aux field -> json key aux
aux := l.aux
// assign parameter of aux
params["aux"] = aux
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 *ListingsRequest) 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 *ListingsRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := l.GetParameters()
if err != nil {
return query, err
}
for _k, _v := range params {
if l.isVarSlice(_v) {
l.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (l *ListingsRequest) 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 *ListingsRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check endpointType field -> json key endpointType
endpointType := l.endpointType
// assign parameter of endpointType
params["endpointType"] = endpointType
return params, nil
}
func (l *ListingsRequest) 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 *ListingsRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (l *ListingsRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (l *ListingsRequest) 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 *ListingsRequest) Do(ctx context.Context) (*Response, error) {
// no body params
var params interface{}
query, err := l.GetQueryParameters()
if err != nil {
return nil, err
}
apiURL := "/v1/cryptocurrency/listings/latest"
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
}

View File

@ -1,22 +1,36 @@
package v1
import (
"encoding/json"
"time"
)
type EndpointType string
const (
EndpointTypeHistorical EndpointType = "historical"
EndpointTypeLatest EndpointType = "latest"
EndpointTypeNew EndpointType = "new"
)
type Response struct {
Data []Data `json:"data"`
Data json.RawMessage `json:"data"`
Status Status `json:"status"`
}
type Data struct {
ID int64 `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
CmcRank int64 `json:"cmc_rank,omitempty"`
IsActive bool `json:"is_active,omitempty"`
IsFiat int64 `json:"is_fiat,omitempty"`
NumMarketPairs int64 `json:"num_market_pairs"`
CirculatingSupply float64 `json:"circulating_supply"`
TotalSupply float64 `json:"total_supply"`
MaxSupply float64 `json:"max_supply"`
LastUpdated time.Time `json:"last_updated"`
DateAdded time.Time `json:"date_added"`
Tags []string `json:"tags"`
SelfReportedCirculatingSupply float64 `json:"self_reported_circulating_supply,omitempty"`
SelfReportedMarketCap float64 `json:"self_reported_market_cap,omitempty"`
Platform Platform `json:"platform"`
Quote map[string]Quote `json:"quote"`
}
type Quote struct {
Price float64 `json:"price"`
Volume24H float64 `json:"volume_24h"`
@ -30,25 +44,6 @@ type Quote struct {
LastUpdated time.Time `json:"last_updated"`
}
type Data struct {
ID int64 `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Slug string `json:"slug"`
CmcRank int64 `json:"cmc_rank,omitempty"`
NumMarketPairs int64 `json:"num_market_pairs"`
CirculatingSupply float64 `json:"circulating_supply"`
TotalSupply float64 `json:"total_supply"`
MaxSupply float64 `json:"max_supply"`
LastUpdated time.Time `json:"last_updated"`
DateAdded time.Time `json:"date_added"`
Tags []string `json:"tags"`
Platform Platform `json:"platform"`
SelfReportedCirculatingSupply interface{} `json:"self_reported_circulating_supply,omitempty"`
SelfReportedMarketCap interface{} `json:"self_reported_market_cap,omitempty"`
Quote map[string]Quote `json:"quote"`
}
type Status struct {
Timestamp time.Time `json:"timestamp"`
ErrorCode int `json:"error_code"`