简体   繁体   中英

python swagger client set host and port

How should you specify host and port in the python client library auto-generated by swagger-codegen ?

The only documentation that I found is here .

Bit of an old one but since this is where I landed first when searching, I'll provide my solution:

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)

But since the configuration gets hard coded, for our project I'll likely end up having a different client for different environments (staging, prod etc).

The target host gets hard-coded in the client code, eg:

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM