1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
"""Wrap requests functions with monads."""
import logging
import requests
from requests.models import Response
from pymonad.either import Left, Right, Either
logger = logging.getLogger(__name__)
# HTML Status codes indicating a successful request.
SUCCESS_CODES = (200, 201, 202, 203, 204, 205, 206, 207, 208, 226)
def get(url, params=None, **kwargs) -> Either:
"""
A wrapper around `requests.get` function.
Takes the same arguments as `requests.get`.
:rtype: pymonad.either.Either
"""
try:
resp = requests.get(url, params=params, **kwargs)
if resp.status_code in SUCCESS_CODES:
return Right(resp.json())
return Left(resp)
except requests.exceptions.RequestException as exc:
return Left(exc)
def post(url, data=None, json=None, **kwargs) -> Either:
"""
A wrapper around `requests.post` function.
Takes the same arguments as `requests.post`.
:rtype: pymonad.either.Either
"""
try:
resp = requests.post(url, data=data, json=json, **kwargs)
if resp.status_code in SUCCESS_CODES:
return Right(resp.json())
return Left(resp)
except requests.exceptions.RequestException as exc:
return Left(exc)
def make_either_error_handler(msg):
"""Make generic error handler for pymonads Either objects."""
def __fail__(error):
if issubclass(type(error), Exception):
logger.debug("\n\n%s (Exception)\n\n", msg, exc_info=True)
raise error
if issubclass(type(error), Response):
try:
_data = error.json()
except Exception as _exc:
raise Exception(error.content) from _exc
raise Exception(_data)
logger.debug("\n\n%s\n\n", msg)
raise Exception(error)
return __fail__
|