Zendesk API搜索结果索引

2024-09-28 05:21:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个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是如何工作的吗?我为我在解释我的担忧时用了错误的句子而道歉。你知道吗


Tags: theidjsonurldataputresponserequest

热门问题