I'm trying to use an API with an OpenAPI specification via Python. I generated the openapi_client and used one of the generated examples to get started. The first call to the API succeeds, but subsequent calls fail due to an invalid session ID.
securitySchemes:
user_session_authentication:
description: [...]
type: apiKey
in: header
name: SOME_PREFIX_user_session_id
security:
- user_session_authentication: [] # default for all actions is user_session_id!
import openapi_client
from openapi_client.api import some_api
from openapi_client.model.inline_response200 import InlineResponse200
from openapi_client.model.inline_response2002 import InlineResponse2002
from pprint import pprint
with openapi_client.ApiClient() as api_client:
username = "john"
password = "45d75ii47"
api_instance = some_api.SomeApi(api_client)
username = username
password = password
body = function_a("foo")
api_response = api_instance.foo(username=username, password=password, body=body)
pprint(api_response)
api_response = api_instance.bar()
pprint(api_response)
The http response to foo() contains the key-value pair 'user_session_id': '1bf92fc3-7f2a-450f-a472-03a07a70bd8d'
. How do I add this key-value pair to the header of the second request? Currently, the header of the second request contains just the following: {'Accept': 'application/json', 'User-Agent': 'OpenAPI-Generator/1.0.0/python'}
I found this question and adapted the code for my use case:
import openapi_client
from openapi_client.api import some_api
from openapi_client import Configuration
from openapi_client.model.inline_response200 import InlineResponse200
from openapi_client.model.inline_response2002 import InlineResponse2002
from pprint import pprint
with openapi_client.ApiClient() as api_client:
username = "john"
password = "45d75ii47"
api_instance = some_api.SomeApi(api_client)
username = username
password = password
body = function_a("foo")
api_response = api_instance.foo(username=username, password=password, body=body)
pprint(api_response)
conf = Configuration()
conf.api_key = {"SOME_PREFIX_user_session_id": api_response.user_session_id}
api_client = openapi_client.ApiClient(None, "SOME_PREFIX_user_session_id", conf.get_api_key_with_prefix("user_session_id"))
api_instance = some_api.SomeApi(api_client)
api_response = api_instance.bar()
pprint(api_response)