有没有比我在这个函数中使用的更好的输入验证模式?在
这是一个没有经过任何验证的函数。它的可读性更高,短小精悍(9点对53点)。在
def webhook(clearbitkey, appid, intercomkey):
event = request.get_json()
id = event['data']['item']['id']
email = event['data']['item']['email']
person = requests.get(CLEARBIT_USER_ENDPOINT.format(email=email), auth=(clearbitkey, '')).json()
domain = person['employment']['domain']
company = requests.get(CLEARBIT_COMPANY_ENDPOINT.format(domain=domain), auth=(clearbitkey, '')).json()
note = create_note(person, company)
res = requests.post(INTERCOM_ENDPOINT,
json=dict(user=dict(id=id), body=note),
headers=dict(accept='application/json'),
auth=(appid, intercomkey))
return jsonify(note=res.json())
但是,它不处理以下任何错误:
有没有更好的模式可以遵循?我曾考虑过使用像voluptous这样的数据验证库,但似乎我仍然存在同样的冗长问题。在
Giton Hub的原始代码看起来不错。它有点过于复杂,但也能处理所有的错误情况。你可以通过抽象的东西来提高可读性。在
为了演示,我可以写这样的代码:
我希望有帮助。在
相关问题 更多 >
编程相关推荐