Fastapi: Automatically generation of client

Created on 31 Oct 2019  路  10Comments  路  Source: tiangolo/fastapi

Description

How can I generate client for my service automatically?

Is it possible to contract for generate of API?

question

Most helpful comment

I wrote a blog post on how to automatically generate openapi clients. The post shows a angular client but it could use any other openapitools generator.

https://dev.to/mxab/generating-openapi-clients-for-fastapi-via-gradle-14dj

All 10 comments

You can use openapi-generator

If your client is Python, I would try with @dmontagu's https://github.com/dmontagu/fastapi_client

Okay. Additional I use nodejs, in particular vuejs for development. Is there client for this cases? If there is not client, how I can do this? Could you get me some advices?

@Hedgehogues I believe there is a section of the openapi-generator README.md that discusses how to use the JavaScript client. There are also typescript client generators. My recommendation would be to go through the docs and look for examples using an openapi client in JavaScript.

@Hedgehogues Here's an example for how I use it with their docker image to get you started

  • host network is only requried if you want to generate against your docker host machines localhost.
  • You can get help output for the specific implementation chosen with the -g flag.
  • Each client is it's own project so read through the generated code, mileage may vary.
docker run --network=host --rm -v .:/tmp openapitools/openapi-generator-cli:v4.0.0 generate \
    -i https://localhost:8080/openapi.json \
    -o tmp/YourApiThingie \
    -D modelDocs=false \
    -D apiDocs=false \
    -D apiTests=false \
    -D modelTests=false \
    -D supportsES6=true \
    -D npmVersion=3.5.2 \
    -g typescript-node \
    --additional-properties supportsES6=true \
    --skip-validate-spec

Oh and you need to run your FastAPI instance with docs=True, or use -i from a file you mount into the container.

@henriklindgren

Thank you for your response. But I have a problem with your docker string:

docker: Error response from daemon: create .: volume name is too short, names should be at least two alphanumeric characters.
See 'docker run --help'.

@Hedgehogues It seems docker has issues with single character volume mounts, not sure if it's only under osx, if you try expanding . (dot) to the full path of the directory I think you will get past the issue

@Hedgehogues you can probably modify @henriklindgren's Docker call with something like:

docker run --network=host --rm -v $(pwd):/tmp openapitools/openapi-generator-cli:v4.0.0 generate \
    -i https://localhost:8080/openapi.json \
    -o /tmp/YourApiThingie \
    -D modelDocs=false \
    -D apiDocs=false \
    -D apiTests=false \
    -D modelTests=false \
    -D supportsES6=true \
    -D npmVersion=3.5.2 \
    -g typescript-node \
    --additional-properties supportsES6=true \
    --skip-validate-spec

But double-check that, I didn't test it myself.

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

I wrote a blog post on how to automatically generate openapi clients. The post shows a angular client but it could use any other openapitools generator.

https://dev.to/mxab/generating-openapi-clients-for-fastapi-via-gradle-14dj

Was this page helpful?
0 / 5 - 0 ratings