exchangerate

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package exchangerate is the library behind the exchangerate command line: the HTTP client, request shaping, and the typed data models for the ExchangeRate-API open tier at open.er-api.com.

No API key required. The client sets a real User-Agent, paces requests, and retries transient failures (429 and 5xx) with exponential backoff.

Index

Constants

View Source
const Host = "open.er-api.com"

Host is the site this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client talks to open.er-api.com over HTTP.

func NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client configured with cfg.

func (*Client) Convert

func (c *Client) Convert(ctx context.Context, from, to string, amount float64) (*Conversion, error)

Convert fetches the rate from base to target and multiplies by amount.

func (*Client) Latest

func (c *Client) Latest(ctx context.Context, base string, limit int) ([]Rate, error)

Latest fetches the current exchange rates for the given base currency. base defaults to "USD" if empty. Rates are sorted alphabetically by currency code; if limit > 0 only the first limit entries are returned.

type Config

type Config struct {
	BaseURL   string
	UserAgent string
	Rate      time.Duration
	Timeout   time.Duration
	Retries   int
}

Config holds all tunable parameters for the Client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Conversion

type Conversion struct {
	From      string  `kit:"id" json:"from"`
	To        string  `json:"to"`
	Amount    float64 `json:"amount"`
	Result    float64 `json:"result"`
	Rate      float64 `json:"rate"`
	UpdatedAt string  `json:"updated_at"` // time_last_update_utc verbatim
}

Conversion is the result of converting an amount from one currency to another.

type Domain

type Domain struct{}

Domain is the exchangerate driver.

func (Domain) Classify

func (Domain) Classify(input string) (uriType, id string, err error)

Classify turns an input into the canonical (type, id).

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for the binary's help and version.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate returns the live https URL for a (type, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type Rate

type Rate struct {
	Rank     int     `json:"rank"`
	Currency string  `kit:"id" json:"currency"` // ISO 4217 code, e.g. "EUR"
	Rate     float64 `json:"rate"`              // units of Currency per 1 unit of base
	Base     string  `json:"base"`              // the base currency, e.g. "USD"
}

Rate is one currency's exchange rate against a base currency.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL