51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
"""Wrap backend requests for usage in commands."""
|
|
import logging
|
|
from urllib.parse import urljoin
|
|
|
|
import requests
|
|
|
|
import hitomi.config as config
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class DrBotzoError(requests.HTTPError):
|
|
"""Wrap HTTPError with the error detail from the dr.botzo API, if available."""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
"""Initialize DrBotzoError."""
|
|
detail = kwargs.pop('detail', None)
|
|
self.detail = detail
|
|
super(DrBotzoError, self).__init__(*args, **kwargs)
|
|
|
|
|
|
class DrBotzoBackend(object):
|
|
"""Basic HTTP requests API, wrapped with some authentication and config stuff."""
|
|
|
|
def get(self, url, **kwargs):
|
|
return self.request('GET', url, **kwargs)
|
|
|
|
def post(self, url, **kwargs):
|
|
return self.request('POST', url, **kwargs)
|
|
|
|
def request(self, method, url, **kwargs):
|
|
"""Wrap requests.post with authentication and hostname settings."""
|
|
try:
|
|
response = requests.request(method, urljoin(config.DR_BOTZO_BACKEND_HOST, url),
|
|
auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
|
|
response.raise_for_status()
|
|
return response
|
|
except requests.ConnectionError as cex:
|
|
logger.exception("received a connection error during %s %s", method, url)
|
|
raise DrBotzoError(cex, detail="A connection error occurred.")
|
|
except requests.HTTPError as httpex:
|
|
logger.exception("received an HTTP error during %s %s", method, url)
|
|
try:
|
|
detail = httpex.response.json()['detail']
|
|
raise DrBotzoError(httpex, detail=detail)
|
|
except (ValueError, KeyError):
|
|
raise DrBotzoError(httpex, detail="An unexpected error occurred.")
|
|
|
|
|
|
dr_botzo = DrBotzoBackend()
|