Toot-Mastodon-CLI-TUI-clien.../toot/http.py

113 lines
3.3 KiB
Python
Raw Normal View History

2017-12-30 14:15:51 +01:00
from requests import Request, Session
2022-11-22 21:19:52 +01:00
from requests.exceptions import RequestException
from toot import __version__
2017-12-30 14:15:51 +01:00
from toot.exceptions import NotFoundError, ApiError
2023-11-21 10:07:36 +01:00
from toot.logging import log_request, log_request_exception, log_response
2017-12-30 14:15:51 +01:00
def send_request(request, allow_redirects=True):
# Set a user agent string
# Required for accessing instances using Cloudfront DDOS protection.
request.headers["User-Agent"] = "toot/{}".format(__version__)
log_request(request)
2022-11-22 21:19:52 +01:00
try:
with Session() as session:
prepared = session.prepare_request(request)
settings = session.merge_environment_settings(prepared.url, {}, None, None, None)
response = session.send(prepared, allow_redirects=allow_redirects, **settings)
except RequestException as ex:
2023-11-21 10:07:36 +01:00
log_request_exception(request, ex)
2022-11-22 21:19:52 +01:00
raise ApiError(f"Request failed: {str(ex)}")
2017-12-30 14:15:51 +01:00
log_response(response)
return response
2017-12-30 14:15:51 +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, path, params=None, headers=None):
url = app.base_url + path
headers = headers or {}
headers["Authorization"] = f"Bearer {user.access_token}"
2017-12-30 14:15:51 +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)
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)
def post(app, user, path, headers=None, files=None, data=None, json=None, allow_redirects=True):
url = app.base_url + path
headers = headers or {}
headers["Authorization"] = f"Bearer {user.access_token}"
2017-12-30 14:15:51 +01:00
return anon_post(url, headers=headers, files=files, data=data, json=json, allow_redirects=allow_redirects)
2017-12-30 14:15:51 +01:00
def patch(app, user, path, headers=None, files=None, data=None, json=None):
url = app.base_url + path
headers = headers or {}
headers["Authorization"] = f"Bearer {user.access_token}"
request = Request('PATCH', url, headers=headers, files=files, data=data, json=json)
response = send_request(request)
return process_response(response)
def delete(app, user, path, data=None, json=None, headers=None):
url = app.base_url + path
2017-12-30 14:15:51 +01:00
headers = headers or {}
headers["Authorization"] = f"Bearer {user.access_token}"
2018-06-14 10:40:16 +02:00
request = Request('DELETE', url, headers=headers, data=data, json=json)
2018-06-14 10:40:16 +02:00
response = send_request(request)
return process_response(response)
def anon_post(url, headers=None, files=None, data=None, json=None, allow_redirects=True):
request = Request(method="POST", url=url, headers=headers, files=files, data=data, json=json)
response = send_request(request, allow_redirects)
2017-12-30 14:15:51 +01:00
return process_response(response)