/x-motion

Primary LanguagePython

Flask Factory

Flask App Template with Factory Pattern Architecture

zarak-khan-Ve2SL0zMhWk-unsplash

Architecture

Directory tree for serving multiple packages as Flask API with multiple endpoints

.
├── app
│   ├── api_a
│   │   ├── __init__.py
│   │   ├── main.py
│   │   ├── sub.py
│   │   └── views.py
│   ├── api_b
│   │   ├── __init__.py
│   │   ├── main.py
│   │   ├── sub.py
│   │   └── views.py
│   ├── flask_run.py
│   ├── __init__.py
│   ├── settings.py
│   └── tests
│       ├── __init__.py
│       └── test_api.py
├── .env
├── .gitignore
├── docker-compose.yml
├── Dockerfile
├── pyproject.toml
├── requirements-dev.txt
├── requirements.in
└── requirements.txt

4 directories, 21 files

Setup

  • Install docker and docker-compose on your machine.

  • Clone the repository & cd to the root directory.

Run

Run the docker container:

docker-compose up

The app will run locally in port 4000.

Check the API

While the above container is running, the API can be accessed via any API development client like Postman.

With CuRL

Run the following command to test the api with curl. Make sure you're sending X-Api-Key: 1234ABCD with the header:

curl -H "Accept: application/json" -H "Content-Type: application/json" -H "X-Api-Key: 1234ABCD" http://localhost:4000/api-a/123

It should return a response similar to this:

{
    "random_first":16,
    "random_second":73,
    "seed":123
}

With Python (Httpx)

Httpx is a modern and faster alternative to Python's revered requests library with similar public API.

  • Install httpx in your local environment:

    pip install httpx
    
  • Run:

    import httpx
    
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "X-Api-Key": "1234ABCD",
    }
    
    with httpx.Client() as client:
        response = client.get("http://localhost:4000/api-a/123", headers=headers)
        json_data = response.json()
    
    # Check successful status code.
    assert response.status_code == 200
    
    # Print JSON response.
    print(json_data)

Run the Tests

Run:

pytest

If you run the tests while the Docker container is running, pytest will run all the tests. However, pytest will only run the unit tests if you run them offline.

Stop the Container

Run:

docker-compose down

Remarks

This template is developed and tested on

  • Python 3.8
  • Docker version 19.03.13
  • docker-compose 1.26.2
  • Ubuntu 20.04 LTS