genericize request methods in DrBotzoBackend
This commit is contained in:
parent
cfb7cdf6b2
commit
5660af1614
|
@ -22,18 +22,24 @@ class DrBotzoError(requests.HTTPError):
|
||||||
class DrBotzoBackend(object):
|
class DrBotzoBackend(object):
|
||||||
"""Basic HTTP requests API, wrapped with some authentication and config stuff."""
|
"""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):
|
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."""
|
"""Wrap requests.post with authentication and hostname settings."""
|
||||||
try:
|
try:
|
||||||
response = requests.post(urljoin(config.DR_BOTZO_BACKEND_HOST, url),
|
response = requests.request(method, urljoin(config.DR_BOTZO_BACKEND_HOST, url),
|
||||||
auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
|
auth=(config.DR_BOTZO_BACKEND_USER, config.DR_BOTZO_BACKEND_PASS), **kwargs)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response
|
return response
|
||||||
except requests.ConnectionError as cex:
|
except requests.ConnectionError as cex:
|
||||||
logger.exception("received a connection error during POST %s", url)
|
logger.exception("received a connection error during %s %s", method, url)
|
||||||
raise DrBotzoError(cex, detail="A connection error occurred.")
|
raise DrBotzoError(cex, detail="A connection error occurred.")
|
||||||
except requests.HTTPError as httpex:
|
except requests.HTTPError as httpex:
|
||||||
logger.exception("received an HTTP error during POST %s", url)
|
logger.exception("received an HTTP error during %s %s", method, url)
|
||||||
try:
|
try:
|
||||||
detail = httpex.response.json()['detail']
|
detail = httpex.response.json()['detail']
|
||||||
raise DrBotzoError(httpex, detail=detail)
|
raise DrBotzoError(httpex, detail=detail)
|
||||||
|
|
Loading…
Reference in New Issue