python swagger 客户端设置主机和端口

python swagger client set host and port

如何在 swagger-codegen 自动生成的 python 客户端库中指定主机和端口?

我找到的唯一文档是 here

目标主机在客户端代码中得到hard-coded,例如:

https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/python/petstore_api/configuration.py#L50

# configuration.py

...

def __init__(self):
  """Constructor"""
  # Default Base url
  self.host = "http://petstore.swagger.io:80/v2"

有点旧,但由于这是我在搜索时首先登陆的地方,我将提供我的解决方案:

configuration = swagger_client.Configuration()
configuration.host = 'http://127.0.0.1:8000'
api_client = swagger_client.ApiClient(configuration=configuration)
api_instance = swagger_client.DefaultApi(api_client=api_client)

但是由于配置是硬编码的,对于我们的项目,我最终可能会针对不同的环境(暂存、生产等)使用不同的客户端。