Documentation
¶
Overview ¶
Package cryptoprice provides a simple Client for querying the most accurate crypto currency price available from the CryptoCompare REST API.
This is not intended to be a complete implementation of the full CryptoCompare REST API. It just accesses the CryptoCompare REST API endpoints required to get the most accurate price available for a given time.
The CryptoCompare REST API documentation can be found here: https://min-api.cryptocompare.com/
Index ¶
Examples ¶
Constants ¶
const ( CryptoCompareURL = "https://min-api.cryptocompare.com/data" NowURI = "/price" MinuteURI = "/histominute" HourURI = "/histohour" ExtraParams = "golang pkg - github.com/canonical-ledgers/cryptoprice" )
Default URIs and parameters
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
URL string // URL to send requests to
FromSymbol string // Cryptocurrency symbol of interest
ToSymbol string // Currency symbol to convert into
TryConversion bool // Set to false to only return data if a direct pair is available
Exchange string // Exchange to use for data (default: "CCCAGG" aggregated average)
ExtraParams string // Name of your application, defaults to const ExtraParams
http.Client
}
Client stores request parameters and provides methods for querying the CryptoCompare REST API. You may set any additional http.Client settings directly on this type.
func NewClient ¶
NewClient returns a pointer to a new Client with the given FromSymbol and ToSymbol set, as well as the correct CryptoCompare API endpoint URL, TryConversion set to true, and the default ExtraParams set.
func (Client) GetPrice ¶
GetPrice returns the most accurate price available for the given time t. If the requested time is within the past minute, the most recent price data is used. If the requested time is within the past 7 days, the simple average of the high and low prices for the minute that is closest to the given time is used. If the request time is any further in the past, the simple average of the high and low prices for the hour that is closest to the given price is used.
Example ¶
package main
import (
"fmt"
"time"
"github.com/canonical-ledgers/cryptoprice"
)
func main() {
client := cryptoprice.NewClient("BTC", "USD")
client.Timeout = 5 * time.Second
p, err := client.GetPrice(time.Now())
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Printf("Latest BTC price: $%v USD\n", p)
}
Output: