Skip to content

API REST services for Brokerage firm to process a set of buy/sell orders.

License

Notifications You must be signed in to change notification settings

rnovec/brokerage-api

Repository files navigation

Brokerage API

API REST services for Brokerage firm to process a set of buy/sell orders.

Features

  • FastAPI - FastAPI framework, high performance, easy to learn, fast to code, ready for production.
  • SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper with SQLite3 implementation.
  • Pytest - The pytest framework makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and libraries.
  • Mangum - Mangum is an adapter for running ASGI applications in AWS Lambda to handle Function URL, API Gateway, ALB, and Lambda@Edge events.
  • Pyscopg2 - Python interface to PostgreSQL database server.

Requirements

  • Python 3.8+
  • Postgres 12.0
  • Docker
  • Docker Compose

Environment variables

  • DATABASE_URL - database uri (postgresql+pyscopg2://user:password@host:port/database)
  • OPENAPI_URL - relative path to openapi.json, default is /openapi.json
  • OPEN_MARKET_AT - open market time, default is 06:00
  • CLOSE_MARKET_AT - close market time, default is 15:00

Disclaimer

There is currently a lot of flexibility with the installation.

Many of the developers use docker compose with visual studio code to test the functionality of the backend in a way that is similar to our deployed environments. Details regarding docker setup are listed later in this document.

You will need to install Postgres and this can be done with docker, a direct system install, or any other typical Postgres install.

These instructions will try to cover setup for both Windows and Linux/Mac.

Install package requirements

For the next 2 commands, replace python with the full path to your Python 3.8+ installation.

  • Install virtualenv
python -m pip install --user virtualenv
  • Create environment
python -m virtualenv venv # or python -m venv venv
  • Activate your virtual environment

Windows

venv/Scripts/activate.ps1

Mac/Linux

source venv/bin/activate

You should now be in the virtual environment and see the (venv) tag as part of your prompt. If you are not using Python 3.8+, the next command will fail.

  • Install package requirements
pip install -r etc/pip/requirements-dev.txt

Please keep in mind that for any local system commands that you run regarding this project and Python, you need to be in the virtual environment. Always re-enter the virtual environment before trying to do anything.

Run locally

Start dev server on your local machine:

uvicorn app.main:app --reload 

Then open your browser to http://localhost:8000/docs

Run Formatting

We are using the black https://pypi.org/project/black/ and isort auto-formatting tools.

Usage: black dir_name and/or isort dir_name to format the code.

isort app && black app

Docker Setup

Docker allows for development within an environment that is similar to our dev and production environments in ECS.

Install Docker

To install docker, Docker desktop is recommended for Windows and Mac. https://www.docker.com/products/docker-desktop

Run Docker Compose

Navigate to the location of the docker-compose.yml file and run the following command.

docker-compose up

Open Swagger docs

Run tests

docker-compose run --rm api pytest

Directory Structure

The following is an overview of the directory structures.

Deployment and build related files can be found in the etc folder.

  • etc\docker contains docker files.
  • etc\pip contains requirements.txt files for python requirements

App folder

Source code is located in the app folder.

  • app\api contains the API versioned code.
  • app\core contains the database configuration, models and schemas.
  • app\common contains global utilities, constants and exceptions.
  • app\tests contains the unit and integration tests.
  • app\main.py is the entry point for the application.
  • app\settings.py contains the global settings for the project.

Live example

Walk through the live example and test the API:

Author ✒

About

API REST services for Brokerage firm to process a set of buy/sell orders.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published