Zendesk API搜索结果索引

时间:2019-10-01 07:57:59

标签: python api zendesk

我编写了一个python脚本来在zendesk API中执行GET和PUT方法,并成功获取了我想要的数据并对票证进行了一些更新。

下面的方法导致票证编号为“ 6442”,而put方法旨在删除标签

from urllib.parse import urlencode
import json
import requests

# Set the credentials
credentials = 'some email', 'some password'
session = requests.Session()
session.auth = credentials

# Set the GET parameters
params_noreply_window = {
    'query': 'type:ticket tags:test status<closed',
}

params_oustide_businesshour = {
    'query': 'type:ticket tags:send_whatsapp_obh status:new',
}

url_search1 = 'https://propertypro.zendesk.com/api/v2/search.json?' + \
    urlencode(params_noreply_window)
url_search2 = 'https://propertypro.zendesk.com/api/v2/search.json?' + \
    urlencode(params_oustide_businesshour)

response_noreply_window = session.get(url_search1)
response_oustide_businesshour = session.get(url_search2)
# -----------------------------------------------------------------------------

if response_noreply_window.status_code != 200 | response_oustide_businesshour.status_code != 200:
    print('Status 1:', response_noreply_window.status_code + 'Status 2:', response_oustide_businesshour.status_code,
          'Problem with the request. Exiting.')
    exit()

# Print the subject of each ticket in the results
data_noreply_window = response_noreply_window.json()
data_oustide_businesshour = response_oustide_businesshour.json()

# Ticket to update
# Create a list containing the values of the id field
# for each dictionary that is an element of the list data
id_merged1 = [result['id'] for result in data_noreply_window['results']]
print(type(id_merged1))
print(id_merged1)

id_merged2 = [result['id'] for result in data_oustide_businesshour['results']]
print(type(id_merged2))
print(id_merged2)


# Join value of list by using comma separated
id_merged1_joined = ','.join(map(str, id_merged1))
print(id_merged1_joined)

id_merged2_joined = ','.join(map(str, id_merged2))
print(id_merged2_joined)

# Package the data in a dictionary matching the expected JSON
data_comment1 = {"ticket":
                 {
                     "remove_tags": ["test"]
                 }
                 }
data_comment2 = {"ticket":
                 {
                     "remove_tags": ["send_whatsapp_obh"]
                 }
                 }


# Encode the data to create a JSON payload
payload1 = json.dumps(data_comment1)
payload2 = json.dumps(data_comment2)

print("**Start**")

# Set the request parameters
url_put_comments1 = 'https://propertypro.zendesk.com/api/v2/tickets/update_many.json?' +\
    'ids=' + id_merged1_joined

url_put_comments2 = 'https://propertypro.zendesk.com/api/v2/tickets/update_many.json?' +\
    'ids=' + id_merged2_joined

user = 'mart.polman@lamudi.co.id'
pwd = 'lamudionfire'
headers = {'content-type': 'application/json'}

# Do the HTTP put request
response_request_noreply = requests.put(url_put_comments1, data=payload1,
                                        auth=(user, pwd), headers=headers)

response_request_obh = requests.put(url_put_comments2, data=payload2,
                                    auth=(user, pwd), headers=headers)

# Check for HTTP codes other than 200
if response_request_noreply.status_code != 200 | response_request_obh.status_code != 200:
    print('Status 1:', response_request_noreply.status_code +
          'Status 1:', response_request_obh.status_code,
          'Problem with the request. Exiting.')
    exit()

    # Report success
    print('Successfully added comment to tickets')

但是,在运行我的python代码并执行另一个GET方法之后,仍然显示相同的凭单号,并且我需要在随机时间内等待以获得想要的结果,该结果将返回“ null”,因为我已经通过使用更新了凭单PUT方法。

有人可以向我解释Zendesk API的工作原理吗?我为在解释我的担忧时的错误句子道歉。

0 个答案:

没有答案