Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python swagger-codegen hard-coded base URL #7804

Open
boompig opened this issue Mar 9, 2018 · 5 comments
Open

Python swagger-codegen hard-coded base URL #7804

boompig opened this issue Mar 9, 2018 · 5 comments

Comments

@boompig
Copy link

boompig commented Mar 9, 2018

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).

@wing328
Copy link
Contributor

wing328 commented Mar 9, 2018

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

@cbornet
Copy link
Contributor

cbornet commented Mar 12, 2018

Yes it can be configured at runtime

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

@klarose
Copy link

klarose commented Oct 31, 2018

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?

@ghost
Copy link

ghost commented Apr 17, 2019

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.

@rnwolfe
Copy link

rnwolfe commented Apr 20, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants