The example of configuring an API in the generated README is broken for python.
The example code generated is:
# Configure API key authorization: api_key
swagger_client.configuration.api_key['api_key'] = 'YOUR_API_KEY'
However, the configuration.py file does not have an api_key attribute. All attributes are now inside the Configuration class and cannot be set at the module level.
2.3.0
(This is just the petstore spec stripped down to only the necessary bits. It will not work if you use the full petstore spec because the example uses the OAuth instead of ApiKey.)
---
swagger: "2.0"
host: "petstore.swagger.io"
basePath: "/v2"
schemes:
- "http"
paths:
/pet:
get:
operationId: "get_pet"
responses:
200:
description: "successful operation"
schema:
type: "object"
properties:
id:
type: "integer"
format: "int64"
securityDefinitions:
api_key:
type: "apiKey"
name: "api_key"
in: "header"
security:
- api_key: []
docker run --rm -v $(pwd):/local swaggerapi/swagger-codegen-cli generate -i swagger.yaml -l python -o /local/clients/python
swagger_client.configuration.api_key['api_key'] = 'YOUR_API_KEY'
@cledoux Do you mind filing a PR to fix the sample code when you've time?
I am having a similar issue. Is this issue solved?
Managed to get it working like this:
config = my_api_name.Configuration()
config.api_key['my-key-name'] = 'my-key-value'
api_client = my_api_name.ApiClient(config)
api_instance = my_api_name.MySampleApi(api_client)
I'm currently doing what @jacobweber is doing as well. Is there an intended way to set defaults for the configuration object so we don't have to set the values after initializing it?
It would be nice to not have to pass around an ApiClient with a configured Configuration object to initialize all my Api instances and instead rely on the default initializer for the generated Api classes.
I still can't get it to work. What is the semantics of 'my-key-name' in
config.api_key['my-key-name'] = 'my-key-value'
I also added:
config.debug = True
to see the http request, but there is no "x-api-key: xxxx" header added. I've found a work-around though:
...
api_client = my_api_name.ApiClient(config)
api_client.set_default_header(header_name='x-api-key', header_value=api_key)
api_instance = my_api_name.MySampleApi(api_client)
@ensonic my-key-name should match the security definition name.
securityDefinitions: {
api_key: {
type: "apiKey",
name: "authorization",
in: "header"
}
}
In my configuration object, that looks like,
api_client.configuration.api_key['authorization'] = 'token_value'
api_client.configuration.api_key_prefix['authorization'] = 'Token'
This produces a header authorization: Token xyz123..
Thanks. That definitely helps. Filed a new ticket https://github.com/swagger-api/swagger-codegen/issues/7847 - the global security setting is ignored :/
Most helpful comment
@ensonic
my-key-nameshould match the security definition name.In my configuration object, that looks like,
This produces a header
authorization: Token xyz123..