diff --git a/starling b/starling index b690003..38d4fd5 100755 --- a/starling +++ b/starling @@ -189,7 +189,12 @@ class StarlingClient: print(f'Response:') print(response.text) sys.exit(1) - return json.loads(response.text, object_hook=lambda obj: types.SimpleNamespace(**obj)) + if response.status_code == 204: # No Content + return None + try: + return json.loads(response.text, object_hook=lambda obj: types.SimpleNamespace(**obj)) + except: + sys.exit(f'ERROR: failed to parse response:\n{response}') def make_signature(self, method, url, date, digest): path = urllib.parse.urlparse(url).path @@ -349,6 +354,18 @@ class StarlingClient: print(f'Failed to create account: {response}') sys.exit(1) + def account_del(self, account_uid): + payee_uid = self.get_payee_uid_from_account(account_uid) + self.delete(f'payees/{payee_uid}/account/{account_uid}') + print(f'Successfully deleted account {account_uid} for payee {payee_uid}') + + def get_payee_uid_from_account(self, account_uid): + for payee in self.payees().payees: + for account in payee.accounts: + if account.payeeAccountUid == account_uid: + return payee.payeeUid + sys.exit(f'ERROR: Can\'t find payee for account {account_uid}') + def pay(self, payee_uid, amount, ref): match = self.amount_re.match(amount) if match is None: @@ -408,7 +425,8 @@ parser = argparse.ArgumentParser( payees - list the customer's payees payee add - adds a new payee payee del - deletes an existing payee - account add - adds a new account to a payee + account add - adds a new account to a payee + account del - removes an account from a payee pay - pay an existing payee ''') parser.add_argument('action', help='which action to perform', nargs='*') @@ -455,7 +473,7 @@ elif action == 'account': parser.error(f'Too few arguments for "{action} {subaction}" action') if count > 2: parser.error(f'Too many arguments for "{action} {subaction}" action') - client.account_del(*actuon_args) + client.account_del(*action_args) else: parser.error(f'Unknown "{action} {subaction}" action') elif action == 'pay':