尽管 API 调用成功,Swagger 的 Python SDK returns None

Swagger's Python SDK returns None despite the API call being successful

这是我为使用 Swagger Python SDK 进行 API 调用而编写的代码:

import swagger_client
client = swagger_client.ApiClient()
response = client.call_api('/student', 'POST')
print(response)

responseNone

call_api 方法采用参数 response_type,它可以具有以下值之一:

  • Python支持的常用数据类型:intstrbool
  • 与被 API 调用 return 编辑的对象对应的模型:swagger_client.models.*

如果未传递此参数,call_api 编辑的值 return 将是 None

在这种情况下:

response = client.call_api('/student', 'POST', response_type = swagger_client.models.Student)

将 return 类型为 Student

的对象
response = client.call_api('/student', 'POST', response_type = 'str')

将return一个字符串。

不需要直接使用ApiClientcall_api方法。您可以改为使用 *Api class 中生成的方法(例如 PetApi):

    self.pet_api.add_pet(body=self.pet)

    fetched = self.pet_api.get_pet_by_id(pet_id=self.pet.id)

参考:https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/python/tests/test_pet_api.py#L92