FastAPI application and service structure for a more maintainable codebase

Overview

Abstracting FastAPI Services

See this article for more information: https://camillovisini.com/article/abstracting-fastapi-services/

Poetry

poetry install

Makefile

make dev # run uvicorn with restart
make test # run pytest
make create-items # create items for illustration purposes
make get-items # retrieve items for illustration purposes
Comments
  • `make dev` throws `[FileNotFoundError] [Errno 2] No such file or directory`

    `make dev` throws `[FileNotFoundError] [Errno 2] No such file or directory`

    Edit: The following command...

    poetry -vvv run uvicorn main:app --reload

    throws:

    [NoSuchOptionException]
    The "--reload" option does not exist.
    
    Traceback (most recent call last):
      File "/usr/lib/python3.9/site-packages/clikit/console_application.py", line 123, in run
        io = io_factory(
      File "/usr/lib/python3.9/site-packages/poetry/console/config/application_config.py", line 168, in create_io
        resolved_command = application.resolve_command(args)
      File "/usr/lib/python3.9/site-packages/clikit/console_application.py", line 110, in resolve_command
        return self._config.command_resolver.resolve(args, self)
      File "/usr/lib/python3.9/site-packages/clikit/resolver/default_resolver.py", line 43, in resolve
        result = self.process_default_commands(args, application.default_commands)
      File "/usr/lib/python3.9/site-packages/clikit/resolver/default_resolver.py", line 104, in process_default_commands
        if resolved_command.is_parsable():
      File "/usr/lib/python3.9/site-packages/clikit/resolver/resolve_result.py", line 43, in is_parsable
        self._parse()
      File "/usr/lib/python3.9/site-packages/clikit/resolver/resolve_result.py", line 49, in _parse
        self._parsed_args = self._command.parse(self._raw_args)
      File "/usr/lib/python3.9/site-packages/clikit/api/command/command.py", line 113, in parse
        return self._config.args_parser.parse(args, self._args_format, lenient)
      File "/usr/lib/python3.9/site-packages/clikit/args/default_args_parser.py", line 53, in parse
        self._parse(args, _fmt, lenient)
      File "/usr/lib/python3.9/site-packages/clikit/args/default_args_parser.py", line 101, in _parse
        self._parse_long_option(token, tokens, fmt, lenient)
      File "/usr/lib/python3.9/site-packages/clikit/args/default_args_parser.py", line 247, in _parse_long_option
        self._add_long_option(name, None, tokens, fmt, lenient)
      File "/usr/lib/python3.9/site-packages/clikit/args/default_args_parser.py", line 300, in _add_long_option
        raise NoSuchOptionException(name)
    

    And regarding the environment... poetry env info throws:

    Virtualenv
    Python:         3.9.1
    Implementation: CPython
    Path:           ~/.cache/pypoetry/virtualenvs/fastapi-service-2hjJIUGa-py3.9
    Valid:          True
    
    System
    Platform: linux
    OS:       posix
    Python:   /usr
    
    opened by fchpr 5
  • Purpose of ServiceResult

    Purpose of ServiceResult

    When looking through the proposed design in the article, I was a little confused about the purpose of the ServiceResult class. My We already have Response and HTTPException from FastAPI/Starlette which seem to fulfill the same purpose. What benefit does having a dedicated ServiceResult provide? (for instance, what is a case where we would want to use __enter__ and __exit__ in a result rather than performing the behavior directly?)

    opened by MatthewScholefield 4
  • Why a single app_exceptions.py?

    Why a single app_exceptions.py?

    I assume it's nicer to have the ./exceptions/ directory and put each service exception module containing relevant error data.

    And if you were to add a new service called Bar:

    ./routers/      bar.py # Router instance and routes
    ./services/     bar.py # Business logic (including CRUD helpers)
    ./schemas/      bar.py # Data "schemas" (e.g., Pydantic models)
    ./models/       bar.py # Database models (e.g., SQLAlchemy models)
    ./exceptions/   bar.py # Exceptoins classes completely decoupled
    
    opened by hosseinsokhandan 0
  • [BUG-Fix]: Fixed update create-items command

    [BUG-Fix]: Fixed update create-items command

    Hello there! I'm using MacBook M1 with cURL: 7.77.0, and without explicitly adding the json header, the server rejects the request with the following error message:

    {"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}

    Problem solved by adding: Content-Type: application/json

    opened by AmitArie 0
  • Example Project ?

    Example Project ?

    @visini ,

    Loved the article accompanying this repository, Do you have a sample project either that you compiled or have referred to that follows this approach. I am trying to follow this approach but would greatly benefit from prototyping with an example to relate to it. Could you kindly share anything if available ?

    Thanks

    opened by UGuntupalli 1
A FastAPI Middleware of joerick/pyinstrument to check your service performance.

fastapi_profiler A FastAPI Middleware of joerick/pyinstrument to check your service performance. ?? Info A FastAPI Middleware of pyinstrument to check

LeoSun 107 Jan 5, 2023
SuperSaaSFastAPI - Python SaaS Boilerplate for building Software-as-Service (SAAS) apps with FastAPI, Vue.js & Tailwind

Python SaaS Boilerplate for building Software-as-Service (SAAS) apps with FastAP

Rudy Bekker 31 Jan 10, 2023
FastAPI-Amis-Admin is a high-performance, efficient and easily extensible FastAPI admin framework. Inspired by django-admin, and has as many powerful functions as django-admin.

简体中文 | English 项目介绍 FastAPI-Amis-Admin fastapi-amis-admin是一个拥有高性能,高效率,易拓展的fastapi管理后台框架. 启发自Django-Admin,并且拥有不逊色于Django-Admin的强大功能. 源码 · 在线演示 · 文档 · 文

AmisAdmin 318 Dec 31, 2022
fastapi-admin2 is an upgraded fastapi-admin, that supports ORM dialects, true Dependency Injection and extendability

FastAPI2 Admin Introduction fastapi-admin2 is an upgraded fastapi-admin, that supports ORM dialects, true Dependency Injection and extendability. Now

Glib 14 Dec 5, 2022
:rocket: CLI tool for FastAPI. Generating new FastAPI projects & boilerplates made easy.

Project generator and manager for FastAPI. Source Code: View it on Github Features ?? Creates customizable project boilerplate. Creates customizable a

Yagiz Degirmenci 1k Jan 2, 2023
Simple FastAPI Example : Blog API using FastAPI : Beginner Friendly

fastapi_blog FastAPI : Simple Blog API with CRUD operation Steps to run the project: git clone https://github.com/mrAvi07/fastapi_blog.git cd fastapi-

Avinash Alanjkar 1 Oct 8, 2022
Пример использования GraphQL Ariadne с FastAPI и сравнение его с GraphQL Graphene FastAPI

FastAPI Ariadne Example Пример использования GraphQL Ariadne с FastAPI и сравнение его с GraphQL Graphene FastAPI - GitHub ###Запуск на локальном окру

ZeBrains Team 9 Nov 10, 2022
Sample-fastapi - A sample app using Fastapi that you can deploy on App Platform

Getting Started We provide a sample app using Fastapi that you can deploy on App

Erhan BÜTE 2 Jan 17, 2022
Flask-vs-FastAPI - Understanding Flask vs FastAPI Web Framework. A comparison of two different RestAPI frameworks.

Flask-vs-FastAPI Understanding Flask vs FastAPI Web Framework. A comparison of two different RestAPI frameworks. IntroductionIn Flask is a popular mic

Mithlesh Navlakhe 1 Jan 1, 2022
FastAPI Server Session is a dependency-based extension for FastAPI that adds support for server-sided session management

FastAPI Server-sided Session FastAPI Server Session is a dependency-based extension for FastAPI that adds support for server-sided session management.

DevGuyAhnaf 5 Dec 23, 2022
Code Specialist 27 Oct 16, 2022
Fastapi-ml-template - Fastapi ml template with python

FastAPI ML Template Run Web API Local $ sh run.sh # poetry run uvicorn app.mai

Yuki Okuda 29 Nov 20, 2022
Sample project showing reliable data ingestion application using FastAPI and dramatiq

Create and deploy a reliable data ingestion service with FastAPI, SQLModel and Dramatiq This is the source code for the data ingestion service explain

François Voron 31 Nov 30, 2022
A server hosts a FastAPI application and multiple clients can be connected to it via SocketIO.

FastAPI_and_SocketIO A server hosts a FastAPI application and multiple clients can be connected to it via SocketIO. Executing server.py sets up the se

Ankit Rana 2 Mar 4, 2022
A FastAPI WebSocket application that makes use of ncellapp package by @hemantapkh

ncellFastAPI author: @awebisam Used FastAPI to create WS application. Ncellapp module by @hemantapkh NOTE: Not following best practices and, needs ref

Aashish Bhandari 7 Oct 1, 2021
Learn to deploy a FastAPI application into production DigitalOcean App Platform

Learn to deploy a FastAPI application into production DigitalOcean App Platform. This is a microservice for our Try Django 3.2 project. The goal is to extract any and all text from images using a technique called OCR.

Coding For Entrepreneurs 59 Nov 29, 2022
Hook Slinger acts as a simple service that lets you send, retry, and manage event-triggered POST requests, aka webhooks

Hook Slinger acts as a simple service that lets you send, retry, and manage event-triggered POST requests, aka webhooks. It provides a fully self-contained docker image that is easy to orchestrate, manage, and scale.

Redowan Delowar 96 Jan 2, 2023
A RESTful API for creating and monitoring resource components of a hypothetical build system. Built with FastAPI and pydantic. Complete with testing and CI.

diskspace-monitor-CRUD Background The build system is part of a large environment with a multitude of different components. Many of the components hav

Nick Hopewell 67 Dec 14, 2022
Cube-CRUD is a simple example of a REST API CRUD in a context of rubik's cube review service.

Cube-CRUD is a simple example of a REST API CRUD in a context of rubik's cube review service. It uses Sqlalchemy ORM to manage the connection and database operations.

Sebastian Andrade 1 Dec 11, 2021