Giter Site home page Giter Site logo

aurelien-clu / template-python-fast-api Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 49 KB

API Python template with FastAPI, Pydantic, Dependency Injector & BDD tests.

License: The Unlicense

Makefile 5.44% Python 90.37% Gherkin 4.18%
bdd dependency-injection fastapi python template poetry pyenv

template-python-fast-api's Introduction

python-fast-api-template

Python FastAPI Pydantic DependencyInjector

Linter Behave

Getting Started

Pre-requisites

Setup

# skip if python 3.9 is already installed with or without pyenv
pyenv install 3.9.10

# update path to your own python 3.9 installation
poetry env use ~/.pyenv/versions/3.9.10/bin/python3.9

# install packages
poetry install

Usage

# format automatically code
make fmt

# run tests
make test

# formats code and run with reloading
make run_dev

# run
make run

API Documentation

How to add new routes?

If you wanted to add POST /x/y you would do:

Implement your new service

Implement your new service at src/svc/x.py.

You can define your YRequest and YResponse using pydantic in src/models/x.py for instance.

If your service has custom exceptions, add them there: src/errors.py.

If your service has a configuration:

  • copy src/config/server.py into src/config/x.py and update it

(Optional) Implement your new infra

Implement repositories, external caches, etc. under src/infra if your service requires some.

If your infra has a configuration:

  • copy src/config/server.py into src/config/my_shiny_infra.py and update it

(e.g. database credentials)

Add the new routes

Now we make your service accessible to the outside.

Copy src/api/health.py into src/api/x.py and update it to use your service.

Add your new router to src/app.py:

# [...]
import src.api.x as api_x

def create_app() -> FastAPI:
    # [...]
    app.include_router(api_x.router)
    # [...]

If your endpoint brings new custom exceptions, then update src/main.py to add an exception_handler for every new exception.

Update the dependency injection

We update the dependency injection so your endpoints will be able to use your new services & new infra (if you made one).

Update the src/container.py:

# [...]
class Container(containers.DeclarativeContainer):
    wiring_config = containers.WiringConfiguration(
        modules=[
            "src.api.health",
            "src.api.x",  # TODO: add your new module
        ]
    )
    
    # config
    # [...]
    # TODO: add your new config if you have one, e.g.
    x_config: XConfig = providers.Singleton(XConfig)

    # infra
    # TODO: setup your new infra if you have some

    # services
    # TODO: setup your new service
    x: XService = providers.Singleton(
        XService,
        cfg=x_config,
    )
    # [...]

Add BDD tests

Copy tests/bdd/health.feature into tests/bdd/x.feature and update it to use your new routes.

If you need custom steps, add them under tests/bdd/steps.

make test
Name                     Stmts   Miss  Cover   Missing
------------------------------------------------------
src/__init__.py              6      0   100%
src/api/__init__.py          0      0   100%
src/api/health.py           25      7    72%   11-13, 19, 25, 31, 37
src/app.py                  21      1    95%   25
src/config/__init__.py       0      0   100%
src/config/server.py         9      0   100%
src/container.py             7      0   100%
src/errors.py                8      3    62%   3, 6, 11
src/infra/__init__.py        0      0   100%
src/main.py                 23     10    57%   15-16, 21-22, 27-32
src/svc/__init__.py          0      0   100%
src/svc/health.py           10      1    90%   7
------------------------------------------------------
TOTAL                      109     22    80%

Coverage is only 80% because an example exception is not used

๐ŸŽ‰

template-python-fast-api's People

Watchers

 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.