def test_class_pots_method(self, mocker, mocked_monzo, pots_api_response): """Test class `pots` method""" mocked_get_response = mocker.patch( 'pymonzo.monzo_api.MonzoAPI._get_response', ) mocked_get_response.return_value.json.return_value = pots_api_response assert mocked_monzo._cached_pots is None result = mocked_monzo.pots() mocked_get_response.assert_called_once_with( method='get', endpoint='/pots', ) pots_json = pots_api_response['pots'] expected_result = [MonzoPot(data=pot, context=mocked_monzo) for pot in pots_json] assert result == expected_result assert mocked_monzo._cached_pots == expected_result # Calling it again should fetch '_cached_pots' mocked_get_response = mocker.patch( 'pymonzo.monzo_api.MonzoAPI._get_response', ) mocked_get_response.return_value.json.return_value = pots_api_response result = mocked_monzo.pots() assert mocked_get_response.call_count == 0 assert result == mocked_monzo._cached_pots # But calling it with 'refresh=True' should do an API request mocked_get_response = mocker.patch( 'pymonzo.monzo_api.MonzoAPI._get_response', ) mocked_get_response.return_value.json.return_value = pots_api_response assert mocked_monzo._cached_pots is not None result = mocked_monzo.pots(refresh=True) mocked_get_response.assert_called_once_with( method='get', endpoint='/pots', ) pots_json = pots_api_response['pots'] expected_result = [MonzoPot(data=pot, context=mocked_monzo) for pot in pots_json] assert result == expected_result assert mocked_monzo._cached_pots == expected_result
def pots(self, refresh=False): """ Returns a list of pots owned by the currently authorised user. Official docs: https://monzo.com/docs/#pots :param refresh: decides if the pots information should be refreshed. :type refresh: bool :returns: list of Monzo pots :rtype: list of MonzoPot """ if not refresh and self._cached_pots: return self._cached_pots endpoint = '/pots/listV1' response = self._get_response( method='get', endpoint=endpoint, ) pots_json = response.json()['pots'] pots = [MonzoPot(data=pot) for pot in pots_json] self._cached_pots = pots return pots
def pots(self, refresh=False, account_id=None): """ Returns a list of pots owned by the currently authorised user. Official docs: https://monzo.com/docs/#pots :param refresh: decides if the pots information should be refreshed. :type refresh: bool :returns: list of Monzo pots :rtype: list of MonzoPot """ if not refresh and self._cached_pots: return self._cached_pots if not account_id: if len(self.accounts()) == 1: account_id = self.accounts()[0].id else: raise ValueError("You need to pass account ID") endpoint = '/pots' response = self._get_response( method='get', endpoint=endpoint, params={ 'current_account_id': account_id, }, ) pots_json = response.json()['pots'] pots = [MonzoPot(data=pot) for pot in pots_json] self._cached_pots = pots return pots
def pot_withdraw(self, account_id=None, pot_id=None, amount=0): """ Withdraw money from a pot Official docs: https://monzo.com/docs/#withdraw-from-a-pot :param account_id: The account id to deposit into :type refresh: unicode :param pot_id: The pot id to withdraw from :type pot_id: unicode :param amount: The amount to transfer :type amount: int :returns: updated MonzoPot :rtype: MonzoPot """ endpoint = '/pots/{pot_id}/withdraw'.format(pot_id=pot_id) data = { 'destination_account_id': account_id, 'amount': amount, 'dedupe_id': str(uuid.uuid4()) } response = self._get_response(method='put', endpoint=endpoint, data=data) response_json = response.json() return MonzoPot(data=response_json, client=self)
def pot_deposit(self, account_id=None, pot_id=None, amount=0): """ Deposit amount of money into a pot Official docs: https://monzo.com/docs/#deposit-into-a-pot :param account_id: The account id to withdraw from :type refresh: unicode :param pot_id: The target pot id :type pot_id: unicode :param amount: The amount to transfer :type amount: int :returns: updated MonzoPot :rtype: MonzoPot """ endpoint = '/pots/{pot_id}/deposit'.format(pot_id=pot_id) data = { 'source_account_id': account_id, 'amount': amount, 'dedupe_id': str(uuid.uuid4()) } response = self._get_response(method='put', endpoint=endpoint, data=data) response_json = response.json() return MonzoPot(data=response_json, client=self)
def pot(self, pot_id): """ Returns a single pot by ID, owned by the currently authorised user. Official docs: https://monzo.com/docs/#pots :param pot_id: Pot ID :type pot_id: str :returns: Monzo pot :rtype: MonzoPot """ endpoint = '/pots/' + pot_id response = self._get_response( method='get', endpoint=endpoint, ) return MonzoPot(data=response.json(), context=self)
def test_class_withdraw_method(self, mocker, mocked_monzo, pots_api_response, accounts_api_response): """Test class `add` method""" mocked_get_response = mocker.patch( 'pymonzo.monzo_api.MonzoAPI._get_response', ) mocked_get_response.return_value.json.return_value = pots_api_response[ 'pots'][0] accounts_json = accounts_api_response['accounts'] pots_json = pots_api_response['pots'] mocked_monzo._cached_accounts = [ MonzoAccount(data=account, context=mocked_monzo) for account in accounts_json ] mocked_monzo._cached_pots = [ MonzoPot(data=pot, context=mocked_monzo) for pot in pots_json ] pot = mocked_monzo.pots()[0] expected_result = pot expected_result.balance = 2500 result = pot.withdraw(9845, mocked_monzo._cached_accounts[0], "abc") mocked_get_response.assert_called_once_with( method='put', endpoint='/pots/' + mocked_monzo._cached_pots[0].id + '/withdraw', body={ 'destination_account_id': mocked_monzo._cached_accounts[0].id, 'amount': 9845, 'dedupe_id': "abc", }, ) assert result is None assert pot == expected_result