Giter Site home page Giter Site logo

ink-splatters / fastapi_client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dmontagu/fastapi_client

0.0 1.0 0.0 140 KB

FastAPI client generator

License: Apache License 2.0

Dockerfile 0.44% Python 61.77% Shell 15.91% Makefile 3.42% Mustache 18.46%

fastapi_client's Introduction

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 
    

With package metadata

If you want generate not only a code, but also a package metadata (e.g. setup.py) for publishing or distributing autogenerated client you can use a --with-meta flag.

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!

fastapi_client's People

Contributors

b0g3r avatar christophelallement avatar daveholladay avatar dmontagu avatar fj-sanchez avatar ink-splatters avatar toumetis avatar

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.