This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
exchange-rates-client.go
90 lines (70 loc) · 2.01 KB
/
exchange-rates-client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package pluginmgr
import (
"net/url"
"github.com/leonardochaia/vendopunkto/clients"
"github.com/leonardochaia/vendopunkto/dtos"
"github.com/leonardochaia/vendopunkto/errors"
"github.com/leonardochaia/vendopunkto/plugin"
)
type exchangeRatesClientImpl struct {
apiURL url.URL
client clients.HTTP
info plugin.PluginInfo
}
func newExchangeRatesClient(
url url.URL,
info plugin.PluginInfo,
client clients.HTTP) plugin.ExchangeRatesPlugin {
return &exchangeRatesClientImpl{
apiURL: url,
info: info,
client: client,
}
}
func (c exchangeRatesClientImpl) getBaseURL(end string) (*url.URL, error) {
u, err := url.Parse(c.info.GetAddress() + plugin.ExchangeRatesMainEndpoint + end)
if err != nil {
return nil, err
}
return u, nil
}
func (c exchangeRatesClientImpl) GetExchangeRates(
currency string,
currencies []string) (plugin.ExchangeRatesResult, error) {
const op errors.Op = "exchangeRatesClient.getExchangeRates"
u, err := c.getBaseURL("")
if err != nil {
return nil, errors.E(op, errors.Internal, err)
}
final := c.apiURL.ResolveReference(u).String()
params := &plugin.GetExchangeRatesParams{
Currency: currency,
Currencies: currencies,
}
var result plugin.ExchangeRatesResult
_, err = c.client.PostJSON(final, params, &result)
if err != nil {
return nil, errors.E(op, err)
}
return result, nil
}
func (c exchangeRatesClientImpl) SearchSupportedCurrencies(term string) ([]dtos.BasicCurrencyDto, error) {
const op errors.Op = "exchangeRatesClient.searchSupportedCurrencies"
u, err := c.getBaseURL(plugin.ExchangeRatesSupportedCurrencies)
if err != nil {
return nil, errors.E(op, errors.Internal, err)
}
final := c.apiURL.ResolveReference(u).String()
params := &dtos.SearchSupportedCurrenciesParams{
Term: term,
}
var result []dtos.BasicCurrencyDto
_, err = c.client.PostJSON(final, params, &result)
if err != nil {
return nil, errors.E(op, err)
}
return result, nil
}
func (c exchangeRatesClientImpl) GetPluginInfo() (plugin.PluginInfo, error) {
return c.info, nil
}