currency

package
v0.58.2 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Eur = "EUR"
	Usd = "USD"
)
View Source
const (
	ECBProviderName              = "ecb"
	ExchangeRateECBUrl           = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
	HistoricalExchangeRateECBUrl = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml"
)
View Source
const (
	OpenExchangeRatesApiProviderName = "openexchangeratesapi"
	OpenExchangeRatesUrl             = "https://openexchangerates.org/api/"
)
View Source
const (
	ExchangeRateRefresh           = 8 * time.Hour
	ExchangeRateDateKey           = "currency_exchange_last_refresh"
	HistoricalExchangeRateDateKey = "currency_exchange_historical_last_refresh"
)
View Source
const RatesAmount = 200

RatesAmount We expect to have around 200 currencies in total. It's okay to exceed the limit, but it's nice to avoid unnecessary reallocations.

Variables

View Source
var Currencies = map[string]string{
	Eur: "€",
	Usd: "$",
}

Functions

func NewCurrencyModule

func NewCurrencyModule() kernel.ModuleFactory

NewCurrencyModule returns a kernel module factory for the currency module. The module will start a background process that periodically updates exchange rates from configured providers. By default, it will update historical exchange rates for the last 30 days. If you need to create a new table with all the data, it is recommended to copy data from an existing table.

func RegisterProvider added in v0.49.2

func RegisterProvider(name string, option ProviderFactory)

RegisterProvider registers a new currency provider with the given name and factory. If a provider with the same name already exists, it will be overwritten. Providers should be registered before currency module is initialized (e.g. in init function).

Types

type Body

type Body struct {
	Content Content `xml:"Cube"`
}

type Content

type Content struct {
	Time  string `xml:"time,attr"`
	Rates []Rate `xml:"Cube"`
}

func (Content) GetTime

func (c Content) GetTime() (time.Time, error)

type ECBProviderHistoricalExchangeResponse added in v0.49.2

type ECBProviderHistoricalExchangeResponse struct {
	Subject string         `xml:"subject"`
	Sender  Sender         `xml:"Sender"`
	Body    HistoricalBody `xml:"Cube"`
}

type ExchangeResponse

type ExchangeResponse struct {
	Subject string `xml:"subject"`
	Sender  Sender `xml:"Sender"`
	Body    Body   `xml:"Cube"`
}

type HistoricalBody

type HistoricalBody struct {
	Content []Content `xml:"Cube"`
}

type Module

type Module struct {
	kernel.EssentialBackgroundModule
	kernel.ServiceStage
	// contains filtered or unexported fields
}

func (Module) IsHealthy added in v0.23.0

func (module Module) IsHealthy(ctx context.Context) (bool, error)

func (Module) Run

func (module Module) Run(ctx context.Context) error

type OpenExchangeRatesApiResponse added in v0.49.2

type OpenExchangeRatesApiResponse struct {
	Timestamp int64              `json:"timestamp"`
	Base      string             `json:"base"`
	Rates     map[string]float64 `json:"rates"`
}

type Provider added in v0.49.2

type Provider interface {
	Name() string
	GetPriority() int
	FetchLatestRates(ctx context.Context) ([]Rate, error)
	FetchHistoricalRates(ctx context.Context, dates []time.Time) (map[time.Time][]Rate, error)
}

func NewECBProviderWithInterfaces added in v0.49.2

func NewECBProviderWithInterfaces(logger log.Logger, http http.Client) Provider

func NewOpenExchangeRatesApiProviderWithInterfaces added in v0.49.2

func NewOpenExchangeRatesApiProviderWithInterfaces(logger log.Logger, http http.Client, settings ProviderSettings) Provider

type ProviderFactory added in v0.49.2

type ProviderFactory func(ctx context.Context, logger log.Logger, http http.Client, settings ProviderSettings) Provider

type ProviderSettings added in v0.49.2

type ProviderSettings struct {
	ApiKey string `cfg:"api_key"`
	// Providers with lower values will be used first. The order of the providers with the same priority is non-deterministic.
	Priority uint `cfg:"priority"`
	Enabled  bool `cfg:"enabled"`
}

type ProvidersConfig added in v0.49.2

type ProvidersConfig map[string]ProviderSettings

type Rate

type Rate struct {
	Currency string  `xml:"currency,attr"`
	Rate     float64 `xml:"rate,attr"`
}

type Sender

type Sender struct {
	Name string `xml:"name"`
}

type Service

type Service interface {
	HasCurrency(ctx context.Context, currency string) (bool, error)
	HasCurrencyAtDate(ctx context.Context, currency string, date time.Time) (bool, error)
	ToEur(ctx context.Context, value float64, fromCurrency string) (float64, error)
	ToEurAtDate(ctx context.Context, value float64, fromCurrency string, date time.Time) (float64, error)
	ToUsd(ctx context.Context, value float64, fromCurrency string) (float64, error)
	ToUsdAtDate(ctx context.Context, value float64, fromCurrency string, date time.Time) (float64, error)
	ToCurrency(ctx context.Context, toCurrency string, value float64, fromCurrency string) (float64, error)
	ToCurrencyAtDate(ctx context.Context, toCurrency string, value float64, fromCurrency string, date time.Time) (float64, error)
}

func New

func New(ctx context.Context, config cfg.Config, logger log.Logger) (Service, error)

func NewWithInterfaces

func NewWithInterfaces(store kvstore.KvStore[float64], clock clock.Clock) Service

type Settings added in v0.37.0

type Settings struct {
	StartDate time.Time `cfg:"start_date"`
}

type UpdaterService

type UpdaterService interface {
	EnsureRecentExchangeRates(ctx context.Context) error
	EnsureHistoricalExchangeRates(ctx context.Context) error
}

func NewUpdater

func NewUpdater(ctx context.Context, config cfg.Config, logger log.Logger) (UpdaterService, error)

func NewUpdaterWithInterfaces

func NewUpdaterWithInterfaces(logger log.Logger, store kvstore.KvStore[float64], httpClient http.Client, clock clock.Clock, settings *Settings, providers []Provider) UpdaterService

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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