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 ¶
Example ¶
package main import ( "github.com/kazhuravlev/go-cbrcur/cbrcur" "net/http" ) func main() { _, _ = cbrcur.New( cbrcur.WithHttpClient(http.DefaultClient), ) }
Output:
func (*Client) GetCurrencies ¶
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 ¶
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 Option ¶
func WithHttpClient ¶
Click to show internal directories.
Click to hide internal directories.