Skip to content

Commit

Permalink
add oxr features
Browse files Browse the repository at this point in the history
  • Loading branch information
luisfmcalado committed Mar 1, 2020
1 parent c3acc2d commit e7cf95d
Show file tree
Hide file tree
Showing 53 changed files with 1,649 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
# E501 is the "Line too long" error. We disable it because we use Black for
# code formatting. Black makes a best effort to keep lines under the max
# length, but can go over in some cases.
# W503 goes against PEP8 rules. It's disabled by default, but must be disabled
# explicitly when using `ignore`.
ignore = E501, W503
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# pyenv
.python-version

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
sudo: false
dist: xenial

language: python

matrix:
include:
- python: "3.7"
- python: "3.8"

cache:
pip: true

install:
- make venv

script:
- make lint
- make fmtcheck
- make ci

after_success: make coveralls
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"ms-python.python",
"bungcip.better-toml",
"EditorConfig.editorconfig"
]
}
23 changes: 23 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// Show the path in the top window bar.
"window.title": "${rootName}${separator}${activeEditorMedium}",
"editor.renderWhitespace": "all",
"files.exclude": {
"**/*.pyc": {
"when": "$(basename).py"
},
"**/__pycache__": true
},
// Formatting
"editor.formatOnSave": true,
"python.formatting.provider": "black",
// Linting
"python.linting.enabled": true,
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
// Tests
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.nosetestsEnabled": false,
"python.pythonPath": "/Users/luis.calado/repos/personal/coinoxr/venv/bin/python3.7"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [2020] Luis Calado

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
VENV_NAME?=venv

venv: $(VENV_NAME)/bin/activate

$(VENV_NAME)/bin/activate: setup.py
pip install --upgrade pip virtualenv
@test -d $(VENV_NAME) || python -m virtualenv --clear $(VENV_NAME)
${VENV_NAME}/bin/python -m pip install -U pip tox
${VENV_NAME}/bin/python -m pip install -e .
@touch $(VENV_NAME)/bin/activate

test: venv
@${VENV_NAME}/bin/tox -p auto

ci: venv
@${VENV_NAME}/bin/python setup.py test -a "--cov=coinoxr"

coveralls: venv
@${VENV_NAME}/bin/pip install --upgrade coveralls
@${VENV_NAME}/bin/coveralls

fmt: venv
@${VENV_NAME}/bin/tox -e fmt

fmtcheck: venv
@${VENV_NAME}/bin/tox -e fmt -- --check --verbose

lint: venv
@${VENV_NAME}/bin/tox -e lint

clean:
@rm -rf $(VENV_NAME) build/ dist/

.PHONY: venv test coveralls fmt fmtcheck lint clean
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
# Coinoxr - Open Exchange Rates

[![Build Status](https://travis-ci.org/luisfmcalado/coinoxr.svg?branch=master)](https://travis-ci.org/luisfmcalado/coinoxr)
[![Coverage Status](https://coveralls.io/repos/github/luisfmcalado/coinoxr/badge.svg?branch=master)](https://coveralls.io/github/luisfmcalado/coinoxr?branch=master)

Python library for Open Exchange Rates API.

## Installation

Install dependency
```bash
$ pip install coinoxr
```

Install dependency from source code
```bash
$ python setup.py install
```

### Requirements
- Python 3.7+


## Usage

```python
import coinoxr
coinoxr.app_id = "c01ed21da6424fd3b0ac68f9e63a3d29"

coinoxr.Latest().get(base="EUR")
```

## Development

Run all the tests for every environment:

```bash
$ make test
```

The code can be formatted with black:

```bash
$ make fmt
```
19 changes: 19 additions & 0 deletions coinoxr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from coinoxr.client import RequestsClient


default_http_client = RequestsClient()
default_url = "https://openexchangerates.org/api/"
app_id = None


def base_url(path):
return default_url + path


from coinoxr.usage import Usage # noqa
from coinoxr.latest import Latest # noqa
from coinoxr.historical import Historical # noqa
from coinoxr.currency import Currency # noqa
from coinoxr.time_series import TimeSeries # noqa
from coinoxr.convert import Convert # noqa
from coinoxr.ohlc import Ohlc # noqa
16 changes: 16 additions & 0 deletions coinoxr/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import requests
from coinoxr.response import Response


class HttpClient:
def get(self, url, params):
return Response(200, None)


class RequestsClient(HttpClient):
def get(self, url, params):
response = requests.get(url, params=params)
try:
return Response(response.status_code, response.json())
except ValueError:
return Response(response.status_code, None)
13 changes: 13 additions & 0 deletions coinoxr/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from coinoxr.requestor import Requestor


class Convert:
def __init__(self, requestor=None):
self._requestor = requestor
if requestor is None:
self._requestor = Requestor()

def get(self, amount, from_currency, to_currency, pretty_print=False):
path = "convert/%s/%s/%s" % (amount, from_currency, to_currency)
params = {"prettyprint": pretty_print}
return self._requestor.get(path, params)
18 changes: 18 additions & 0 deletions coinoxr/currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from coinoxr.requestor import Requestor


class Currency:
def __init__(self, requestor=None):
self._requestor = requestor
if requestor is None:
self._requestor = Requestor(skip_app_id=True)

def get(
self, pretty_print=False, show_alternative=False, show_inactive=False,
):
params = {
"prettyprint": pretty_print,
"show_alternative": show_alternative,
"show_inactive": show_inactive,
}
return self._requestor.get("currencies.json", params)
2 changes: 2 additions & 0 deletions coinoxr/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class AppIdError(Exception):
pass
21 changes: 21 additions & 0 deletions coinoxr/historical.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from coinoxr.requestor import Requestor


class Historical:
def __init__(self, requestor=None):
self._requestor = requestor
if requestor is None:
self._requestor = Requestor()

def get(
self, date, base=None, pretty_print=False, symbols=None, show_alternative=False
):
params = {"prettyprint": pretty_print, "show_alternative": show_alternative}

if base is not None:
params["base"] = base

if symbols is not None:
params["symbols"] = symbols

return self._requestor.get("historical/%s.json" % date, params)
19 changes: 19 additions & 0 deletions coinoxr/latest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from coinoxr.requestor import Requestor


class Latest:
def __init__(self, requestor=None):
self._requestor = requestor
if requestor is None:
self._requestor = Requestor()

def get(self, base=None, pretty_print=False, symbols=None, show_alternative=False):
params = {"prettyprint": pretty_print, "show_alternative": show_alternative}

if base is not None:
params["base"] = base

if symbols is not None:
params["symbols"] = symbols

return self._requestor.get("latest.json", params)
25 changes: 25 additions & 0 deletions coinoxr/ohlc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from coinoxr.requestor import Requestor


class Ohlc:
def __init__(self, requestor=None):
self._requestor = requestor
if requestor is None:
self._requestor = Requestor()

def get(
self, start_time, period, base=None, pretty_print=False, symbols=None,
):
params = {
"prettyprint": pretty_print,
"period": period,
"start_time": start_time,
}

if base is not None:
params["base"] = base

if symbols is not None:
params["symbols"] = symbols

return self._requestor.get("ohlc.json", params)
Loading

0 comments on commit e7cf95d

Please sign in to comment.