Skip to content

utils

pymonzo utils.

WSGIApp

Bare-bones WSGI app made for retrieving the OAuth callback.

Source code in pymonzo/utils.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class WSGIApp:
    """Bare-bones WSGI app made for retrieving the OAuth callback."""

    last_request_uri = ""

    def __call__(
        self,
        environ: dict,
        start_response: Callable[[str, List[Tuple[str, str]]], None],
    ) -> List[bytes]:
        """Implement WSGI interface and save the URL of the callback."""
        start_response("200 OK", [("Content-type", "text/plain; charset=utf-8")])
        self.last_request_uri = request_uri(environ)
        msg = "Monzo OAuth authorization complete."
        return [msg.encode("utf-8")]

__call__(environ, start_response)

Implement WSGI interface and save the URL of the callback.

Source code in pymonzo/utils.py
89
90
91
92
93
94
95
96
97
98
def __call__(
    self,
    environ: dict,
    start_response: Callable[[str, List[Tuple[str, str]]], None],
) -> List[bytes]:
    """Implement WSGI interface and save the URL of the callback."""
    start_response("200 OK", [("Content-type", "text/plain; charset=utf-8")])
    self.last_request_uri = request_uri(environ)
    msg = "Monzo OAuth authorization complete."
    return [msg.encode("utf-8")]

empty_dict_to_none(v)

Return None if the passed value is an empty dict, otherwise do nothing.

Parameters:

Name Type Description Default
v Any

Value to check.

required

Returns:

Type Description
Any

Passed value or None if it's an empty dict.

Source code in pymonzo/utils.py
39
40
41
42
43
44
45
46
47
48
49
50
51
def empty_dict_to_none(v: Any) -> Any:
    """Return `None` if the passed value is an empty dict, otherwise do nothing.

    Arguments:
        v: Value to check.

    Returns:
        Passed value or `None` if it's an empty dict.
    """
    if isinstance(v, dict) and not bool(v):
        return None

    return v

empty_str_to_none(v)

Return None passed value is an empty string, otherwise do nothing.

Parameters:

Name Type Description Default
v Any

Value to check.

required

Returns:

Type Description
Any

Passed value or None if it's an empty string.

Source code in pymonzo/utils.py
24
25
26
27
28
29
30
31
32
33
34
35
36
def empty_str_to_none(v: Any) -> Any:
    """Return `None` passed value is an empty string, otherwise do nothing.

    Arguments:
        v: Value to check.

    Returns:
        Passed value or `None` if it's an empty string.
    """
    if isinstance(v, str) and v == "":
        return None

    return v

format_currency(amount, currency)

Format passed amount with two decimal places.

Used as a fallback when babel isn't available.

Parameters:

Name Type Description Default
amount float

Amount of money.

required
currency str

Money currency. Unused here, but needed to match the signature with babel.numbers.format_currency.

required

Returns:

Type Description
str

Passed amount with two decimal places.

Source code in pymonzo/utils.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def format_currency(amount: float, currency: str) -> str:
    """Format passed amount with two decimal places.

    Used as a fallback when `babel` isn't available.

    Arguments:
        amount: Amount of money.
        currency: Money currency. Unused here, but needed to match the signature
            with `babel.numbers.format_currency`.

    Returns:
        Passed amount with two decimal places.
    """
    return f"{amount:.2f}"

format_datetime(dt)

Format passed datetime in user locale.

Used as a fallback when babel isn't available.

Parameters:

Name Type Description Default
dt datetime

Datetime to format.

required

Returns:

Type Description
str

Passed datetime formatted in user locale.

Source code in pymonzo/utils.py
54
55
56
57
58
59
60
61
62
63
64
65
def format_datetime(dt: datetime) -> str:
    """Format passed `datetime` in user locale.

    Used as a fallback when `babel` isn't available.

    Arguments:
        dt: Datetime to format.

    Returns:
        Passed `datetime` formatted in user locale.
    """
    return dt.strftime(locale.nl_langinfo(locale.D_T_FMT))

get_authorization_response_url(host, port)

Get OAuth authorization response URL.

It's done by creating a bare-bones HTTP server and retrieving a single request, the OAuth callback.

Parameters:

Name Type Description Default
host str

temporary HTTP server host name.

required
port int

temporary HTTP server port.

required

Returns:

Type Description
str

URL of the OAuth authorization response.

Source code in pymonzo/utils.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def get_authorization_response_url(host: str, port: int) -> str:
    """Get OAuth authorization response URL.

    It's done by creating a bare-bones HTTP server and retrieving a single request,
    the OAuth callback.

    Arguments:
        host: temporary HTTP server host name.
        port: temporary HTTP server port.

    Returns:
        URL of the OAuth authorization response.
    """
    wsgi_app = WSGIApp()
    with make_server(host, port, wsgi_app) as server:  # type: ignore
        server.handle_request()

    return wsgi_app.last_request_uri

n_days_ago(n)

Return datetime that was n days ago.

Parameters:

Name Type Description Default
n int

Number of days to go back.

required

Returns:

Type Description
datetime

Datetime that was n days ago.

Source code in pymonzo/utils.py
10
11
12
13
14
15
16
17
18
19
20
21
def n_days_ago(n: int) -> datetime:
    """Return datetime that was `n` days ago.

    Arguments:
        n: Number of days to go back.

    Returns:
        Datetime that was `n` days ago.
    """
    today = datetime.now()
    delta = timedelta(days=n)
    return today - delta