Giter Club home page Giter Club logo

currencies-converter-rest-service-fastapi's Introduction

Currencies converter REST service

shields-swagger shields-tests shields-coverage shields-commit shields-python shields-lang shields-black shields-license

Main features

  • 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

Endpoints

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.

Getting Started

Prerequisites

  • Docker (if you want to make your life easier)
  • Poetry (for development only)

Installation

  1. Get a free API Key at http://openexchangerates.org
  2. Clone the repo
    git clone https://github.com/bxsx/currencies-converter-rest-service-fastapi.git
  3. Go to the project directory
    cd currencies-converter-rest-service-fastapi/
  4. Setup your .env, you can use .env-dev as example (NOTE: .env-dev is not production-ready)
    cp .env-dev .env
  5. Enter your API key (abcdef123456 here) in .env
    sed -i -e 's/OXR_ID=.*/OXR_ID=abcdef123456/' .env
  6. Run the service
    docker-compose up --build

Development vs production version

.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)

Usage

General usage

curl -X GET "$HOST/api/currencies/convert/{base}/{to}/{amount}" -H  "accept: application/json"

Example request (assuming .env-dev settings are used)

curl -X GET "http://0.0.0.0/api/currencies/convert/USD/EUR/123" -H  "accept: application/json"
Response (Code 200)
{
  "base": "USD",
  "to": "EUR",
  "amount": 123,
  "exchange_rate": {
    "quote": 0.836134,
    "timestamp": "2020-11-29T21:00:02+00:00"
  },
  "result": 102.844482
}

Documentation

OpenAPI schema

Go to /openapi.json to get the current OpenAPI JSON schema. Or view already generated openapi.json in the repository.

Interactive API documentation (provided by Swagger UI)

Go to /docs to see the automatic interactive API documentation.

swagger

ReDoc

Go to /redoc to see the ReDoc documentation.

redoc

Configuration

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=

Auto-tune web server

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.

Tests and development

  1. Install Poetry
  2. Install all dependencies
    poetry install
  3. Run test suite
    poetry run pytest --cov=app
  4. 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 ============================
    
  5. 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
  6. Setup COMPOSE_FILE (e.g. via .env file) or run all compose files explicitly
    docker-compose -f compose-docker.yml -f compose-docker-dev.yml up --build
  7. Happy hacking!

Built With

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Bart Skowron - bxsx - [email protected]

https://github.com/bxsx/currencies-converter-rest-service-fastapi

currencies-converter-rest-service-fastapi's People

Contributors

bxsx avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.