Skip to content

Commit 68e7a3f

Browse files
committed
Add latest rates
1 parent 5ff5d3e commit 68e7a3f

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ client = exchangerate.ExchangerateClient()
1414
print(client.symbols())
1515
```
1616

17+
- Get latest rates
18+
```
19+
import exchangerate
20+
client = exchangerate.ExchangerateClient()
21+
print(client.latest())
22+
```
23+
1724
## Configuration
1825
-
1926

src/exchangerate/client.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,37 @@ class ExchangerateClient:
99
"""
1010
Primary client class
1111
"""
12-
def __init__(self, base_currency="USD", region=Region.AMERICA):
12+
def __init__(self, base_currency="USD", server_region=Region.AMERICA):
1313
self.base_currency = base_currency
14-
self.region = region
14+
self.server_region = server_region
1515
self.session = requests.Session()
1616

1717
# -------------------------------------------------------------------
1818
# Public methods
1919
# -------------------------------------------------------------------
2020

2121
def symbols(self):
22+
"""
23+
Get list of supported symbols
24+
"""
2225
url = self._build_url(path="symbols")
2326
resp_json = self._validate_and_get_json(url)
2427
return resp_json.get("rates")
2528

29+
def latest(self, symbols=None, amount=1):
30+
"""
31+
Get latest rate
32+
33+
@param symbols: list of currencies
34+
@param amount: the currency amount
35+
"""
36+
params = {"amount": amount}
37+
if symbols: params["symbols"] = ",".join(symbols)
38+
39+
url = self._build_url(path="latest", params=params)
40+
resp_json = self._validate_and_get_json(url)
41+
return resp_json.get("rates")
42+
2643
# -------------------------------------------------------------------
2744
def _validate_and_get_json(self, url):
2845
resp = self.session.get(url)
@@ -47,7 +64,7 @@ def _build_url(self, path="", params=None):
4764
return urlunparse(
4865
Components(
4966
scheme='https',
50-
netloc=self.region.value,
67+
netloc=self.server_region.value,
5168
query=urlencode(params),
5269
path=path,
5370
url="/",

tests/test_client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import pytest
12
from exchangerate.client import ExchangerateClient
23

4+
def assert_currency_exist(result):
5+
for currency in ["USD", "CAD", "AUD"]:
6+
assert currency in result
7+
38
def test_get_symbols():
49
client = ExchangerateClient()
510
all_symbols = client.symbols()
11+
assert_currency_exist(all_symbols)
12+
13+
def test_get_latest_rate():
14+
client = ExchangerateClient()
15+
assert_currency_exist(client.latest())
16+
assert_currency_exist(client.latest(amount=5))
617

7-
assert "USD" in all_symbols
8-
assert "CAD" in all_symbols
9-
assert "AUD" in all_symbols
18+
selected_rates = client.latest(symbols=["JPY", "CHF"], amount=5)
19+
assert "JPY" in selected_rates
20+
assert "CHF" in selected_rates

0 commit comments

Comments
 (0)