Skip to content

Commit a0e87f4

Browse files
committed
Working tests
1 parent 6c8e3c2 commit a0e87f4

File tree

10 files changed

+76
-40
lines changed

10 files changed

+76
-40
lines changed

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[metadata]
2-
version = attr: examplepy.__version__
2+
version = attr: exchangerate.__version__
33
license_files = LICENSE

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
'Operating System :: OS Independent',
4040
],
4141
python_requires='>=3.6',
42-
# install_requires=['Pillow'],
42+
install_requires=['requests'],
4343
extras_require={
4444
'dev': ['check-manifest'],
4545
# 'test': ['coverage'],

src/examplepy/__init__.py

-3
This file was deleted.

src/examplepy/module1.py

-22
This file was deleted.

src/exchangerate/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__version__ = "0.1.0"
2+
3+
from .config import Region
4+
from .client import ExchangerateClient

src/exchangerate/client.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import requests
2+
from collections import namedtuple
3+
from urllib.parse import urlencode, urlunparse
4+
5+
from .config import Region
6+
from .exceptions import *
7+
8+
class ExchangerateClient:
9+
"""
10+
Primary client class
11+
"""
12+
def __init__(self, base_currency="USD", region=Region.AMERICA):
13+
self.base_currency = base_currency
14+
self.region = region
15+
self.session = requests.Session()
16+
17+
def symbols(self):
18+
url = self._build_url(path="symbols")
19+
resp_json = self._validate_and_get_json(url)
20+
return resp_json.get("rates")
21+
22+
def _validate_and_get_json(self, url):
23+
resp = self.session.get(url)
24+
if resp.status_code != 200:
25+
raise ResponseErrorException("Status code=%d calling url=%s" % (resp.status_code, url))
26+
27+
resp_json = resp.json()
28+
if not resp_json.get("success", False):
29+
raise ResponseErrorException("No success field calling url=%s" % (url))
30+
31+
return resp_json
32+
33+
def _build_url(self, path="", params=None):
34+
Components = namedtuple(
35+
typename='Components',
36+
field_names=['scheme', 'netloc', 'url', 'path', 'query', 'fragment']
37+
)
38+
39+
if not params:
40+
params = {}
41+
42+
return urlunparse(
43+
Components(
44+
scheme='https',
45+
netloc=self.region.value,
46+
query=urlencode(params),
47+
path=path,
48+
url="/",
49+
fragment=""
50+
)
51+
)

src/exchangerate/config.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from enum import Enum
2+
3+
class Region(Enum):
4+
EUROPE = "api.exchangerate.host"
5+
AMERICA = "api-us.exchangerate.host"

src/exchangerate/exceptions.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ExchangerateException(Exception):
2+
pass
3+
4+
class ResponseErrorException(ExchangerateException):
5+
pass

tests/test_client.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from exchangerate.client import ExchangerateClient
2+
3+
def test_get_symbols():
4+
client = ExchangerateClient()
5+
all_symbols = client.symbols()
6+
7+
assert "USD" in all_symbols
8+
assert "CAD" in all_symbols
9+
assert "AUD" in all_symbols

tests/test_module1.py

-13
This file was deleted.

0 commit comments

Comments
 (0)