Skip to content

Commit 481f820

Browse files
authored
Merge pull request #1 from dannyp11/dev
Add symbol & doc
2 parents 1af515d + 8c7f210 commit 481f820

File tree

12 files changed

+107
-45
lines changed

12 files changed

+107
-45
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212

1313
jobs:
1414
build:
15-
runs-on: ubuntu-latest
15+
runs-on: ubuntu-20.04
1616
strategy:
1717
matrix:
1818
python: [3.6, 3.7, 3.8, 3.9]

README.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
# Introduction
2-
This is unofficial https://exchangerate.host python client library
2+
This is unofficial https://exchangerate.host (https://github.com/Formicka/exchangerate.host) python client library
33

44
# Getting started
55

66
## Installation
7+
- Using pip `pip install exchangerate-client
78

8-
## Sample usage
9+
## Usage
10+
- Get all currency symbols
11+
```
12+
import exchangerate
13+
client = exchangerate.ExchangerateClient()
14+
print(client.symbols())
15+
```
916

10-
# Development guide
17+
## Configuration
18+
-
19+
20+
# Development guide
21+
## Testing
22+
This package uses `tox` to run testing automation against multiple python versions, to install and run tox, use
23+
24+
```
25+
pip install tox
26+
tox
27+
```
28+
29+
## Local development
30+
- Install to local env with `pip install --editable .`
31+
- Then this will work `python -c "import exchangerate"`

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
long_description = fh.read()
55

66
setuptools.setup(
7-
name='exchangerate-python',
7+
name='exchangerate-client',
88
author='Daniel Pham',
99
author_email='[email protected]',
1010
description='Unofficial exchangerate.host python client library',
@@ -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

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
# -------------------------------------------------------------------
18+
# Public methods
19+
# -------------------------------------------------------------------
20+
21+
def symbols(self):
22+
url = self._build_url(path="symbols")
23+
resp_json = self._validate_and_get_json(url)
24+
return resp_json.get("rates")
25+
26+
# -------------------------------------------------------------------
27+
def _validate_and_get_json(self, url):
28+
resp = self.session.get(url)
29+
if resp.status_code != 200:
30+
raise ResponseErrorException("Status code=%d calling url=%s" % (resp.status_code, url))
31+
32+
resp_json = resp.json()
33+
if not resp_json.get("success", False):
34+
raise ResponseErrorException("No success field calling url=%s" % (url))
35+
36+
return resp_json
37+
38+
def _build_url(self, path="", params=None):
39+
Components = namedtuple(
40+
typename='Components',
41+
field_names=['scheme', 'netloc', 'url', 'path', 'query', 'fragment']
42+
)
43+
44+
if not params:
45+
params = {}
46+
47+
return urlunparse(
48+
Components(
49+
scheme='https',
50+
netloc=self.region.value,
51+
query=urlencode(params),
52+
path=path,
53+
url="/",
54+
fragment=""
55+
)
56+
)

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)