python – 为什么我的ZenDesk宏被更新,但实际上没有变化?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 为什么我的ZenDesk宏被更新,但实际上没有变化?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4301字,纯文字阅读大概需要7分钟。
内容图文
![python – 为什么我的ZenDesk宏被更新,但实际上没有变化?](/upload/InfoBanner/zyjiaocheng/727/0d654c37d5634d42ac01a0a6e983ba3e.jpg)
我试图在ZenDesk上批量编辑我的个人宏的签名,唯一的方法是通过API.所以我写了这个快速的Python脚本来尝试这样做:
import sys
import time
import logging
import requests
import re
start_time = time.time()
# Set up logging
logger = logging.getLogger()
log_handler = logging.StreamHandler(sys.stdout)
log_handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s - %(funcName)s - line %(lineno)d"))
log_handler.setLevel(logging.DEBUG)
logger.addHandler(log_handler)
logger.setLevel(logging.DEBUG)
def doTheGet(url, user, pwd):
response = requests.get(url, auth=(user + "/token", pwd))
if response.status_code != 200:
logger.error("Status: %s (%s) Problem with the request. Exiting. %f seconds elapsed" % (response.status_code, response.reason, time.time() - start_time))
exit()
data = response.json()
return data
def doThePut(url, updated_data, user, pwd):
response = requests.put(url, json="{'macro': {'actions': %r}}" % updated_data, headers={"Content-Type": "application/json"}, auth=(user + "/token", pwd))
if response.status_code != 200:
logger.error("Status: %s (%s) Problem with the request. Exiting. %f seconds elapsed" % (response.status_code, response.reason, time.time() - start_time))
exit()
data = response.json()
return data
def getMacros():
macros = {}
data = doTheGet("https://mydomain.zendesk.com/api/v2/macros.json", "me@mydomain.com", "111tokenZZZ")
def getMacros(macro_list, page, page_count):
if not page:
for macro in macro_list:
if macro["restriction"] and macro["active"]:
if macro["restriction"]["type"] == "User":
macros[macro["id"]] = macro["actions"]
else:
for macro in macro_list:
if macro["restriction"] and macro["active"]:
if macro["restriction"]["type"] == "User":
macros[macro["id"]] = macro["actions"]
page_count += 1
new_data = doTheGet(page, "me@mydomain.com", "111tokenZZZ")
new_macs = new_data["macros"]
new_next_page = new_data["next_page"]
getMacros(new_macs, new_next_page, page_count)
macs = data["macros"]
current_page = 1
next_page = data["next_page"]
getMacros(macs, next_page, current_page)
return macros
def updateMacros():
macros = getMacros()
regular = "RegEx to match signature to be replaced$" #since some macros already have the updated signature
for macro in macros:
for action in macros[macro]:
if action["field"] == "comment_value":
if re.search(regular, action["value"][1]):
ind = action["value"][1].rfind("\n")
action["value"][1] = action["value"][1][:ind] + "\nNew signature"
return macros
macs = updateMacros()
for mac in macs:
doThePut("https://mydomain.zendesk.com/api/v2/macros/%d.json" % (mac), macs[mac], "me@mydomain.com", "111tokenZZZ")
现在,一切都按预期运行,我没有错误.当我在ZenDesk上访问我的宏并通过最后更新对它们进行排序时,我确实看到脚本做了一些事情,因为它们显示为今天的最后更新.但是,它们没有任何变化.我确保编辑了我发送的数据(updateMacros正在完成它的工作).我确保请求发回一个OK响应.所以我发送更新的数据,得到200响应,但the response sent back显示了以前的宏,没有变化.
我发现的唯一可能是错误的是我发送的数据的格式,或类似的东西.但即便如此,我预计回复不会是200,那么……
我在这里错过了什么?
解决方法:
看起来您在PUT请求中对JSON数据进行了双重编码:
response = requests.put(url, json="{'macro': {'actions': %r}}" % updated_data, headers={"Content-Type": "application/json"}, auth=(user + "/token", pwd))
json参数需要一个对象,然后它尽职尽责地编码为JSON并作为请求的主体发送;这只是一种便利;实施很简单,
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
# provides this natively, but Python 3 gives a Unicode string.
content_type = 'application/json'
body = complexjson.dumps(json)
if not isinstance(body, bytes):
body = body.encode('utf-8')
(来源:https://github.com/kennethreitz/requests/blob/master/requests/models.py#L424)
由于值总是通过json.dumps()传递,如果传递表示已经编码的JSON的字符串,它本身将被编码:
"{\'macro\': {\'actions\': [{\'field\': \'comment_value\', \'value\': [\'channel:all\', \'Spiffy New Sig that will Never Be Saved\']}]}}"
ZenDesk,在给出JSON它不期望的时候,更新updated_at字段并且……没有别的.您可以通过传递一个空字符串来验证这一点 – 结果相同.
请注意,您还依赖Python的repr格式来填充您的JSON;这也许是一个坏主意.相反,让我们重新构建我们的宏对象并让请求对其进行编码:
response = requests.put(url, json={'macro': {'actions': updated_data}}, headers={"Content-Type": "application/json"}, auth=(user + "/token", pwd))
这应该做你期望的.
内容总结
以上是互联网集市为您收集整理的python – 为什么我的ZenDesk宏被更新,但实际上没有变化?全部内容,希望文章能够帮你解决python – 为什么我的ZenDesk宏被更新,但实际上没有变化?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。