JSON Decoding issue using Requests in python 2.7.10 for Zendesk API

541 views Asked by At

I have a python script that I am using to call to the Zendesk API in order to get back a JSON return. The typical Data = response.json() worked until I had to port everything over to python 2.7. It worked fine in 3.4. I tried various different encoding and decoding strategies, and I have gotten it past the break, but then it is in a form I can use. I need to be able to access the json parts by string, not integer Data['users'] as an example. Here is the code I have up to the break.

import datetime
import time
import calendar
import requests
import os.path as path
import json
import logging
import Query2UploadRouter
import sys
from httplib import BadStatusLine
from io import open


def byteify(input):

    if isinstance(input, dict):
        return {byteify(key):byteify(value) for key,value in input.iteritems()}
    elif isinstance(input, list):
        return [byteify(element) for element in input]
    elif isinstance(input, unicode):
        return input.encode('utf-8')
    else:
        return input

def runQuery(endPoint,tableName,topJsonTier,userName,passWord,schema,verticaName,verticaPassword,i):

    if path.isfile('Data_Aquisition_Log_' + str(datetime.date.today()) + '.txt') == True:
        logging.basicConfig(filename='Data_Aquisition_Log_' + str(datetime.date.today()) + '.txt', filemode='a', level=logging.DEBUG)
    else:
        logging.basicConfig(filename='Data_Aquisition_Log_' + str(datetime.date.today()) + '.txt', filemode='w', level=logging.DEBUG)

    startTime = calendar.timegm(datetime.datetime.now().timetuple()) - 3600

    url = "https://myURL.zendesk.com/api/v2/" + str(endPoint) + str(startTime)

    user = userName
    pwd = passWord
    headers = {'content-type': 'application/json'}

    # Create a requests to avoid flooding the server with requests
    # Do all of this stuff and then do it again as long as the response JSON has a next page
    while True:

        # Do the HTTP get request

        for x in xrange(0,9):

            logging.debug('Query Attempt...' + str(x))

            try:

                logging.debug(url)
                response = requests.get(url, auth=(user,passWord))
                break

            except:

                err = sys.exc_info()[0]
                logging.debug('There was an error when attemtping to execute the query!  URL: ' + str(url) + ' Error: ' + str(err))
                time.sleep(5)
                pass

        # Check for HTTP codes other than 200
        if response.status_code != 200 or response.status_code is None:
            if response.status_code == 429:
                logging.debug(unicode(response.status_code) + 'Rate Limited.  Waiting to Retry')
                time.sleep(float(response.headers['retry-after']))
                response = requests.get(url, auth=(user,passWord))
            elif response.status_code == 422:
                logging.debug( 'The last record was reached')
                break
            else:
                logging.debug('Problem with the request. Retrying.')
                response = requests.get(url, auth=(user,passWord))

        # Decode the JSON response into a dictionary and use the data

        data = byteify(response.json())

Using the biteify was a solution I found online that was my most recent attempt. Here was the error response I got back:

DEBUG:root:Starting run for ZendeskUsersTest!!
DEBUG:root:Query Attempt... 0 
DEBUG:root:https://myURL.zendesk.com/api/v2/incremental/users.json?start_time=1434444644
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): .zendesk.com
DEBUG:requests.packages.urllib3.connectionpool:"GET /api/v2/incremental/users.json?start_time=1434444644 HTTP/1.1" 200 None 
DEBUG:root:An error caused the run to break! <type 'exceptions.ValueError'>
No JSON object could be decoded
DEBUG:root:All data aquisition was completed!

So as you can see I'm getting a 200 response. Putting in the URL directly into a web page gives me that JSON return and I validated it using JSONlint.

0

There are 0 answers