version bump + CBR fx ingestor
This commit is contained in:
537
api/fx/ingestor/internal/market/cbr/connector.go
Normal file
537
api/fx/ingestor/internal/market/cbr/connector.go
Normal file
@@ -0,0 +1,537 @@
|
||||
package cbr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/fx/ingestor/internal/market/common"
|
||||
mmodel "github.com/tech/sendico/fx/ingestor/internal/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/net/html/charset"
|
||||
)
|
||||
|
||||
type cbrConnector struct {
|
||||
id mmodel.Driver
|
||||
provider string
|
||||
client *http.Client
|
||||
base string
|
||||
dailyPath string
|
||||
directoryPath string
|
||||
dynamicPath string
|
||||
logger mlogger.Logger
|
||||
|
||||
byISO map[string]valuteInfo
|
||||
byID map[string]valuteInfo
|
||||
}
|
||||
|
||||
const defaultCBRBaseURL = "https://www.cbr.ru"
|
||||
const (
|
||||
defaultDirectoryPath = "/scripts/XML_valFull.asp"
|
||||
defaultDailyPath = "/scripts/XML_daily.asp"
|
||||
defaultDynamicPath = "/scripts/XML_dynamic.asp"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultDialTimeoutSeconds = 5 * time.Second
|
||||
defaultDialKeepAliveSeconds = 30 * time.Second
|
||||
defaultTLSHandshakeTimeoutSeconds = 5 * time.Second
|
||||
defaultResponseHeaderTimeoutSeconds = 10 * time.Second
|
||||
defaultRequestTimeoutSeconds = 10 * time.Second
|
||||
)
|
||||
|
||||
func NewConnector(logger mlogger.Logger, settings model.SettingsT) (mmodel.Connector, error) {
|
||||
baseURL := defaultCBRBaseURL
|
||||
provider := strings.ToLower(mmodel.DriverCBR.String())
|
||||
dialTimeout := defaultDialTimeoutSeconds
|
||||
dialKeepAlive := defaultDialKeepAliveSeconds
|
||||
tlsHandshakeTimeout := defaultTLSHandshakeTimeoutSeconds
|
||||
responseHeaderTimeout := defaultResponseHeaderTimeoutSeconds
|
||||
requestTimeout := defaultRequestTimeoutSeconds
|
||||
directoryPath := defaultDirectoryPath
|
||||
dailyPath := defaultDailyPath
|
||||
dynamicPath := defaultDynamicPath
|
||||
|
||||
if settings != nil {
|
||||
if value, ok := settings["base_url"].(string); ok && strings.TrimSpace(value) != "" {
|
||||
baseURL = strings.TrimSpace(value)
|
||||
}
|
||||
if value, ok := settings["provider"].(string); ok && strings.TrimSpace(value) != "" {
|
||||
provider = strings.TrimSpace(value)
|
||||
}
|
||||
if value, ok := settings["directory_path"].(string); ok && strings.TrimSpace(value) != "" {
|
||||
directoryPath = strings.TrimSpace(value)
|
||||
}
|
||||
if value, ok := settings["daily_path"].(string); ok && strings.TrimSpace(value) != "" {
|
||||
dailyPath = strings.TrimSpace(value)
|
||||
}
|
||||
if value, ok := settings["dynamic_path"].(string); ok && strings.TrimSpace(value) != "" {
|
||||
dynamicPath = strings.TrimSpace(value)
|
||||
}
|
||||
dialTimeout = common.DurationSetting(settings, "dial_timeout_seconds", dialTimeout)
|
||||
dialKeepAlive = common.DurationSetting(settings, "dial_keep_alive_seconds", dialKeepAlive)
|
||||
tlsHandshakeTimeout = common.DurationSetting(settings, "tls_handshake_timeout_seconds", tlsHandshakeTimeout)
|
||||
responseHeaderTimeout = common.DurationSetting(settings, "response_header_timeout_seconds", responseHeaderTimeout)
|
||||
requestTimeout = common.DurationSetting(settings, "request_timeout_seconds", requestTimeout)
|
||||
}
|
||||
|
||||
parsed, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return nil, merrors.InvalidArgumentWrap(err, "cbr: invalid base url", "base_url")
|
||||
}
|
||||
|
||||
var transport http.RoundTripper = &http.Transport{
|
||||
DialContext: (&net.Dialer{Timeout: dialTimeout, KeepAlive: dialKeepAlive}).DialContext,
|
||||
TLSHandshakeTimeout: tlsHandshakeTimeout,
|
||||
ResponseHeaderTimeout: responseHeaderTimeout,
|
||||
}
|
||||
|
||||
if customTransport, ok := settings["http_round_tripper"].(http.RoundTripper); ok && customTransport != nil {
|
||||
transport = customTransport
|
||||
}
|
||||
|
||||
connector := &cbrConnector{
|
||||
id: mmodel.DriverCBR,
|
||||
provider: provider,
|
||||
client: &http.Client{
|
||||
Timeout: requestTimeout,
|
||||
Transport: transport,
|
||||
},
|
||||
base: strings.TrimRight(parsed.String(), "/"),
|
||||
dailyPath: dailyPath,
|
||||
directoryPath: directoryPath,
|
||||
dynamicPath: dynamicPath,
|
||||
logger: logger.Named("cbr"),
|
||||
}
|
||||
|
||||
if err := connector.refreshDirectory(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return connector, nil
|
||||
}
|
||||
|
||||
func (c *cbrConnector) ID() mmodel.Driver {
|
||||
return c.id
|
||||
}
|
||||
|
||||
func (c *cbrConnector) FetchTicker(ctx context.Context, symbol string) (*mmodel.Ticker, error) {
|
||||
isoCode, asOfDate, err := parseSymbol(symbol)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
valute, ok := c.byISO[isoCode]
|
||||
if !ok {
|
||||
return nil, merrors.InvalidArgument("cbr: unknown currency "+isoCode, "symbol")
|
||||
}
|
||||
|
||||
var price string
|
||||
if asOfDate != nil {
|
||||
price, err = c.fetchHistoricalRate(ctx, valute, *asOfDate)
|
||||
} else {
|
||||
price, err = c.fetchDailyRate(ctx, valute)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
return &mmodel.Ticker{
|
||||
Symbol: formatSymbol(isoCode, asOfDate),
|
||||
BidPrice: price,
|
||||
AskPrice: price,
|
||||
Provider: c.provider,
|
||||
Timestamp: now,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *cbrConnector) refreshDirectory() error {
|
||||
endpoint, err := c.buildURL(c.directoryPath, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return merrors.InternalWrap(err, "cbr: build directory request")
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
c.logger.Warn("CBR directory request failed", zap.Error(err))
|
||||
return merrors.InternalWrap(err, "cbr: directory request failed")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
c.logger.Warn("CBR directory returned non-OK status", zap.Int("status", resp.StatusCode))
|
||||
return merrors.Internal("cbr: unexpected status " + strconv.Itoa(resp.StatusCode))
|
||||
}
|
||||
|
||||
decoder := xml.NewDecoder(resp.Body)
|
||||
decoder.CharsetReader = charset.NewReaderLabel
|
||||
|
||||
var directory valuteDirectory
|
||||
if err := decoder.Decode(&directory); err != nil {
|
||||
c.logger.Warn("CBR directory decode failed", zap.Error(err))
|
||||
return merrors.InternalWrap(err, "cbr: decode directory")
|
||||
}
|
||||
|
||||
mapping, err := buildValuteMapping(directory.Items)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.byISO = mapping.byISO
|
||||
c.byID = mapping.byID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *cbrConnector) fetchDailyRate(ctx context.Context, valute valuteInfo) (string, error) {
|
||||
endpoint, err := c.buildURL(c.dailyPath, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return "", merrors.InternalWrap(err, "cbr: build daily request")
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
c.logger.Warn("CBR daily request failed", zap.String("currency", valute.ISOCharCode), zap.Error(err))
|
||||
return "", merrors.InternalWrap(err, "cbr: daily request failed")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
c.logger.Warn("CBR daily returned non-OK status", zap.Int("status", resp.StatusCode))
|
||||
return "", merrors.Internal("cbr: unexpected status " + strconv.Itoa(resp.StatusCode))
|
||||
}
|
||||
|
||||
decoder := xml.NewDecoder(resp.Body)
|
||||
decoder.CharsetReader = charset.NewReaderLabel
|
||||
|
||||
var payload dailyRates
|
||||
if err := decoder.Decode(&payload); err != nil {
|
||||
c.logger.Warn("CBR daily decode failed", zap.Error(err))
|
||||
return "", merrors.InternalWrap(err, "cbr: decode daily response")
|
||||
}
|
||||
|
||||
entry := payload.find(valute.ID)
|
||||
if entry == nil {
|
||||
return "", merrors.NoData("cbr: currency not found in daily rates: " + valute.ISOCharCode)
|
||||
}
|
||||
|
||||
if err := validateDailyEntry(valute, entry); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return computePrice(entry.Value, entry.Nominal)
|
||||
}
|
||||
|
||||
func (c *cbrConnector) fetchHistoricalRate(ctx context.Context, valute valuteInfo, date time.Time) (string, error) {
|
||||
query := map[string]string{
|
||||
"date_req1": date.Format("02/01/2006"),
|
||||
"date_req2": date.Format("02/01/2006"),
|
||||
"VAL_NM_RQ": valute.ID,
|
||||
}
|
||||
endpoint, err := c.buildURL(c.dynamicPath, query)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return "", merrors.InternalWrap(err, "cbr: build historical request")
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
c.logger.Warn("CBR historical request failed", zap.String("currency", valute.ISOCharCode), zap.Error(err))
|
||||
return "", merrors.InternalWrap(err, "cbr: historical request failed")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
c.logger.Warn("CBR historical returned non-OK status", zap.Int("status", resp.StatusCode))
|
||||
return "", merrors.Internal("cbr: unexpected status " + strconv.Itoa(resp.StatusCode))
|
||||
}
|
||||
|
||||
decoder := xml.NewDecoder(resp.Body)
|
||||
decoder.CharsetReader = charset.NewReaderLabel
|
||||
|
||||
var payload dynamicRates
|
||||
if err := decoder.Decode(&payload); err != nil {
|
||||
c.logger.Warn("CBR historical decode failed", zap.Error(err))
|
||||
return "", merrors.InternalWrap(err, "cbr: decode historical response")
|
||||
}
|
||||
|
||||
record := payload.find(valute.ID, date)
|
||||
if record == nil {
|
||||
return "", merrors.NoData("cbr: historical rate not found for " + valute.ISOCharCode)
|
||||
}
|
||||
|
||||
if record.Nominal != "" {
|
||||
nominal, err := parseNominal(record.Nominal)
|
||||
if err != nil {
|
||||
return "", merrors.InvalidDataType(err.Error())
|
||||
}
|
||||
if nominal != valute.Nominal {
|
||||
return "", merrors.Internal("cbr: historical nominal mismatch for " + valute.ISOCharCode)
|
||||
}
|
||||
}
|
||||
|
||||
return computePrice(record.Value, strconv.FormatInt(valute.Nominal, 10))
|
||||
}
|
||||
|
||||
func (c *cbrConnector) buildURL(path string, query map[string]string) (string, error) {
|
||||
base, err := url.Parse(c.base)
|
||||
if err != nil {
|
||||
return "", merrors.InternalWrap(err, "cbr: parse base url")
|
||||
}
|
||||
base.Path = strings.TrimRight(base.Path, "/") + path
|
||||
q := base.Query()
|
||||
for key, value := range query {
|
||||
q.Set(key, value)
|
||||
}
|
||||
base.RawQuery = q.Encode()
|
||||
return base.String(), nil
|
||||
}
|
||||
|
||||
type valuteDirectory struct {
|
||||
Items []valuteItem `xml:"Item"`
|
||||
}
|
||||
|
||||
type valuteItem struct {
|
||||
ID string `xml:"ID,attr"`
|
||||
ISOChar string `xml:"ISO_Char_Code"`
|
||||
ISONum string `xml:"ISO_Num_Code"`
|
||||
Name string `xml:"Name"`
|
||||
EngName string `xml:"EngName"`
|
||||
NominalStr string `xml:"Nominal"`
|
||||
}
|
||||
|
||||
type valuteInfo struct {
|
||||
ID string
|
||||
ISOCharCode string
|
||||
ISONumCode string
|
||||
Name string
|
||||
EngName string
|
||||
Nominal int64
|
||||
}
|
||||
|
||||
type valuteMapping struct {
|
||||
byISO map[string]valuteInfo
|
||||
byID map[string]valuteInfo
|
||||
}
|
||||
|
||||
func buildValuteMapping(items []valuteItem) (*valuteMapping, error) {
|
||||
byISO := make(map[string]valuteInfo, len(items))
|
||||
byID := make(map[string]valuteInfo, len(items))
|
||||
byNum := make(map[string]string, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
id := strings.TrimSpace(item.ID)
|
||||
isoChar := strings.ToUpper(strings.TrimSpace(item.ISOChar))
|
||||
isoNum := strings.TrimSpace(item.ISONum)
|
||||
name := strings.TrimSpace(item.Name)
|
||||
engName := strings.TrimSpace(item.EngName)
|
||||
nominal, err := parseNominal(item.NominalStr)
|
||||
if err != nil {
|
||||
return nil, merrors.InvalidDataType("cbr: parse directory nominal: " + err.Error())
|
||||
}
|
||||
if id == "" || isoChar == "" {
|
||||
return nil, merrors.InvalidDataType("cbr: directory contains entry with empty id or iso code")
|
||||
}
|
||||
|
||||
info := valuteInfo{
|
||||
ID: id,
|
||||
ISOCharCode: isoChar,
|
||||
ISONumCode: isoNum,
|
||||
Name: name,
|
||||
EngName: engName,
|
||||
Nominal: nominal,
|
||||
}
|
||||
|
||||
if existing, ok := byISO[isoChar]; ok && existing.ID != id {
|
||||
return nil, merrors.InvalidDataType("cbr: duplicate ISO code " + isoChar)
|
||||
}
|
||||
if existing, ok := byID[id]; ok && existing.ISOCharCode != isoChar {
|
||||
return nil, merrors.InvalidDataType("cbr: duplicate valute id " + id)
|
||||
}
|
||||
if isoNum != "" {
|
||||
if existingID, ok := byNum[isoNum]; ok && existingID != id {
|
||||
return nil, merrors.InvalidDataType("cbr: duplicate ISO numeric code " + isoNum)
|
||||
}
|
||||
byNum[isoNum] = id
|
||||
}
|
||||
|
||||
byISO[isoChar] = info
|
||||
byID[id] = info
|
||||
}
|
||||
|
||||
if len(byISO) == 0 {
|
||||
return nil, merrors.InvalidDataType("cbr: empty directory received")
|
||||
}
|
||||
|
||||
return &valuteMapping{
|
||||
byISO: byISO,
|
||||
byID: byID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type dailyRates struct {
|
||||
Valutes []dailyValute `xml:"Valute"`
|
||||
}
|
||||
|
||||
type dailyValute struct {
|
||||
ID string `xml:"ID,attr"`
|
||||
NumCode string `xml:"NumCode"`
|
||||
CharCode string `xml:"CharCode"`
|
||||
Nominal string `xml:"Nominal"`
|
||||
Name string `xml:"Name"`
|
||||
Value string `xml:"Value"`
|
||||
}
|
||||
|
||||
func (d *dailyRates) find(id string) *dailyValute {
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
for idx := range d.Valutes {
|
||||
if strings.EqualFold(strings.TrimSpace(d.Valutes[idx].ID), id) {
|
||||
return &d.Valutes[idx]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type dynamicRates struct {
|
||||
Records []dynamicRecord `xml:"Record"`
|
||||
}
|
||||
|
||||
type dynamicRecord struct {
|
||||
ID string `xml:"Id,attr"`
|
||||
DateRaw string `xml:"Date,attr"`
|
||||
Nominal string `xml:"Nominal"`
|
||||
Value string `xml:"Value"`
|
||||
}
|
||||
|
||||
func (d *dynamicRates) find(id string, date time.Time) *dynamicRecord {
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
target := date.Format("02.01.2006")
|
||||
for idx := range d.Records {
|
||||
rec := &d.Records[idx]
|
||||
if !strings.EqualFold(strings.TrimSpace(rec.ID), id) {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(rec.DateRaw) == target {
|
||||
return rec
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateDailyEntry(expected valuteInfo, entry *dailyValute) error {
|
||||
if entry == nil {
|
||||
return merrors.NoData("cbr: missing daily entry")
|
||||
}
|
||||
if !strings.EqualFold(strings.TrimSpace(entry.CharCode), expected.ISOCharCode) {
|
||||
return merrors.Internal("cbr: char code mismatch for " + expected.ISOCharCode)
|
||||
}
|
||||
if expected.ISONumCode != "" && strings.TrimSpace(entry.NumCode) != expected.ISONumCode {
|
||||
return merrors.Internal("cbr: iso numeric mismatch for " + expected.ISOCharCode)
|
||||
}
|
||||
if expected.Name != "" && strings.TrimSpace(entry.Name) != expected.Name {
|
||||
return merrors.Internal("cbr: currency name mismatch for " + expected.ISOCharCode)
|
||||
}
|
||||
|
||||
nominal, err := parseNominal(entry.Nominal)
|
||||
if err != nil {
|
||||
return merrors.InvalidDataType("cbr: parse daily nominal: " + err.Error())
|
||||
}
|
||||
if nominal != expected.Nominal {
|
||||
return merrors.Internal("cbr: nominal mismatch for " + expected.ISOCharCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseSymbol(symbol string) (string, *time.Time, error) {
|
||||
trimmed := strings.TrimSpace(symbol)
|
||||
if trimmed == "" {
|
||||
return "", nil, merrors.InvalidArgument("cbr: symbol is empty", "symbol")
|
||||
}
|
||||
|
||||
parts := strings.Split(trimmed, "@")
|
||||
if len(parts) > 2 {
|
||||
return "", nil, merrors.InvalidArgument("cbr: invalid symbol format", "symbol")
|
||||
}
|
||||
|
||||
iso := strings.ToUpper(strings.TrimSpace(parts[0]))
|
||||
if len(iso) != 3 {
|
||||
return "", nil, merrors.InvalidArgument("cbr: symbol must be ISO currency code", "symbol")
|
||||
}
|
||||
|
||||
if len(parts) == 1 {
|
||||
return iso, nil, nil
|
||||
}
|
||||
|
||||
datePart := strings.TrimSpace(parts[1])
|
||||
if datePart == "" {
|
||||
return "", nil, merrors.InvalidArgument("cbr: date component is empty", "symbol")
|
||||
}
|
||||
|
||||
parsed, err := time.Parse("2006-01-02", datePart)
|
||||
if err != nil {
|
||||
return "", nil, merrors.InvalidArgumentWrap(err, "cbr: invalid date component", "symbol")
|
||||
}
|
||||
|
||||
return iso, &parsed, nil
|
||||
}
|
||||
|
||||
func parseNominal(value string) (int64, error) {
|
||||
nominal, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
|
||||
if err != nil || nominal <= 0 {
|
||||
return 0, merrors.InvalidDataType("cbr: invalid nominal \"" + value + "\"")
|
||||
}
|
||||
return nominal, nil
|
||||
}
|
||||
|
||||
func computePrice(value string, nominalStr string) (string, error) {
|
||||
raw := strings.ReplaceAll(strings.TrimSpace(value), " ", "")
|
||||
raw = strings.ReplaceAll(raw, ",", ".")
|
||||
|
||||
r := new(big.Rat)
|
||||
if _, ok := r.SetString(raw); !ok {
|
||||
return "", merrors.InvalidDataType("invalid decimal \"" + value + "\"")
|
||||
}
|
||||
|
||||
nominal, err := parseNominal(nominalStr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
den := big.NewRat(nominal, 1)
|
||||
price := new(big.Rat).Quo(r, den)
|
||||
return price.FloatString(8), nil
|
||||
}
|
||||
|
||||
func formatSymbol(iso string, asOf *time.Time) string {
|
||||
if asOf == nil {
|
||||
return iso
|
||||
}
|
||||
return iso + "@" + asOf.Format("2006-01-02")
|
||||
}
|
||||
226
api/fx/ingestor/internal/market/cbr/connector_test.go
Normal file
226
api/fx/ingestor/internal/market/cbr/connector_test.go
Normal file
@@ -0,0 +1,226 @@
|
||||
package cbr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestFetchTickerDaily(t *testing.T) {
|
||||
transport := &stubRoundTripper{
|
||||
responses: map[string]stubResponse{
|
||||
"/scripts/XML_valFull.asp": {body: valuteDirectoryXML},
|
||||
"/scripts/XML_daily.asp": {body: dailyRatesXML},
|
||||
},
|
||||
}
|
||||
|
||||
conn, err := NewConnector(zap.NewNop(), map[string]any{
|
||||
"base_url": "http://cbr.test",
|
||||
"http_round_tripper": transport,
|
||||
"request_timeout_seconds": 1,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewConnector returned error: %v", err)
|
||||
}
|
||||
|
||||
ticker, err := conn.FetchTicker(context.Background(), "USD")
|
||||
if err != nil {
|
||||
t.Fatalf("FetchTicker returned error: %v", err)
|
||||
}
|
||||
|
||||
if ticker.Provider != "cbr" {
|
||||
t.Fatalf("unexpected provider: %s", ticker.Provider)
|
||||
}
|
||||
if ticker.BidPrice != "95.12340000" || ticker.AskPrice != "95.12340000" {
|
||||
t.Fatalf("unexpected bid/ask: %s / %s", ticker.BidPrice, ticker.AskPrice)
|
||||
}
|
||||
if ticker.Symbol != "USD" {
|
||||
t.Fatalf("unexpected symbol: %s", ticker.Symbol)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchTickerValidatesDailyEntry(t *testing.T) {
|
||||
transport := &stubRoundTripper{
|
||||
responses: map[string]stubResponse{
|
||||
"/scripts/XML_valFull.asp": {body: valuteDirectoryXML},
|
||||
"/scripts/XML_daily.asp": {body: strings.ReplaceAll(dailyRatesXML, "<CharCode>USD</CharCode>", "<CharCode>XXX</CharCode>")},
|
||||
},
|
||||
}
|
||||
|
||||
conn, err := NewConnector(zap.NewNop(), map[string]any{
|
||||
"base_url": "http://cbr.test",
|
||||
"http_round_tripper": transport,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewConnector returned error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := conn.FetchTicker(context.Background(), "USD"); err == nil {
|
||||
t.Fatalf("FetchTicker expected to fail due to mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchTickerHistorical(t *testing.T) {
|
||||
transport := &stubRoundTripper{
|
||||
responses: map[string]stubResponse{
|
||||
"/scripts/XML_valFull.asp": {body: valuteDirectoryXML},
|
||||
"/scripts/XML_dynamic.asp": {
|
||||
body: dynamicRatesXML,
|
||||
check: func(r *http.Request) error {
|
||||
if got := r.URL.Query().Get("VAL_NM_RQ"); got != "R01235" {
|
||||
return fmt.Errorf("unexpected valute id: %s", got)
|
||||
}
|
||||
if got := r.URL.Query().Get("date_req1"); got != "05/01/2023" {
|
||||
return fmt.Errorf("unexpected date_req1: %s", got)
|
||||
}
|
||||
if got := r.URL.Query().Get("date_req2"); got != "05/01/2023" {
|
||||
return fmt.Errorf("unexpected date_req2: %s", got)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
conn, err := NewConnector(zap.NewNop(), map[string]any{
|
||||
"base_url": "http://cbr.test",
|
||||
"http_round_tripper": transport,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewConnector returned error: %v", err)
|
||||
}
|
||||
|
||||
ticker, err := conn.FetchTicker(context.Background(), "USD@2023-01-05")
|
||||
if err != nil {
|
||||
t.Fatalf("FetchTicker returned error: %v", err)
|
||||
}
|
||||
|
||||
if ticker.BidPrice != "70.10000000" || ticker.AskPrice != "70.10000000" {
|
||||
t.Fatalf("unexpected bid/ask: %s / %s", ticker.BidPrice, ticker.AskPrice)
|
||||
}
|
||||
if ticker.Symbol != "USD@2023-01-05" {
|
||||
t.Fatalf("unexpected symbol: %s", ticker.Symbol)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchTickerUnknownCurrency(t *testing.T) {
|
||||
transport := &stubRoundTripper{
|
||||
responses: map[string]stubResponse{
|
||||
"/scripts/XML_valFull.asp": {body: valuteDirectoryXML},
|
||||
"/scripts/XML_daily.asp": {body: dailyRatesXML},
|
||||
},
|
||||
}
|
||||
|
||||
conn, err := NewConnector(zap.NewNop(), map[string]any{
|
||||
"base_url": "http://cbr.test",
|
||||
"http_round_tripper": transport,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewConnector returned error: %v", err)
|
||||
}
|
||||
|
||||
_, err = conn.FetchTicker(context.Background(), "ZZZ")
|
||||
if err == nil {
|
||||
t.Fatalf("FetchTicker expected to fail for unknown currency")
|
||||
}
|
||||
if !errors.Is(err, merrors.ErrInvalidArg) {
|
||||
t.Fatalf("expected invalid argument error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchTickerRespectsCustomPaths(t *testing.T) {
|
||||
transport := &stubRoundTripper{
|
||||
responses: map[string]stubResponse{
|
||||
"/dir.xml": {body: valuteDirectoryXML},
|
||||
"/rates.xml": {body: dailyRatesXML},
|
||||
},
|
||||
}
|
||||
|
||||
conn, err := NewConnector(zap.NewNop(), map[string]any{
|
||||
"base_url": "http://cbr.test",
|
||||
"directory_path": "/dir.xml",
|
||||
"daily_path": "/rates.xml",
|
||||
"http_round_tripper": transport,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewConnector returned error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := conn.FetchTicker(context.Background(), "USD"); err != nil {
|
||||
t.Fatalf("FetchTicker returned error with custom paths: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
const valuteDirectoryXML = `
|
||||
<Valuta name="Foreign Currency Market">
|
||||
<Item ID="R01235">
|
||||
<ISO_Num_Code>840</ISO_Num_Code>
|
||||
<ISO_Char_Code>USD</ISO_Char_Code>
|
||||
<Nominal>1</Nominal>
|
||||
<Name>US Dollar</Name>
|
||||
<EngName>US Dollar</EngName>
|
||||
</Item>
|
||||
</Valuta>`
|
||||
|
||||
const dailyRatesXML = `
|
||||
<ValCurs Date="02.09.2024" name="Foreign Currency Market">
|
||||
<Valute ID="R01235">
|
||||
<NumCode>840</NumCode>
|
||||
<CharCode>USD</CharCode>
|
||||
<Nominal>1</Nominal>
|
||||
<Name>US Dollar</Name>
|
||||
<Value>95,1234</Value>
|
||||
</Valute>
|
||||
</ValCurs>`
|
||||
|
||||
const dynamicRatesXML = `
|
||||
<ValCurs ID="R01235" DateRange1="05/01/2023" DateRange2="05/01/2023" name="Foreign Currency Market Dynamic">
|
||||
<Record Date="05.01.2023" Id="R01235">
|
||||
<Nominal>1</Nominal>
|
||||
<Value>70,1</Value>
|
||||
</Record>
|
||||
</ValCurs>`
|
||||
|
||||
type stubResponse struct {
|
||||
status int
|
||||
body string
|
||||
check func(*http.Request) error
|
||||
}
|
||||
|
||||
type stubRoundTripper struct {
|
||||
responses map[string]stubResponse
|
||||
}
|
||||
|
||||
func (s *stubRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if s.responses == nil {
|
||||
return nil, fmt.Errorf("no responses configured")
|
||||
}
|
||||
res, ok := s.responses[req.URL.Path]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected request path: %s", req.URL.Path)
|
||||
}
|
||||
if res.check != nil {
|
||||
if err := res.check(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
status := res.status
|
||||
if status == 0 {
|
||||
status = http.StatusOK
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: status,
|
||||
Body: io.NopCloser(strings.NewReader(res.body)),
|
||||
Header: http.Header{"Content-Type": []string{"text/xml"}},
|
||||
Request: req,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user