Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmontagu/fastapi_client
FastAPI client generator
https://github.com/dmontagu/fastapi_client
Last synced: 6 days ago
JSON representation
FastAPI client generator
- Host: GitHub
- URL: https://github.com/dmontagu/fastapi_client
- Owner: dmontagu
- License: apache-2.0
- Created: 2019-08-03T10:35:23.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-06T21:45:13.000Z (over 1 year ago)
- Last Synced: 2024-10-18T12:21:04.135Z (3 months ago)
- Language: Python
- Size: 125 KB
- Stars: 333
- Watchers: 8
- Forks: 44
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE.TXT
Awesome Lists containing this project
- awesome-fastapi - FastAPI Client Generator - Generate a mypy- and IDE-friendly API client from an OpenAPI spec. (Third-Party Extensions / Developer Tools)
- awesome-fastapi - FastAPI Client Generator - Generate a mypy- and IDE-friendly API client from an OpenAPI spec. (Third-Party Extensions / Developer Tools)
README
# 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-generatorLook 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
```python
from client.api_client import ApiClient, AsyncApis, SyncApis
from client.models import Petclient = 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
```bash
./scripts/generate.sh -i -p -o
[-n ] [--include-auth]
[--] [*openapi-generator-args]
```
and will produce a client library at `/`.The OpenAPI json input can be either a URL or a local file path.
For example, running
```bash
./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](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach).
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!