1

I need help, trying to use Python to make the following request for tickets updated within the last 24 hours but cant seem to figure this out based on the example provided.It gives me a valid response in Postman so the GET request works, I just can't figure out how to code this.

The Query I am trying to make is: https://clubready.zendesk.com/api/v2/search.json?sort_by=updated_at&sort_order=desc&query=updated>24hours type:ticket

My code is:

import requests

url = "https://clubready.zendesk.com/api/v2/search.json"

querystring = {"sort_by":"updated_at","sort_order":"desc","query":"updated%3E24hours%20type:ticket"}

payload = ""
headers = {
    'Authorization': "Basic ZHJvYmluc29uQGNsdWJyZWFkeS5jb206QWNlbWFuMTAh",
    'User-Agent': "PostmanRuntime/7.15.0",
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Postman-Token': "9d56b0e2-729f-4170-88f3-bcf9dbfa1020,803b5113-075b-4838-9fad-0d819c389c7a",
    'Host': "clubready.zendesk.com",
    'cookie': "__cfduid=d0076a9c776e07ff900489e935bb86e691558998542; __cfruid=03f547455e192905682b701e2bc5521a99d71c6c-1560442970; _zendesk_shared_session=-NEV5WTdPS1kyb1Z4SHBDbnk1d1dsWWhqUGloTTg1WEQybGs4L3FkNGRMakZtN0g3aGlTRlV1S3RrV1dlTllWWVFySUZvNkFFWHM5VVlEUDVyM0g1OFV3Q3grZjNIYWVwZGR1MlA4T05NN2NzZDlxMTZqUm0rNk9pVzFOSjk4M0I5SnB2d056MVNiZ28rWEdhMS9uQmhWT3g0MG1iaEtmVUpreTJzOXRRbVZHYUEvN1AyVk1KWkRtSzdiOVQvV2tkU0hia2VhenRCRjhGY3k1TTRFQ2dzUT09LS1BT2FIWEhzUXVwWGNxZ3lBcXBReklRPT0%3D--3c394a5133686509e48321137b451c2d037cf3ee; _help_center_session=NndtdTNYTDl3NTRWT1lLVGlLT1pYMVI4NVF5MncxOTVYdTlZWG1QTlJ5cGtTbXdPeGYyL2R6TWVwdXZXeUV6U2p3NlR0eHdnR29GOFlSMVpTalJvMUFRNCs1SkJqZHdJQXpOQzNveCtyT2xFU2pycGtUdEZRYWRnWHh1Z1YzcjFVWk5Ec1VoWjV0U2JXZGJTMURkT0djdnRiL3F6VlN2QU1IWDNSSVJRUTNuSm1mK21XZDdtSGhGaDc4bStsQzdIZ3dQeVVESW1sV3VaOGJuSjBjSjlBYWJ3a3J3bzEraHVDelJBclFtbk10RT0tLWMyTWxDNHpNQWcvVFRZbXo3R0szZkE9PQ%3D%3D--047ca57c1f10d81ce14f8de917a3ff6f43e9ef7b; _zendesk_session=BAh7C0kiD3Nlc3Npb25faWQGOgZFVEkiJWM3NjFiY2E5Zjc3YWVmMDg3Mjc2M2ZlNzk3MzYxZTFjBjsAVEkiDGFjY291bnQGOwBGaQKIAUkiCnJvdXRlBjsARmkDKzoBSSIOaXNfbW9iaWxlBjsAVEZJIhN3YXJkZW4ubWVzc2FnZQY7AFR7AEkiEF9jc3JmX3Rva2VuBjsARkkiRVlESmVEL1NWYVNhKzZ2YjRIRENaUGlBa29mdWtUZlArVU5QNEU3RXpTcjdhMlYwNG5OdzFVMU5INk1yY1pCTVoGOwBG--a237bcedbc9d391a74d2ad7b549bc7835b0e7566",
    'accept-encoding': "gzip, deflate",
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, data=payload, headers=headers, params=querystring)

print(response.text)

{"results":[],"facets":null,"next_page":null,"previous_page":null,"count":0}

I get this when using the code but I get an actual list in Postman.

1 Answer 1

1

This is very similar to a query I usually do in Python to get Zendesk ticket data.

First, let's import the dependencies:

import requests
from urllib.parse import urlencode

Now, let's create the query string and add it to a dictionary that includes all your request parameters:

queryString = "updated>24hours type:ticket"

params = {
    'query': queryString,
    'sort_by': 'updated_at',
    'sort_order': 'desc'
}

The last thing we need to do is to specify the url, user and pwd you need:

url = 'https://clubready.zendesk.com/api/v2/search.json?' + urlencode(params)
user = YOUR_EMAIL + '/token'
pwd = YOUR_TOKEN

Now we're ready to make the request. To do this, set a while loop that will read the response .json and append it to a resultsList.

#use a counter to print the current page number
counter = 0

while url:
    response = requests.get(url, auth=(user, pwd))
    if response.status_code == 429:
        #handle rate limiting with Zendesk's recommended timeout value
        print('Rate limited! Please wait.')
        time.sleep(int(response.headers['retry-after']))
        continue
    if response.status_code != 200:
        #for any other code, exist the request
        print('Status:', response.status_code, 'Problem with the request. Exiting.')
        exit()

    #if the request went through, read through the json and add each ticket to resultsList
    data = response.json()

    for result in data['results']:
        resultsList.append(result)

    #print the current page number. This is not necessary, but nice
    counter += 1
    print(str(counter) + ", ", end='')

    #get the url of the next page that is given inside the results json
    url = data['next_page']

    #timeout for 1 second to avoid accidental rate limiting
    time.sleep(1)

Using a timeout after reading each page helped me avoid the odd rate-limiting event.

resultsList will be a list, each item representing one ticket. So the length of the list is identical to the number of tickets found by your query. This list can be used to create a Pandas DataFrame where you can analyze your ticket data further.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.