- supported currencies: USD, EUR, CZK, PLN
- currencies rates are updated everyday at 3:30 am CET
- source data is fetched via http://openexchange.org
- any math calculations use decimal precision of 6
Method | URL | Path Params | Description |
---|---|---|---|
GET | /api/currencies/convert |
:base/:to/:amount (all required) |
Currencies convert controller |
See Documentation for more detailed information about the API.
- Get a free API Key at http://openexchangerates.org
- Clone the repo
git clone https://github.com/bxsx/currencies-converter-rest-service-fastapi.git
- Go to the project directory
cd currencies-converter-rest-service-fastapi/
- Setup your
.env
, you can use.env-dev
as example (NOTE:.env-dev
is not production-ready)cp .env-dev .env
- Enter your API key (
abcdef123456
here) in.env
sed -i -e 's/OXR_ID=.*/OXR_ID=abcdef123456/' .env
- Run the service
docker-compose up --build
.env-dev
contains COMPOSE_FILE
environment variable that is being used by docker-compose
.
It is set to run service in a DEVELOPMENT MODE
.
To deploy on production, don't use .env-dev
file.
Other alternatives are to edit COMPOSE_FILE
variable or to run the compose file explicitly:
docker-compose -f docker-compose.yml up --build
Development mode:
- Live-code (working directory is mounted, web server is reloaded on any changes)
- Concurrency is disabled (
gunicorn
is shutdown,uvicorn --reload
is used)
curl -X GET "$HOST/api/currencies/convert/{base}/{to}/{amount}" -H "accept: application/json"
curl -X GET "http://0.0.0.0/api/currencies/convert/USD/EUR/123" -H "accept: application/json"
{
"base": "USD",
"to": "EUR",
"amount": 123,
"exchange_rate": {
"quote": 0.836134,
"timestamp": "2020-11-29T21:00:02+00:00"
},
"result": 102.844482
}
Go to /openapi.json
to get the current OpenAPI JSON schema.
Or view already generated openapi.json in the repository.
Go to /docs
to see the automatic interactive API documentation.
Go to /redoc
to see the ReDoc documentation.
All application settings are stored in .env
file.
See start.sh
for deployment configuration and set corresponding variable via .env
file if needed.
The most important variables (with default values):
LOG_LEVEL=info
ACCESS_LOGFILE="-"
ERROR_LOGFILE="-"
HOST=0.0.0.0
PORT=80
KEEP_ALIVE=5
TIMEOUT=120
GRACEFUL_TIMEOUT=120
WORKERS_PER_CORE=1
MAX_WORKERS=
WEB_CONCURRENCY=
This service has an auto-tuning mechanism to achieve the best performance.
However it's possible to tune the server manually via WEB_CONCURRENCY
and other environmental variables.
See start.sh
for more details.
- Install Poetry
- Install all dependencies
poetry install
- Run test suite
poetry run pytest --cov=app
- Verify all tests passed
============================ test session starts ============================ platform darwin -- Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 rootdir: /Users/xsx/+dev/projects/currencies-converter-rest-service-fastapi, configfile: pyproject.toml plugins: mock-3.3.1, cov-2.10.1, asyncio-0.14.0 collected 525 items tests/test_actions.py ............................... [ 5%] tests/test_api.py ................................................... [ 15%] ..................................................................... [ 28%] ..................................................................... [ 41%] ..................................................................... [ 55%] ..................................................................... [ 68%] ..................................................................... [ 81%] ............................................................ [ 92%] tests/test_cache.py ..... [ 93%] tests/test_celerybeat.py . [ 93%] tests/test_celerytasks.py ..... [ 94%] tests/test_schemas.py .... [ 95%] tests/test_validation.py .................... [ 99%] tests/helpers/test/test_supported_currencies.py ... [100%] ---------- coverage: platform darwin, python 3.8.6-final-0 ----------- Name Stmts Miss Cover ---------------------------------------- app/__init__.py 0 0 100% app/actions.py 16 0 100% app/api.py 8 0 100% app/cache.py 12 0 100% app/celerybeat.py 17 1 94% app/celerytasks.py 40 0 100% app/main.py 13 3 77% app/schemas.py 20 0 100% app/settings.py 8 0 100% ---------------------------------------- TOTAL 134 4 97% =========================== 525 passed in 10.17s ============================
- Test suite contains external services tests that can consume you quota.
To run test suite excluding external tests
poetry run pytest -m "not ext" --cov=app
- Setup
COMPOSE_FILE
(e.g. via.env
file) or run all compose files explicitlydocker-compose -f compose-docker.yml -f compose-docker-dev.yml up --build
- Happy hacking!
Distributed under the MIT License. See LICENSE for more information.
Bart Skowron - bxsx - [email protected]
https://github.com/bxsx/currencies-converter-rest-service-fastapi