2017-12-30 14:15:51 +01:00
|
|
|
from requests import Request, Session
|
|
|
|
from toot.exceptions import NotFoundError, ApiError
|
2017-12-30 16:30:35 +01:00
|
|
|
from toot.logging import log_request, log_response
|
2017-12-30 14:15:51 +01:00
|
|
|
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
def send_request(request, allow_redirects=True):
|
|
|
|
log_request(request)
|
|
|
|
|
|
|
|
with Session() as session:
|
|
|
|
prepared = session.prepare_request(request)
|
|
|
|
response = session.send(prepared, allow_redirects=allow_redirects)
|
|
|
|
|
2017-12-30 14:15:51 +01:00
|
|
|
log_response(response)
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
return response
|
|
|
|
|
2017-12-30 14:15:51 +01:00
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
def _get_error_message(response):
|
|
|
|
"""Attempt to extract an error message from response body"""
|
|
|
|
try:
|
|
|
|
data = response.json()
|
|
|
|
if "error_description" in data:
|
|
|
|
return data['error_description']
|
|
|
|
if "error" in data:
|
|
|
|
return data['error']
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return "Unknown error"
|
|
|
|
|
|
|
|
|
|
|
|
def process_response(response):
|
|
|
|
if not response.ok:
|
|
|
|
error = _get_error_message(response)
|
2017-12-30 14:15:51 +01:00
|
|
|
|
|
|
|
if response.status_code == 404:
|
|
|
|
raise NotFoundError(error)
|
|
|
|
|
|
|
|
raise ApiError(error)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def get(app, user, url, params=None):
|
|
|
|
url = app.base_url + url
|
|
|
|
headers = {"Authorization": "Bearer " + user.access_token}
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
request = Request('GET', url, headers, params=params)
|
|
|
|
response = send_request(request)
|
2017-12-30 14:15:51 +01:00
|
|
|
|
|
|
|
return process_response(response)
|
|
|
|
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
def anon_get(url, params=None):
|
|
|
|
request = Request('GET', url, None, params=params)
|
|
|
|
response = send_request(request)
|
2017-12-30 14:15:51 +01:00
|
|
|
|
|
|
|
return process_response(response)
|
|
|
|
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
def post(app, user, url, data=None, files=None, allow_redirects=True):
|
2017-12-30 14:15:51 +01:00
|
|
|
url = app.base_url + url
|
|
|
|
headers = {"Authorization": "Bearer " + user.access_token}
|
|
|
|
|
|
|
|
request = Request('POST', url, headers, files, data)
|
2017-12-30 16:30:35 +01:00
|
|
|
response = send_request(request, allow_redirects)
|
|
|
|
|
|
|
|
return process_response(response)
|
2017-12-30 14:15:51 +01:00
|
|
|
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
def anon_post(url, data=None, files=None, allow_redirects=True):
|
|
|
|
request = Request('POST', url, {}, files, data)
|
|
|
|
response = send_request(request, allow_redirects)
|
2017-12-30 14:15:51 +01:00
|
|
|
|
|
|
|
return process_response(response)
|