cbrcur

package
v0.0.0-...-ca9bfaf Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2018 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidConfiguration = errors.New("invalid configuration")
)

Functions

This section is empty.

Types

type Client

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

func New

func New(options ...Option) (*Client, error)
Example
package main

import (
	"github.com/kazhuravlev/go-cbrcur/cbrcur"
	"net/http"
)

func main() {
	_, _ = cbrcur.New(
		cbrcur.WithHttpClient(http.DefaultClient),
	)
}
Output:

func (*Client) GetCurrencies

func (c *Client) GetCurrencies(ctx context.Context) ([]Currency, error)
Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"github.com/kazhuravlev/go-cbrcur/cbrcur"
	"io/ioutil"
	"net/http"
	"time"
)

type roundTripper struct {
	status  int
	headers http.Header
	body    []byte
}

func (rt roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	return &http.Response{
		StatusCode: rt.status,
		Body:       ioutil.NopCloser(bytes.NewBuffer(rt.body)),
		Header:     rt.headers,
	}, nil
}

func mockClientResponse(status int, headers http.Header, body []byte) *http.Client {
	rt := roundTripper{
		status:  status,
		headers: headers,
		body:    body,
	}

	return &http.Client{Transport: rt}
}

var (
	bodyCurrencies, _    = ioutil.ReadFile("./testdata/currencies.xml")
	mockedCurrencyClient = mockClientResponse(200, make(http.Header), bodyCurrencies)

	bodyRates, _ = ioutil.ReadFile("./testdata/rate_report.xml")
)

func main() {
	client, _ := cbrcur.New(cbrcur.WithHttpClient(mockedCurrencyClient))

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	currencies, _ := client.GetCurrencies(ctx)
	fmt.Println(currencies[:1])
}
Output:

[{R01010 Австралийский доллар Australian Dollar 1 R01010     36 AUD}]

func (*Client) GetRatesReport

func (c *Client) GetRatesReport(ctx context.Context, date *time.Time) (*Report, error)
Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"github.com/kazhuravlev/go-cbrcur/cbrcur"
	"io/ioutil"
	"net/http"
	"time"
)

type roundTripper struct {
	status  int
	headers http.Header
	body    []byte
}

func (rt roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	return &http.Response{
		StatusCode: rt.status,
		Body:       ioutil.NopCloser(bytes.NewBuffer(rt.body)),
		Header:     rt.headers,
	}, nil
}

func mockClientResponse(status int, headers http.Header, body []byte) *http.Client {
	rt := roundTripper{
		status:  status,
		headers: headers,
		body:    body,
	}

	return &http.Client{Transport: rt}
}

var (
	bodyCurrencies, _ = ioutil.ReadFile("./testdata/currencies.xml")

	bodyRates, _      = ioutil.ReadFile("./testdata/rate_report.xml")
	mockedRatesClient = mockClientResponse(200, make(http.Header), bodyRates)
)

func main() {
	client, _ := cbrcur.New(cbrcur.WithHttpClient(mockedRatesClient))

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	date := time.Date(2015, 8, 22, 0, 0, 0, 0, time.UTC)
	rates, _ := client.GetRatesReport(ctx, &date)
	fmt.Println(rates.Rates[:1])
}
Output:

[{R01010 36 AUD 1 Австралийский доллар 49.9059}]

type Currency

type Currency struct {
	ID          string `xml:"ID,attr"`
	Name        string `xml:"Name"`
	EngName     string `xml:"EngName"`
	Nominal     int    `xml:"Nominal"`
	ParentCode  string `xml:"ParentCode"`
	ISONumCode  int    `xml:"ISO_Num_Code"`
	ISOCharCode string `xml:"ISO_Char_Code"`
}

type Option

type Option func(*Client) error

func WithHttpClient

func WithHttpClient(client *http.Client) Option

type Rate

type Rate struct {
	ID       string      `xml:"ID,attr"`
	NumCode  int         `xml:"NumCode"`
	CharCode string      `xml:"CharCode"`
	Nominal  int         `xml:"Nominal"`
	Name     string      `xml:"Name"`
	Value    customFloat `xml:"Value"`
}

type Report

type Report struct {
	Rates []Rate
	Date  time.Time
}

Jump to

Keyboard shortcuts

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