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] Example of providing API key does not work #7269

Closed
cledoux opened this issue Dec 28, 2017 · 7 comments
Closed

[Python] Example of providing API key does not work #7269

cledoux opened this issue Dec 28, 2017 · 7 comments

Comments

@cledoux
Copy link

cledoux commented Dec 28, 2017

Description

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.

Swagger-codegen version

2.3.0

Swagger declaration file content or url

(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: []
Command line used for generation

docker run --rm -v $(pwd):/local swaggerapi/swagger-codegen-cli generate -i swagger.yaml -l python -o /local/clients/python

Steps to reproduce
  1. Generate the python client using the provided swagger yaml.
  2. pip install the generated client.
  3. Attempt to follow the example in the generated README.
@wing328
Copy link
Contributor

wing328 commented Jan 29, 2018

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?

@jacksonlauder
Copy link

I am having a similar issue. Is this issue solved?

@jacobweber
Copy link

jacobweber commented Mar 13, 2018

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)

@mascah
Copy link

mascah commented Mar 14, 2018

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.

@ensonic
Copy link

ensonic commented Mar 15, 2018

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)

@mascah
Copy link

mascah commented Mar 15, 2018

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

@ensonic
Copy link

ensonic commented Mar 15, 2018

Thanks. That definitely helps. Filed a new ticket #7847 - the global security setting is ignored :/

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

6 participants