openapiopenapi-generatoropenapi-generator-cli

Is an independent OpenAPI client required for each endpoint (URL)?


Summary:

Do I need to install an independent OpenAPI client for each endpoint URL?

Details:

I am using an OpenAPI generator to create Python clients with the purpose of receiving data from multiple end points. For example, the OpenAPI endpoint URLs could be called:

https://some-url.com/swagger/yaml/User.yaml
https://some-url.com/swagger/yaml/Data.yaml

If relevant, these endpoints are based on OpenAPI Specification (OAS) v3.1.

As far as I understand from the official OpenAPI guideline, I am supposed to take the following steps, per URL:

As I have two URLs, I am assigning a unique name to each package, so that they both can be installed within the same venv virtual environemt via:

java -jar openapi-generator-cli.jar generate \
    --input-spec https://some-url.com/swagger/yaml/User.yaml \
    --additional-properties packageName=openapi_user \
    --generator-name python \
    --output ./generated_openapi_user
java -jar openapi-generator-cli.jar generate \
    --input-spec https://some-url.com/swagger/yaml/Data.yaml \
    --additional-properties packageName=**openapi_data** \
    --generator-name python \
    --output ./generated_openapi_data

While this works, I feel that I am not doing this right, or as expected. Installing a package for each URL seems a bit much. I wondered if this is the right way because such a task seems to be much more straighforward in R where you only need a single package to communicate with multiple entries.

As I am new to OpenAPI, I would like to hear what is the best approach to do this.


Solution

  • I think you are doing in right way. Some big systems provide multiple OpenAPI specs for different purposes (or modules, User and Data in your case). So we generate multiple Python client packages apart.

    Try github.com/sennder/python-client-generator. It generate a Python package from an OpenAPI spec. In your case, you can generate user and data clients to ./src/openapi-clients/user/ and ./src/openapi-clients/data/ respectively with the --output parameter. And you can import them directly.