moonmath/coindesk/requests.go

49 lines
1.2 KiB
Go
Raw Normal View History

2024-03-17 09:10:05 +00:00
package coindesk
import (
"context"
"fmt"
"strings"
"time"
"github.com/carlmjohnson/requests"
)
// GetPriceValues gets timestamped prices for a particular asset.
func GetPriceValues(
ctx context.Context, asset Asset, startDate, endDate time.Time,
) (resp Response[PriceValues], err error) {
const basePath = "v2/tb/price/values"
const timeFormat = "2006-01-02T15:04"
err = requests.New(commonConfig).
Path(fmt.Sprintf("%s/%s", basePath, asset)).
Param("start_date", startDate.Format(timeFormat)).
Param("end_date", endDate.Format(timeFormat)).
ToJSON(&resp).
Fetch(ctx)
return
}
// GetAssetTickers gets tickers for a set of assets.
func GetAssetTickers(ctx context.Context, assets ...Asset) (resp Response[AssetTickers], err error) {
const basePath = "v2/tb/price/ticker"
var strAssets []string
for _, asset := range assets {
strAssets = append(strAssets, string(asset))
}
err = requests.New(commonConfig).
Path(basePath).
Param("assets", strings.Join(strAssets, ",")).
ToJSON(&resp).
Fetch(ctx)
return
}
const baseUrl = "https://production.api.coindesk.com"
func commonConfig(rb *requests.Builder) {
rb.
BaseURL(baseUrl).
Accept("application/json;charset=utf-8")
}