fixer

package module
v0.0.0-...-041235e Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2020 License: MIT Imports: 10 Imported by: 0

README

fixer

Build Status Go Report Card GoDoc License MIT

Go client for Fixer.io (Foreign exchange rates and currency conversion API)

You need to register for a free access key if using the default Fixer API.

The default client loads its access key from the environment variable FIXER_ACCESS_KEY

Installation

go get -u github.com/peterhellberg/fixer

Usage examples

SEK quoted against USD and EUR

package main

import (
	"context"
	"encoding/json"
	"os"

	"github.com/peterhellberg/fixer"
)

func main() {
	resp, err := fixer.Latest(context.Background(),
		fixer.Base(fixer.SEK),
		fixer.Symbols(
			fixer.USD,
			fixer.EUR,
		),
	)
	if err != nil {
		return
	}

	encode(resp)
}

func encode(v interface{}) {
	enc := json.NewEncoder(os.Stdout)
	enc.SetEscapeHTML(false)
	enc.SetIndent("", " ")
	enc.Encode(v)
}
{
 "base": "SEK",
 "date": "2017-05-24T00:00:00Z",
 "rates": {
  "EUR": 0.10265,
  "USD": 0.1149
 },
 "links": {
  "base": "https://api.fixer.io",
  "self": "https://api.fixer.io/latest?base=SEK&symbols=EUR%2CUSD"
 }
}

Using the Foreign exchange rates API instead

package main

import (
	"context"
	"encoding/json"
	"os"
	"time"

	"github.com/peterhellberg/fixer"
)

func main() {
	f := fixer.ExratesClient

	resp, err := f.At(context.Background(), time.Now(),
		fixer.Base(fixer.GBP),
		fixer.Symbols(
			fixer.SEK,
			fixer.NOK,
		),
	)
	if err != nil {
		return
	}

	encode(resp)
}

func encode(v interface{}) {
	enc := json.NewEncoder(os.Stdout)
	enc.SetEscapeHTML(false)
	enc.SetIndent("", " ")
	enc.Encode(v)
}
{
 "base": "GBP",
 "date": "2020-04-17T00:00:00Z",
 "rates": {
  "NOK": 12.9739704293,
  "SEK": 12.4799374554
 },
 "links": {
  "base": "https://api.exchangeratesapi.io",
  "self": "https://api.exchangeratesapi.io/2020-04-18?base=GBP&symbols=NOK%2CSEK"
 }
}

API documentation

https://fixer.io/documentation

License (MIT)

Copyright (c) 2017-2020 Peter Hellberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package fixer contains a client for the Foreign exchange rates and currency conversion API

Installation

go get -u github.com/peterhellberg/fixer

Usage

A small usage example

package main

import (
	"context"
	"flag"
	"fmt"

	"github.com/peterhellberg/fixer"
)

func main() {
	f := flag.String("from", "EUR", "")
	t := flag.String("to", "SEK", "")
	n := flag.Float64("n", 1, "")

	flag.Parse()

	from, to := fixer.Currency(*f), fixer.Currency(*t)

	resp, err := fixer.Latest(context.Background(),
		fixer.Base(from), fixer.Symbols(to),
	)

	if err == nil {
		fmt.Printf("%.2f %s equals %.2f %s\n", *n, from, resp.Rates[to]**n, to)
	}
}

API Documentation

http://fixer.io/

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilResponse         = NewError("Unexpected nil response")
	ErrUnexpectedStatus    = NewError("Unexpected status")
	ErrNotFound            = NewError(http.StatusText(http.StatusNotFound))
	ErrUnprocessableEntity = NewError(http.StatusText(http.StatusUnprocessableEntity))
)

Errors

View Source
var DefaultClient = FixerClient

DefaultClient is the default client for the Foreign exchange rates and currency conversion API

View Source
var ExratesClient = NewClient(BaseURL("https://api.exchangeratesapi.io"))

ExratesClient is a client configured to use https://api.exchangeratesapi.io

View Source
var FixerClient = NewClient(AccessKey(os.Getenv("FIXER_ACCESS_KEY")))

FixerClient is a client configured to use https://api.fixer.io

Functions

func AccessKey

func AccessKey(ak string) func(*Client)

AccessKey sets the access key used by the client

func Base

func Base(c Currency) url.Values

Base sets the base query variable based on a Currency

func BaseURL

func BaseURL(rawurl string) func(*Client)

BaseURL changes the base URL to the provided rawurl

func HTTPClient

func HTTPClient(hc *http.Client) func(*Client)

HTTPClient changes the HTTP client to the provided *http.Client

func Symbols

func Symbols(cs ...Currency) url.Values

Symbols sets the symbols query variable based on the provided currencies

func UserAgent

func UserAgent(ua string) func(*Client)

UserAgent changes the User-Agent used by the client

Types

type Client

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

Client for the Foreign exchange rates and currency conversion API

func NewClient

func NewClient(options ...func(*Client)) *Client

NewClient creates a Client

func (*Client) At

func (c *Client) At(ctx context.Context, t time.Time, attributes ...url.Values) (*Response, error)

At returns historical rates for any day since 1999

func (*Client) Latest

func (c *Client) Latest(ctx context.Context, attributes ...url.Values) (*Response, error)

Latest foreign exchange reference rates

type Currencies

type Currencies []Currency

Currencies is a slice of Currency

func (Currencies) String

func (cs Currencies) String() string

type Currency

type Currency string

Currency is the type used for ISO 4217 Currency codes

const (
	AED Currency = "AED"
	AFN Currency = "AFN"
	ALL Currency = "ALL"
	AMD Currency = "AMD"
	ANG Currency = "ANG"
	AOA Currency = "AOA"
	ARS Currency = "ARS"
	AUD Currency = "AUD"
	AWG Currency = "AWG"
	AZN Currency = "AZN"
	BAM Currency = "BAM"
	BBD Currency = "BBD"
	BDT Currency = "BDT"
	BGN Currency = "BGN"
	BHD Currency = "BHD"
	BIF Currency = "BIF"
	BMD Currency = "BMD"
	BND Currency = "BND"
	BOB Currency = "BOB"
	BRL Currency = "BRL"
	BSD Currency = "BSD"
	BTC Currency = "BTC"
	BTN Currency = "BTN"
	BWP Currency = "BWP"
	BYN Currency = "BYN"
	BYR Currency = "BYR"
	BZD Currency = "BZD"
	CAD Currency = "CAD"
	CDF Currency = "CDF"
	CHF Currency = "CHF"
	CLF Currency = "CLF"
	CLP Currency = "CLP"
	CNY Currency = "CNY"
	COP Currency = "COP"
	CRC Currency = "CRC"
	CUC Currency = "CUC"
	CUP Currency = "CUP"
	CVE Currency = "CVE"
	CZK Currency = "CZK"
	DJF Currency = "DJF"
	DKK Currency = "DKK"
	DOP Currency = "DOP"
	DZD Currency = "DZD"
	EGP Currency = "EGP"
	ERN Currency = "ERN"
	ETB Currency = "ETB"
	EUR Currency = "EUR"
	FJD Currency = "FJD"
	FKP Currency = "FKP"
	GBP Currency = "GBP"
	GEL Currency = "GEL"
	GGP Currency = "GGP"
	GHS Currency = "GHS"
	GIP Currency = "GIP"
	GMD Currency = "GMD"
	GNF Currency = "GNF"
	GTQ Currency = "GTQ"
	GYD Currency = "GYD"
	HKD Currency = "HKD"
	HNL Currency = "HNL"
	HRK Currency = "HRK"
	HTG Currency = "HTG"
	HUF Currency = "HUF"
	IDR Currency = "IDR"
	ILS Currency = "ILS"
	IMP Currency = "IMP"
	INR Currency = "INR"
	IQD Currency = "IQD"
	IRR Currency = "IRR"
	ISK Currency = "ISK"
	JEP Currency = "JEP"
	JMD Currency = "JMD"
	JOD Currency = "JOD"
	JPY Currency = "JPY"
	KES Currency = "KES"
	KGS Currency = "KGS"
	KHR Currency = "KHR"
	KMF Currency = "KMF"
	KPW Currency = "KPW"
	KRW Currency = "KRW"
	KWD Currency = "KWD"
	KYD Currency = "KYD"
	KZT Currency = "KZT"
	LAK Currency = "LAK"
	LBP Currency = "LBP"
	LKR Currency = "LKR"
	LRD Currency = "LRD"
	LSL Currency = "LSL"
	LTL Currency = "LTL"
	LVL Currency = "LVL"
	LYD Currency = "LYD"
	MAD Currency = "MAD"
	MDL Currency = "MDL"
	MGA Currency = "MGA"
	MKD Currency = "MKD"
	MMK Currency = "MMK"
	MNT Currency = "MNT"
	MOP Currency = "MOP"
	MRO Currency = "MRO"
	MUR Currency = "MUR"
	MVR Currency = "MVR"
	MWK Currency = "MWK"
	MXN Currency = "MXN"
	MYR Currency = "MYR"
	MZN Currency = "MZN"
	NAD Currency = "NAD"
	NGN Currency = "NGN"
	NIO Currency = "NIO"
	NOK Currency = "NOK"
	NPR Currency = "NPR"
	NZD Currency = "NZD"
	OMR Currency = "OMR"
	PAB Currency = "PAB"
	PEN Currency = "PEN"
	PGK Currency = "PGK"
	PHP Currency = "PHP"
	PKR Currency = "PKR"
	PLN Currency = "PLN"
	PYG Currency = "PYG"
	QAR Currency = "QAR"
	RON Currency = "RON"
	RSD Currency = "RSD"
	RUB Currency = "RUB"
	RWF Currency = "RWF"
	SAR Currency = "SAR"
	SBD Currency = "SBD"
	SCR Currency = "SCR"
	SDG Currency = "SDG"
	SEK Currency = "SEK"
	SGD Currency = "SGD"
	SHP Currency = "SHP"
	SLL Currency = "SLL"
	SOS Currency = "SOS"
	SRD Currency = "SRD"
	STD Currency = "STD"
	SVC Currency = "SVC"
	SYP Currency = "SYP"
	SZL Currency = "SZL"
	THB Currency = "THB"
	TJS Currency = "TJS"
	TMT Currency = "TMT"
	TND Currency = "TND"
	TOP Currency = "TOP"
	TRY Currency = "TRY"
	TTD Currency = "TTD"
	TWD Currency = "TWD"
	TZS Currency = "TZS"
	UAH Currency = "UAH"
	UGX Currency = "UGX"
	USD Currency = "USD"
	UYU Currency = "UYU"
	UZS Currency = "UZS"
	VEF Currency = "VEF"
	VND Currency = "VND"
	VUV Currency = "VUV"
	WST Currency = "WST"
	XAF Currency = "XAF"
	XAG Currency = "XAG"
	XAU Currency = "XAU"
	XCD Currency = "XCD"
	XDR Currency = "XDR"
	XOF Currency = "XOF"
	XPF Currency = "XPF"
	YER Currency = "YER"
	ZAR Currency = "ZAR"
	ZMK Currency = "ZMK"
	ZMW Currency = "ZMW"
	ZWL Currency = "ZWL"
)

Currency codes published by the European Central Bank

type Date

type Date struct {
	time.Time
}

Date wraps time.Time

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) error

UnmarshalJSON parses dates in YYYY-MM-DD format

type Error

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

Error type for Fixer API requests

func NewError

func NewError(msg string) *Error

NewError creates a new Error

func (*Error) Error

func (e *Error) Error() string

Error message

type Links map[string]string

Links is a links object related to the primary data of the Response

type Rates

type Rates map[Currency]float64

Rates is the list of rates quoted against the base (EUR by default)

type Response

type Response struct {
	Base  Currency `json:"base"`
	Date  Date     `json:"date"`
	Rates Rates    `json:"rates"`
	Links Links    `json:"links,omitempty"`
}

Response data from the Foreign exchange rates and currency conversion API

func At

func At(ctx context.Context, t time.Time, attributes ...url.Values) (*Response, error)

At returns historical rates for any day since 1999 using the DefaultClient

func Latest

func Latest(ctx context.Context, attributes ...url.Values) (*Response, error)

Latest foreign exchange reference rates using the DefaultClient

Jump to

Keyboard shortcuts

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