Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
matrix:
python: [3.6, 3.7, 3.8, 3.9]
Expand Down
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
# Introduction
This is unofficial https://exchangerate.host python client library
This is unofficial https://exchangerate.host (https://github.com/Formicka/exchangerate.host) python client library

# Getting started

## Installation
- Using pip `pip install exchangerate-client

## Sample usage
## Usage
- Get all currency symbols
```
import exchangerate
client = exchangerate.ExchangerateClient()
print(client.symbols())
```

# Development guide
## Configuration
-

# Development guide
## Testing
This package uses `tox` to run testing automation against multiple python versions, to install and run tox, use

```
pip install tox
tox
```

## Local development
- Install to local env with `pip install --editable .`
- Then this will work `python -c "import exchangerate"`
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[metadata]
version = attr: examplepy.__version__
version = attr: exchangerate.__version__
license_files = LICENSE
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
long_description = fh.read()

setuptools.setup(
name='exchangerate-python',
name='exchangerate-client',
author='Daniel Pham',
author_email='tdpham1105@yahoo.com',
description='Unofficial exchangerate.host python client library',
Expand Down Expand Up @@ -39,7 +39,7 @@
'Operating System :: OS Independent',
],
python_requires='>=3.6',
# install_requires=['Pillow'],
install_requires=['requests'],
extras_require={
'dev': ['check-manifest'],
# 'test': ['coverage'],
Expand Down
3 changes: 0 additions & 3 deletions src/examplepy/__init__.py

This file was deleted.

22 changes: 0 additions & 22 deletions src/examplepy/module1.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/exchangerate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__version__ = "0.1.0"

from .config import Region
from .client import ExchangerateClient
56 changes: 56 additions & 0 deletions src/exchangerate/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import requests
from collections import namedtuple
from urllib.parse import urlencode, urlunparse

from .config import Region
from .exceptions import *

class ExchangerateClient:
"""
Primary client class
"""
def __init__(self, base_currency="USD", region=Region.AMERICA):
self.base_currency = base_currency
self.region = region
self.session = requests.Session()

# -------------------------------------------------------------------
# Public methods
# -------------------------------------------------------------------

def symbols(self):
url = self._build_url(path="symbols")
resp_json = self._validate_and_get_json(url)
return resp_json.get("rates")

# -------------------------------------------------------------------
def _validate_and_get_json(self, url):
resp = self.session.get(url)
if resp.status_code != 200:
raise ResponseErrorException("Status code=%d calling url=%s" % (resp.status_code, url))

resp_json = resp.json()
if not resp_json.get("success", False):
raise ResponseErrorException("No success field calling url=%s" % (url))

return resp_json

def _build_url(self, path="", params=None):
Components = namedtuple(
typename='Components',
field_names=['scheme', 'netloc', 'url', 'path', 'query', 'fragment']
)

if not params:
params = {}

return urlunparse(
Components(
scheme='https',
netloc=self.region.value,
query=urlencode(params),
path=path,
url="/",
fragment=""
)
)
5 changes: 5 additions & 0 deletions src/exchangerate/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from enum import Enum

class Region(Enum):
EUROPE = "api.exchangerate.host"
AMERICA = "api-us.exchangerate.host"
5 changes: 5 additions & 0 deletions src/exchangerate/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ExchangerateException(Exception):
pass

class ResponseErrorException(ExchangerateException):
pass
9 changes: 9 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from exchangerate.client import ExchangerateClient

def test_get_symbols():
client = ExchangerateClient()
all_symbols = client.symbols()

assert "USD" in all_symbols
assert "CAD" in all_symbols
assert "AUD" in all_symbols
13 changes: 0 additions & 13 deletions tests/test_module1.py

This file was deleted.