1

I want to write a client to an API that's described via Swagger. I'm new to Python but not to programming.

The API definition is here: https://app.swaggerhub.com/apis/calpolyud/Reeher/1

I used the codegen tools to generate a Python client and have been tinkering with it. No matter which generated class I attempt to invoke, I always get this error:

TypeError: 'NoneType' object is not iterable

Here's my sample code:

from __future__ import print_function
import time
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint
# create an instance of the API class

swagger_client.Configuration.apikey = 'somethingsecret'

api_instance = swagger_client.ContactReportApi()
activity_id = 'R176954627' # str | activityID of contact report

try:
    # Find a contact report by activityID
    response = api_instance. get_contact_report(activity_id)
except ApiException as e:
    print("Exception when calling ContactReportApi->get_contact_report: %s\n" % e)

Here's the stacktrace.

 Traceback (most recent call last):
    File ".\api_client.py", line 15, in 
    api_instance.get_contact_report(activity_id)
  File "V:\UD\Shared\Data Systems\Projects\Reeher API\swagger_client\api\contact_report_api.py", line 66, in get_contact_report
    (data) = self.get_contact_report_with_http_info(activity_id, **kwargs)  # noqa: E501
  File "V:\UD\Shared\Data Systems\Projects\Reeher API\swagger_client\api\contact_report_api.py", line 140, in get_contact_report_with_http_info
    collection_formats=collection_formats)
  File "V:\UD\Shared\Data Systems\Projects\Reeher API\swagger_client\api_client.py", line 333, in call_api
    _preload_content, _request_timeout)
  File "V:\UD\Shared\Data Systems\Projects\Reeher API\swagger_client\api_client.py", line 122, in __call_api
    collection_formats))
  File "V:\UD\Shared\Data Systems\Projects\Reeher API\swagger_client\api_client.py", line 416, in parameters_to_tuples
    for k, v in six.iteritems(params) if isinstance(params, dict) else params:  # noqa: E501
TypeError: 'NoneType' object is not iterable

I can roll my own client and teach myself more Python, but I was expecting to benefit from the Swagger ecosystem. Am I missing something simple here?

  • I'm not familiar with this API nor am I familiar with Swagger but, from skimming through the contact_report_api.py, my 1st guess is that your activityID might not be correct and it's returning a NoneType (i.e. it's not finding the data for that activityID and that call is returning None). So, when the nested method `parameters_to_tuples` is trying to iterate over the parameters that `get_contact_report` is supposed to return, there's nothing to iterate over (because the `params` variable value is `None`). All of this is to ask the question...are you sure that's a valid activityID? – Jesse McCall Dec 14 '17 at 00:09
  • Yes it is valid. I'm able to submit these same requests through the Swagger web UI after authenticating with my api_key and call the various methods that list things and get them based on their IDs, and nothing I try works. So I think it is a more fundamental problem than just fetching an ID that doesn't exist. – Brian W. Spolarich Dec 14 '17 at 00:32
  • Actually I spoke too soon. I can use the SwaggerUI that the vendor provides, authenticate, and submit requests. If I use the SwaggerUI within my project on the swaggerhub.com site (where I just imported the vendor's Swagger JSON file), I can authenticate but requests never get the api_key header ("Failed to authenticate with apiKey : null"). I'm wondering if there's a problem in the Swagger definition file that's translating into bad client code? – Brian W. Spolarich Dec 14 '17 at 00:41
  • I fixed the problem with the vendor-provided swagger file by adding this global security definition: `security:` ` - apikey: []` and was able to execute API operations through the Swagger UI with the new security scheme, and regenerated the client package. I still get the same errors as above. – Brian W. Spolarich Dec 14 '17 at 17:19
  • I get a very similar error - I wish I could find a _working_ Python example to see what I'm doing wrong - I suspect the JSON is missing some braces or header or something. – bombcar Mar 12 '18 at 14:40
  • Are you having a problem with the Reeher API specifically? – Brian W. Spolarich Mar 12 '18 at 18:48
  • Possible duplicate of [TypeError: 'NoneType' object is not iterable in Python](https://stackoverflow.com/questions/3887381/typeerror-nonetype-object-is-not-iterable-in-python) – bsplosion Jun 05 '19 at 18:07

0 Answers0