LuSyringe is a documentation injection tool for your classes when using Fast API

Overview
drawing

LuSyringe

LuSyringe is a documentation injection tool for your classes when using Fast API

 

Benefits

The main benefit is being able to separate your business code (classes) from the logic of the documentation and pydantic validation. For example, a class that serves as a response for an endpoint may look like this without LuSyringe:

class HealthResponse(BaseModel):
  ping: str = Field(..., example="pong")
  version: str = Field(..., example="1.0.0")

And that's not bad at first look, but the response class is tightly coupled of the logic of the validation + documentation offered by Pydantic and FastAPI. When dealing with inheritance, you may run into cases that the search for where the documentation is being defined is a bit harsh.

Well, with LuSyringe that's how the HealthReponse class would look.

from lusyringe import lusyringe

class HealthResponse(metaclass=lusyringe(health_response_docs)):
  ping: str
  version: str

Nice, isn't it? 🤩 . But hey, what about inheritance, what if I'm inheriting these fields from a base class, or what if this base class is inheriting these fields?

Hey, calm down. I solved all these things for you here, no need to worries 😝 , ah! I do type checking for you too, (but I also have a non-strict mode if you are adventurous) .

How it works

The usage is pretty simple 🥰 . You can define your docs object in the following manner:

from lusyringe import Prescription

health_response_docs = [
    Prescription(
        field='ping',
        type=str,
        doc=Field(..., example='Pong')
    ),
    Prescription(
        field='version',
        type=str,
        doc=Field(..., example='0.0.1')
    ),
]

Then you can pass your docs object to lusyringe, like this:

from lusyringe import lusyringe

# import your file
from ... import health_response_docs

class HealthResponse(metaclass=lusyringe(health_response_docs)):
  ping: str
  version: str

Cool, huh? I can throw some errors if you forget to define your fields in the class, or in a base class being inherited from 👮

NotImplementedError: f"Documentation for {field} with type {type_} was found,"
                     f"but field was not implemented in {class_name}"

So be a good developer and do not forget to declare your things. But hey! Remember when I called you adventurous? Yeah, I have a little surprise for you:

class HealthResponse(metaclass=lusyringe(health_response_docs, strict=False)):
  pass

What!? What does the strict mean? Well, basically I'll allow your recklessness in don't defining the fields, so I'll do it for you 🙄 . But I'll get mad if you declare something with the wrong type! So be in line.

ValueError: f"Tried to apply type {applied_type} to already defined {field}"
            f"of type {existent_type}"

Installing

Initially this will only be available for the folks at Luizalabs 😇 . But if you are from here, you can just:

pip install lusyringe

If you have our pypi

You might also like...
A fast and durable Pub/Sub channel over Websockets. FastAPI + WebSockets + PubSub ==  ⚡ 💪 ❤️
A fast and durable Pub/Sub channel over Websockets. FastAPI + WebSockets + PubSub == ⚡ 💪 ❤️

⚡ 🗞️ FastAPI Websocket Pub/Sub A fast and durable Pub/Sub channel over Websockets. The easiest way to create a live publish / subscribe multi-cast ov

Cookiecutter API for creating Custom Skills for Azure Search using Python and Docker

cookiecutter-spacy-fastapi Python cookiecutter API for quick deployments of spaCy models with FastAPI Azure Search The API interface is compatible wit

Restful Api developed with Flask using Prometheus and Grafana for monitoring and containerization with Docker :rocket:
Restful Api developed with Flask using Prometheus and Grafana for monitoring and containerization with Docker :rocket:

Hephaestus 🚀 In Greek mythology, Hephaestus was either the son of Zeus and Hera or he was Hera's parthenogenous child. ... As a smithing god, Hephaes

Boilerplate code for quick docker implementation of REST API with JWT Authentication using FastAPI, PostgreSQL and PgAdmin ⭐

FRDP Boilerplate code for quick docker implementation of REST API with JWT Authentication using FastAPI, PostgreSQL and PgAdmin ⛏ . Getting Started Fe

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-

API using python and Fastapi framework

Welcome 👋 CFCApi is a API DEVELOPMENT PROJECT UNDER CODE FOR COMMUNITY ! Project Walkthrough 🚀 CFCApi run on Python using FASTapi Framework Docs The

Lazy package to start your project using FastAPI✨
Lazy package to start your project using FastAPI✨

Fastapi-lazy 🦥 Utilities that you use in various projects made in FastAPI. Source Code: https://github.com/yezz123/fastapi-lazy Install the project:

api versioning for fastapi web applications

fastapi-versioning api versioning for fastapi web applications Installation pip install fastapi-versioning Examples from fastapi import FastAPI from f

api versioning for fastapi web applications

fastapi-versioning api versioning for fastapi web applications Installation pip install fastapi-versioning Examples from fastapi import FastAPI from f

Owner
Enzo Ferrari
Hey! I'm a Computer Engineering student @ SENAI CIMATEC. Also, I'm currently a Jr. Software Engineer @ Luizalabs
Enzo Ferrari
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
FastAPI Socket.io with first-class documentation using AsyncAPI

fastapi-sio Socket.io FastAPI integration library with first-class documentation using AsyncAPI The usage of the library is very familiar to the exper

Marián Hlaváč 9 Jan 2, 2023
Run your jupyter notebooks as a REST API endpoint. This isn't a jupyter server but rather just a way to run your notebooks as a REST API Endpoint.

Jupter Notebook REST API Run your jupyter notebooks as a REST API endpoint. This isn't a jupyter server but rather just a way to run your notebooks as

Invictify 54 Nov 4, 2022
Practice-python is a simple Fast api project for dealing with modern rest api technologies.

Practice Python Practice-python is a simple Fast api project for dealing with modern rest api technologies. Deployment with docker Go to the project r

null 0 Sep 19, 2022
MS Graph API authentication example with Fast API

MS Graph API authentication example with Fast API What it is & does This is a simple python service/webapp, using FastAPI with server side rendering,

Andrew Hart 4 Aug 11, 2022
Fast, simple API for Apple firmwares.

Loyal Fast, Simple API for fetching Apple Firmwares. The API server is closed due to some reasons. Wait for v2 releases. Features Fetching Signed IPSW

null 11 Oct 28, 2022
A simple Blogging Backend app created with Fast API

This is a simple blogging app backend built with FastAPI. This project is created to simulate a real CRUD blogging system. It is built to be used by s

Owusu Kelvin Clark 13 Mar 24, 2022
Dead simple CSRF security middleware for Starlette ⭐ and Fast API ⚡

csrf-starlette-fastapi Dead simple CSRF security middleware for Starlette ⭐ and Fast API ⚡ Will work with either a <input type="hidden"> field or ajax

Nathaniel Sabanski 9 Nov 20, 2022
An alternative implement of Imjad API | Imjad API 的开源替代

HibiAPI An alternative implement of Imjad API. Imjad API 的开源替代. 前言 由于Imjad API这是什么?使用人数过多, 致使调用超出限制, 所以本人希望提供一个开源替代来供社区进行自由的部署和使用, 从而减轻一部分该API的使用压力 优势

Mix Technology 450 Dec 29, 2022
Turns your Python functions into microservices with web API, interactive GUI, and more.

Instantly turn your Python functions into production-ready microservices. Deploy and access your services via HTTP API or interactive UI. Seamlessly export your services into portable, shareable, and executable files or Docker images.

Machine Learning Tooling 2.8k Jan 4, 2023