如何使用Python在Zendesk中查询票证清单?

时间:2019-06-13 17:31:27

标签: python zendesk zendesk-api

我需要帮助,尝试使用Python对最近24小时内更新的票证发出以下请求,但根据提供的示例似乎无法弄清楚这一点,它在邮递员中给了我一个有效的响应,因此GET请求有效,我只是不知道该怎么编码。

我要进行的查询是:https://clubready.zendesk.com/api/v2/search.json?sort_by=updated_at&sort_order=desc&query=updated>24hours type:ticket

我的代码是:

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}

我在使用代码时得到了这个,但在邮递员中却得到了一个实际的列表。

1 个答案:

答案 0 :(得分:0)

这非常类似于我通常在Python中为获取Zendesk票证数据而执行的查询。

首先,让我们导入依赖项:

import requests
from urllib.parse import urlencode

现在,让我们创建查询字符串并将其添加到包含所有请求参数的字典中:

queryString = "updated>24hours type:ticket"

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

我们最后要做的是指定您需要的urluserpwd

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

现在我们可以提出请求了。为此,请设置一个while循环,该循环将读取响应.json并将其附加到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)

阅读每一页后使用超时可以帮助我避免出现奇数速率限制事件。

resultsList将是一个列表,每个项目代表一张票证。因此,列表的长度等于您的查询找到的票证数量。此列表可用于创建Pandas DataFrame,您可以在其中进一步分析票证数据。