FastAPI client generator

Overview

FastAPI-based API Client Generator

Generate a mypy- and IDE-friendly API client from an OpenAPI spec.

  • Sync and async interfaces are both available
  • Comes with support for the OAuth2.0 password flow
  • Easily extended, with built-in support for request middleware
  • Designed for integration with FastAPI, but should work with most OpenAPI specs
  • Makes use of https://github.com/OpenAPITools/openapi-generator

Look inside example/client to see an example of the generated output!


Warning: This is still in the proof-of-concept phase, and should not yet be considered to have a stable interface.

  • Some OpenAPI features (like discriminator fields) are not yet supported.
  • While the goal is to support any OpenAPI spec, it is most likely to work well with specs generated by FastAPI.

If you try this out, please help me by reporting any issues you notice!

Client library usage

from client.api_client import ApiClient, AsyncApis, SyncApis
from client.models import Pet

client = ApiClient(host="http://localhost")
sync_apis = SyncApis(client)
async_apis = AsyncApis(client)

pet_1 = sync_apis.pet_api.get_pet_by_id(pet_id=1)
assert isinstance(pet_1, Pet)

async def get_pet_2() -> Pet:
    pet_2 = await async_apis.pet_api.get_pet_by_id(pet_id=2)
    assert isinstance(pet_2, Pet)
    return pet_2

The example generated client library is contained in example/client.

Generated clients will have the following dependencies:

  • pydantic for models
  • httpx for networking
  • fastapi for jsonable_encoder and OAuth models (I hope to eventually remove this as a dependency)
  • typing_extensions for Enums via Literal (I eventually hope to replace this with standard enums)

More examples of usage (including auth) are contained in example/usage_example.py.

Generating the client library

Using the generator looks like

./scripts/generate.sh -i <openapi_json> -p <package_name> -o <output_path>
  [-n <import_name>] [--include-auth]
  [--] [*openapi-generator-args]

and will produce a client library at <output_path>/<package_name>.

The OpenAPI json input can be either a URL or a local file path.

For example, running

./scripts/generate.sh \
  -i https://petstore.swagger.io/v2/swagger.json \
  -p client \
  -o generated \
  -n example.client \
  --include-auth

produces the example client (along with the OAuth2.0 password flow client), places it in generated/client, and makes any generated client-referencing imports start with example.client.

(Note: to prevent accidental overwrites, you would need to manually remove generated/client if it already exists.)

With FastAPI

  • To generate a client for a default FastAPI app running on localhost (NOT inside a docker container):

      ./scripts/generate.sh -i http://localhost/openapi.json -p my_client -o generated
    
  • Since the generator runs inside docker, if your server is also running in a docker container on the same machine, you may need to provide a special hostname. Passing the --map-localhost argument will make the script attempt to perform this automatically:

      ./scripts/generate.sh --map-localhost -i http://localhost/openapi.json -p my_client -o generated
      # Transforms the input to http://host.docker.internal/openapi.json 
    

Generation details

  • The only local dependencies for generation are docker and standard command line tools.
  • openapi-generator is used to generate the code from the openapi spec
    • The custom templates are located in openapi-python-templates
  • autoflake, isort, and black are used to format the code after generation

Contributing

There are a variety of make rules for setup/testing; here are some highlights:

  • make develop: Sets up the local development environment.
  • make regenerate: Regenerates the example client from the example's openapi.json and the templates.
    • Note: This will overwrite changes! Make sure you commit (or edit the templates) before running this.
  • make: Checks that isort, black, flake8, mypy, and pytest all pass
  • make testcov: Generates a coverage report for the tests.

Pull requests are welcome and appreciated!

Owner
David Montague
David Montague
Farlimit - FastAPI rate limit with python

FastAPIRateLimit Contributing is F&E (free&easy) Y Usage pip install farlimit N

omid 27 Oct 06, 2022
Basic fastapi blockchain - An api based blockchain with full functionality

Basic fastapi blockchain - An api based blockchain with full functionality

1 Nov 27, 2021
Browse JSON API in a HTML interface.

Falcon API Browse This project provides a middleware for Falcon Web Framework that will render the response in an HTML form for documentation purpose.

Abhilash Raj 4 Mar 16, 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
Town / City geolocations with FastAPI & Mongo

geolocations-api United Kingdom Town / City geolocations with FastAPI & Mongo Build container To build a custom image or extend the api run the follow

Joe Gasewicz 3 Jan 26, 2022
Adds simple SQLAlchemy support to FastAPI

FastAPI-SQLAlchemy FastAPI-SQLAlchemy provides a simple integration between FastAPI and SQLAlchemy in your application. It gives access to useful help

Michael Freeborn 465 Jan 07, 2023
flask extension for integration with the awesome pydantic package

flask extension for integration with the awesome pydantic package

249 Jan 06, 2023
Stac-fastapi built on Tile38 and Redis to support caching

stac-fastapi-caching Stac-fastapi built on Tile38 to support caching. This code is built on top of stac-fastapi-elasticsearch 0.1.0 with pyle38, a Pyt

Jonathan Healy 4 Apr 11, 2022
✨️🐍 SPARQL endpoint built with RDFLib to serve machine learning models, or any other logic implemented in Python

✨ SPARQL endpoint for RDFLib rdflib-endpoint is a SPARQL endpoint based on a RDFLib Graph to easily serve machine learning models, or any other logic

Vincent Emonet 27 Dec 19, 2022
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

Yasser Tahiri 16 Oct 07, 2022
Easily integrate socket.io with your FastAPI app 🚀

fastapi-socketio Easly integrate socket.io with your FastAPI app. Installation Install this plugin using pip: $ pip install fastapi-socketio Usage To

Srdjan Stankovic 210 Dec 23, 2022
Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Docker, automatic HTTPS and more.

Full Stack FastAPI and PostgreSQL - Base Project Generator Generate a backend and frontend stack using Python, including interactive API documentation

Sebastián Ramírez 10.8k Jan 08, 2023
Fastapi performans monitoring

Fastapi-performans-monitoring This project is a simple performance monitoring for FastAPI. License This project is licensed under the terms of the MIT

bilal alpaslan 11 Dec 31, 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
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 01, 2022
This code generator creates FastAPI app from an openapi file.

fastapi-code-generator This code generator creates FastAPI app from an openapi file. This project is an experimental phase. fastapi-code-generator use

Koudai Aono 632 Jan 05, 2023
FastAPI with async for generating QR codes and bolt11 for Lightning Addresses

sendsats An API for getting QR codes and Bolt11 Invoices from Lightning Addresses. Share anywhere; as a link for tips on a twitter profile, or via mes

Bitkarrot 12 Jan 07, 2023
Generate Class & Decorators for your FastAPI project ✨🚀

Classes and Decorators to use FastAPI with class based routing. In particular this allows you to construct an instance of a class and have methods of that instance be route handlers for FastAPI & Pyt

Yasser Tahiri 34 Oct 27, 2022
🐍 Simple FastAPI template with factory pattern architecture

Description This is a minimalistic and extensible FastAPI template that incorporates factory pattern architecture with divisional folder structure. It

Redowan Delowar 551 Dec 24, 2022
Reusable utilities for FastAPI

Reusable utilities for FastAPI Documentation: https://fastapi-utils.davidmontague.xyz Source Code: https://github.com/dmontagu/fastapi-utils FastAPI i

David Montague 1.3k Jan 04, 2023