Skip to content

Instantly share code, notes, and snippets.

@toughrogrammer
Created May 23, 2016 06:12
Show Gist options
  • Select an option

  • Save toughrogrammer/b579424278a274a1dcfcdd89cac2a554 to your computer and use it in GitHub Desktop.

Select an option

Save toughrogrammer/b579424278a274a1dcfcdd89cac2a554 to your computer and use it in GitHub Desktop.
try:
auth = OAuth(access_token) if use_auth else None
payload_data = json.dumps(payload) if payload is not None else None
if payload_data is None:
request = requests.Request(method, url, params=params, auth=auth).prepare()
else:
request = requests.Request(method, url, params=params, data=payload_data, auth=auth).prepare()
request.headers['Content-Type'] = 'application/json'
req_session = requests.Session()
response = req_session.send(request)
if do_abort:
response.raise_for_status()
return response
except requests.ConnectionError as e:
if do_abort:
abort(500)
else:
return None
except requests.HTTPError as e:
if response.headers['Content-Type'] == 'application/json':
response_json = response.json()
if 'reason' in response_json:
reason = response_json['reason']
abort(response.status_code, reason)
else:
abort(response.status_code)
else:
abort(response.status_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment