如何解决OpenAPI & Python:如何使用 apiKey 安全方案?
我正在尝试通过 Python 使用具有 OpenAPI 规范的 API。我生成了 openapi_client 并使用生成的示例之一开始。对 API 的第一次调用成功,但由于会话 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)
对 foo() 的 http 响应包含键值对 'user_session_id': '1bf92fc3-7f2a-450f-a472-03a07a70bd8d'
。如何将此键值对添加到第二个请求的标头中?目前,第二个请求的标头仅包含以下内容:{'Accept': 'application/json','User-Agent': 'OpenAPI-Generator/1.0.0/python'}
解决方法
我发现了 this 个问题并针对我的用例修改了代码:
import pytest
def test_direct_exception_with_final_try_except():
print("Direct Exception + extra try/except works")
with pytest.raises(ValueError):
raise ValueError()
try:
raise
except:
pass
def test_direct_exception():
print("Direct Exception fails")
with pytest.raises(ValueError):
raise ValueError() # Not here
with pytest.raises(ValueError):
raise ValueError() # Value error will be raised here in debugging mode.
if __name__ == '__main__':
pytest.main([__file__,'-rP'])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。