def callapi(url, method='post', payload=None, headers=None, auth=None, check=True):
"""
Simple wrapper around `requests.post`, with excessive logging.
Returns a Flask-friendly tuple on success or failure.
Logs and re-raises any exceptions.
"""
if not headers:
headers = {'Content-type': 'application/json'}
try:
logging.info("URL=%s" % url)
logging.info("Auth=%s" % str(auth))
logging.info("Headers=%s" % headers)
logging.info("Body=%s" % payload)
logging.info("Check=%s" % check)
if (auth is not None):
r = requests.request(method, url, auth=auth, headers=headers, data=payload, verify=bool(strtobool(str(check))))
else:
r = requests.request(method, url, headers=headers, data=payload, verify=check)
if r.status_code >= 200 and r.status_code < 300:
if (payload is None):
return r.text
else:
return ("OK", r.status_code, None)
except:
logging.exception("Can't create new payload. Check code and try again.")
raise
return ("%s" % r.text, r.status_code, None)
评论列表
文章目录