尽管API调用成功,但Swagger的Python SDK仍返回None

时间:2016-01-21 14:57:38

标签: python swagger swagger-2.0

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

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

responseNone

2 个答案:

答案 0 :(得分:1)

您无需直接使用call_api ApiClient方法。您可以使用* Api类中生成的方法(例如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

答案 1 :(得分:0)

call_api方法接受参数response_type,该参数可以具有以下值之一:

  • Python支持的常用数据类型:intstrbool等。
  • 与API调用返回的对象对应的模型:swagger_client.models.*

如果未传递此参数,call_api返回的值将为None

在这种情况下:

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

将返回Student

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

将返回一个字符串。

相关问题