Search code examples
pythoncontextio

Troubleshooting of python code with respect to NULL


I am not able to use the module contextIO as explained on this page....

https://github.com/contextio/Python-ContextIO

I get an error where I expect to see the email message contents.

import contextio as c

CONSUMER_KEY = 'dummy_key'
CONSUMER_SECRET = 'dummy_secret'

context_io = c.ContextIO(
    consumer_key=CONSUMER_KEY, 
    consumer_secret=CONSUMER_SECRET
)

accounts = context_io.get_accounts(email='dummy.email@gmail.com')

params = {
    'id': 'dummy_id'
}

account = c.Account(context_io, params) 

params = {
    'message_id': 'dummy_message_id'
}

message = c.Message(account, params)

message.get()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "contextio/__init__.py", line 1721, in get
    self.__init__(self.parent, self._request_uri('', params=params))
  File "contextio/__init__.py", line 481, in _request_uri
    uri, method=method, params=params, headers=headers, body=body
  File "contextio/__init__.py", line 481, in _request_uri
    uri, method=method, params=params, headers=headers, body=body
  File "contextio/__init__.py", line 150, in _request_uri
    self._handle_request_error(response)
  File "contextio/__init__.py", line 185, in _handle_request_error
    response_json = response.json()
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 638, in json
    return json.loads(self.text or self.content, **kwargs)
  File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 413, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 420, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)

How do I know if this error is due to the NULL returned as message object? And what is NoneType?

>>> type(message)
<class 'contextio.Message'>
>>> dir(message)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_request_uri', '_uri_for', 'addresses', 'base_uri', 'body', 'date', 'date_indexed', 'delete', 'email_message_id', 'facebook_headers', 'files', 'flags', 'folders', 'get', 'get_body', 'get_flags', 'get_folders', 'get_headers', 'get_source', 'get_thread', 'gmail_message_id', 'gmail_thread_id', 'headers', 'keys', 'list_headers', 'message_id', 'parent', 'person_info', 'post', 'post_flag', 'post_folder', 'put_folders', 'sanitize_params', 'source', 'sources', 'subject', 'thread']

>>> type(message.subject)
<type 'NoneType'>

Why is my code not returning anything even if I am supplying the correct credentials?


Solution

  • This error happens because the Context.IO lib is expecting a JSON response, but it's not getting it. This is where it blows up:

    File "contextio/__init__.py", line 185, in _handle_request_error
      response_json = response.json()
    

    This may be a problem with the service, returning an invalid JSON body or invalid header, but most likely, your request is failing for some other reason and the library isn't prepared to deal with the error.

    Try going into the line above, setting a break there and check what's the value of response.content. If that's an error message, it will give you a hint into what's wrong, and why the library fails. If it's valid JSON, check the Content-Type header and make sure it's application/json.