dinero: github.com/mattevans/dinero Index | Files

package dinero

import "github.com/mattevans/dinero"

Index

Package Files

cache.go currencies.go dinero.go rates.go

func CheckResponse Uses

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors. A response is considered an error if it has a status code outside the 200 range. API error responses map to ErrorResponse.

type CacheService Uses

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

CacheService handles in-memory caching of our rates.

func NewCacheService Uses

func NewCacheService(
    client *Client,
    store *cache.Cache,
) *CacheService

NewCacheService creates a new handler for this service.

func (*CacheService) Expire Uses

func (s *CacheService) Expire(base string)

Expire will expire the cache for a given base currency.

func (*CacheService) Get Uses

func (s *CacheService) Get(base string) (*RateResposne, bool)

Get will return our in-memory stored currency/rates.

func (*CacheService) IsExpired Uses

func (s *CacheService) IsExpired(base string) bool

IsExpired checks whether or not rate stored is expired.

func (*CacheService) Store Uses

func (s *CacheService) Store(rsp *RateResposne)

Store will store our currency/rates in-memory.

type Client Uses

type Client struct {

    // AppID is the Open Exchange Rates application ID.
    AppID string
    // UserAgent is the UA for this package that all requests will use.
    UserAgent string
    // BackendURL is the base API endpoint at OXR.
    BackendURL *url.URL

    // Services used for communicating with the API.
    Rates      *RatesService
    Currencies *CurrenciesService
    Cache      *CacheService
    // contains filtered or unexported fields
}

Client holds a connection to the OXR API.

func NewClient Uses

func NewClient(appID, baseCurrency string, expiry time.Duration) *Client

NewClient creates a new Client with the appropriate connection details and services used for communicating with the API.

func (*Client) Do Uses

func (c *Client) Do(req *http.Request, v interface{}) (*Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in 'v', or returned as an error if an API (if found).

func (*Client) NewRequest Uses

func (c *Client) NewRequest(method, urlPath string, body interface{}) (*http.Request, error)

NewRequest creates an authenticated API request. A relative URL can be provided in urlPath, which will be resolved to the BackendURL of the Client.

func (*Client) NewUnauthedRequest Uses

func (c *Client) NewUnauthedRequest(method, urlPath string, body interface{}) (*http.Request, error)

NewUnauthedRequest creates an unauthenticated API request. A relative URL can be provided in urlPath, which will be resolved to the BackendURL of the Client.

type CurrenciesService Uses

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

CurrenciesService handles currency request/responses.

func NewCurrenciesService Uses

func NewCurrenciesService(
    client *Client,
) *CurrenciesService

NewCurrenciesService creates a new handler for this service.

func (*CurrenciesService) List Uses

func (s *CurrenciesService) List() ([]*CurrencyResponse, error)

List will fetch all list of all currencies available via the OXR api.

type CurrencyResponse Uses

type CurrencyResponse struct {
    Code string `json:"code"`
    Name string `json:"name"`
}

CurrencyResponse represents a currency from OXR.

type ErrorResponse Uses

type ErrorResponse struct {
    *http.Response
    ErrorCode   int64  `json:"status"`
    Message     string `json:"message"`
    Description string `json:"description"`
}

An ErrorResponse reports the error caused by an API request

func (*ErrorResponse) Error Uses

func (r *ErrorResponse) Error() string

type RateResposne Uses

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

RateResposne holds our forex rates for a given base currency

type RatesService Uses

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

RatesService handles rate request/responses.

func NewRatesService Uses

func NewRatesService(
    client *Client,
    baseCurrency string,
) *RatesService

NewRatesService creates a new handler for this service.

func (*RatesService) Get Uses

func (s *RatesService) Get(code string) (*float64, error)

Get will fetch a single rate for a given currency either from the the store or the OXR api.

func (*RatesService) GetBaseCurrency Uses

func (s *RatesService) GetBaseCurrency() string

GetBaseCurrency will return the baseCurrency.

func (*RatesService) List Uses

func (s *RatesService) List() (*RateResposne, error)

List will fetch all the latest rates for the base currency either from the the store or the OXR api.

func (*RatesService) SetBaseCurrency Uses

func (s *RatesService) SetBaseCurrency(base string)

SetBaseCurrency will set the base currency to be used for requests.

func (*RatesService) Update Uses

func (s *RatesService) Update(base string) error

Update will update the rates for the given currency from OXR.

type Response Uses

type Response struct {
    *http.Response
    ErrorCode int64
    Message   string
}

Response is a OXR response. This wraps the standard http.Response returned from the OXR API.

Package dinero imports 10 packages (graph). Updated 2019-06-16. Refresh now. Tools for package owners.