Swagger-codegen: Python swagger-codegen hard-coded base URL

Created on 9 Mar 2018  路  5Comments  路  Source: swagger-api/swagger-codegen

Description

Swagger API client for python should allow for the base URL to be passed via arguments at runtime

Swagger-codegen version

2.3.1

Swagger declaration file content or url

Any, can use petstore.

Command line used for generation

swagger-codegen generate -i swagger.yaml -l python -o client-lib

Steps to reproduce

Cannot pass URL to client library

Related issues/PRs
Suggest a fix/enhancement

Instead of hard-coding the base URL like here, instead pass it as a parameter to __init__ in Configuration (or anywhere else that makes sense).

Python Question

Most helpful comment

Yes it can be configured at runtime

api_client = ApiClient()
api_client.configuration.host = "http://whatever"

All 5 comments

I think you can change the base path in the python client during runtime.

Yes it can be configured at runtime

api_client = ApiClient()
api_client.configuration.host = "http://whatever"

I do not like this particular approach. The reason? As an API author, I have an API where I encode a version/etc into the basepath so that the server can handle multiple versions concurrently/explicitly not handle unsupported versions.

If I bump the version of the API, I expect the version in the basepath to increase as well.

As a user of the client, I, for the most part, do not care about the details of the path to the API. I only care about where the API server is located. However, because the host includes both the base path and the host, I need to know the version/etc in order to construct a new host. This means that every time the API version changes, I need to update the code using the client library. The act of using the new version of the library should be sufficient, since it should be abstracting such details.

For example, my client has the following line in Configuration()'s constructor:

self.host = "https://localhost/my_api/1.0.0"

I tried changing host to something else with the expectation that the client would just work.

config = client_lib.Configuration()
config.host = "https://my-new-host"

However, I get 404s when I try to use the API. Looking at the server logs, I see accesses to stuff like /resource where I would expect /my_api/1.0.0/resource.

Right now the url is constructed as follows:

        url = self.configuration.host + resource_path

I think that the host should actually be the host, and the base path something separate. I.e.

url = self.configuration.host + "/" + self.configuration.base_path + resource_path

The base_path would default to /my_api/1.0.0 in my case.

Does that seem reasonable?

https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/

The problem is that codegen is made to generate code. It is not responsible for environment... however, I strongly agree to updating the python code so it take a parameter to set the host url.

As an alternative, a hosts entry can solve what this ticket is providing.

Has any change to this behavior occurred? If not, I agree with needing to ask for hostname and port as a required argument when initializing the API client.

I've recently begun modeling APIs with OAS3 for products that are explicitly and exclusively deployed on-premise. This means there is no SaaS to point to for a baseUrl, and that hardcoding one is not useful as it will _always_ result in error. Therefore, the API client should require a host/port (socket) to be passed to the API client at runtime.

I see where this can be modified in the Configuration class (for example, in the generated python library); however, I would much prefer to do it programmatically in the generator.

Was this page helpful?
0 / 5 - 0 ratings