Skip to content

resources

Monzo API 'accounts' resource.

AccountsResource dataclass

Bases: BaseResource

Monzo API 'accounts' resource.

Note

Monzo API docs: https://docs.monzo.com/#accounts

Source code in pymonzo/accounts/resources.py
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
65
66
67
68
69
70
71
@dataclass
class AccountsResource(BaseResource):
    """Monzo API 'accounts' resource.

    Note:
        Monzo API docs: https://docs.monzo.com/#accounts
    """

    _cached_accounts: List[MonzoAccount] = field(default_factory=list)

    def get_default_account(self) -> MonzoAccount:
        """If the user has only one active account, treat it as the default account.

        Returns:
            User's active account.

        Raises:
            CannotDetermineDefaultAccount: If user has more than one active account.
        """
        accounts = self.list()

        # If there is only one account, return it
        if len(accounts) == 1:
            return accounts[0]

        # Otherwise check if there is only one active (non-closed) account
        active_accounts = [account for account in accounts if not account.closed]

        if len(active_accounts) == 1:
            return active_accounts[0]

        raise CannotDetermineDefaultAccount(
            "Cannot determine default account. "
            "You need to explicitly pass an 'account_id' argument."
        )

    def list(self, *, refresh: bool = False) -> List[MonzoAccount]:
        """Return a list of user's Monzo accounts.

        It's often used when deciding whether to require explicit account ID
        or use the only active one, so we cache the response by default.

        Note:
            Monzo API docs: https://docs.monzo.com/#list-accounts

        Arguments:
            refresh: Whether to refresh the cached list of accounts.

        Returns:
            A list of user's Monzo accounts.
        """
        if not refresh and self._cached_accounts:
            return self._cached_accounts

        endpoint = "/accounts"
        response = self._get_response(method="get", endpoint=endpoint)

        accounts = [MonzoAccount(**account) for account in response.json()["accounts"]]
        self._cached_accounts = accounts

        return accounts

get_default_account()

If the user has only one active account, treat it as the default account.

Returns:

Type Description
MonzoAccount

User's active account.

Raises:

Type Description
CannotDetermineDefaultAccount

If user has more than one active account.

Source code in pymonzo/accounts/resources.py
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
def get_default_account(self) -> MonzoAccount:
    """If the user has only one active account, treat it as the default account.

    Returns:
        User's active account.

    Raises:
        CannotDetermineDefaultAccount: If user has more than one active account.
    """
    accounts = self.list()

    # If there is only one account, return it
    if len(accounts) == 1:
        return accounts[0]

    # Otherwise check if there is only one active (non-closed) account
    active_accounts = [account for account in accounts if not account.closed]

    if len(active_accounts) == 1:
        return active_accounts[0]

    raise CannotDetermineDefaultAccount(
        "Cannot determine default account. "
        "You need to explicitly pass an 'account_id' argument."
    )

list(*, refresh=False)

Return a list of user's Monzo accounts.

It's often used when deciding whether to require explicit account ID or use the only active one, so we cache the response by default.

Note

Monzo API docs: https://docs.monzo.com/#list-accounts

Parameters:

Name Type Description Default
refresh bool

Whether to refresh the cached list of accounts.

False

Returns:

Type Description
List[MonzoAccount]

A list of user's Monzo accounts.

Source code in pymonzo/accounts/resources.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def list(self, *, refresh: bool = False) -> List[MonzoAccount]:
    """Return a list of user's Monzo accounts.

    It's often used when deciding whether to require explicit account ID
    or use the only active one, so we cache the response by default.

    Note:
        Monzo API docs: https://docs.monzo.com/#list-accounts

    Arguments:
        refresh: Whether to refresh the cached list of accounts.

    Returns:
        A list of user's Monzo accounts.
    """
    if not refresh and self._cached_accounts:
        return self._cached_accounts

    endpoint = "/accounts"
    response = self._get_response(method="get", endpoint=endpoint)

    accounts = [MonzoAccount(**account) for account in response.json()["accounts"]]
    self._cached_accounts = accounts

    return accounts