Skip to content

Commit

Permalink
Create classes to store data and reduce api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
CamLamb committed Mar 25, 2018
1 parent 8282daa commit a28fc0e
Showing 1 changed file with 116 additions and 1 deletion.
117 changes: 116 additions & 1 deletion MonzoOAuth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class MonzoOAuth:

auth_uri = 'https://auth.monzo.com/'
token_uri = 'https://api.monzo.com/oauth2/token'
api_url = 'https://api.monzo.com/'
Expand Down Expand Up @@ -79,3 +78,119 @@ def credentials_to_dict(self, credentials):
'client_secret': credentials.client_secret,
'scopes': ''
}


class User:
def __init__(self, monzo: MonzoOAuth):
self.monzo = monzo

def get_accounts(self):
response = self.monzo.query('accounts')

accounts = []
for account in response['accounts']:
accounts.append(Account(monzo=self.monzo, id=account['id'], account=account))

return accounts


class Account:
def __init__(self, monzo: MonzoOAuth, id: str, account: dict = None):
self.monzo = monzo

if not account:
accounts = monzo.query('accounts')['accounts']
for a in accounts:
if a['id'] == id:
account = a
break

self.id = account['id']
self.description = account['description']
self.created = account['created']
self.balance = None
self.spent_today = None

def get_balance(self):
if not self.balance:
balance = self.monzo.query('balance', {'account_id': self.id})
self.balance = Price(amount=balance['balance'], currency_code=balance['currency'])
return self.balance

def get_spent_today(self):
if not self.spent_today:
balance = self.monzo.query('balance', {'account_id': self.id})
self.spent_today = Price(amount=balance['spend_today'], currency_code=balance['currency'])
return self.spent_today

def get_transactions(self):
response = self.monzo.query('transactions', {'account_id': self.id})

transactions = []
for transaction in response['transactions']:
transactions.append(Transaction(monzo=self.monzo, id=transaction['id'], transaction=transaction))_
return transactions

def get_pots(self):
response = self.monzo.query('pots')

pots = []
for pot in response['pots']:
pots.append(Pot(monzo=self.monzo, id=pot['id'], pot=pot))

return pots


class Pot:
def __init__(self, monzo: MonzoOAuth, id: str, pot: dict = None):
self.monzo = monzo

if not pot:
pots = monzo.query('pots')['pots']
for p in pots:
if p['id'] == id:
pot = p
break

self.id = pot['id']
self.name = pot['name']
self.style = pot['style']
self.balance = pot['balance']
self.currency = pot['currency']
self.created = pot['created']
self.updated = pot['updated']
self.deleted = pot['deleted']

def get_name(self):
return self.name

def get_balance(self):
return Price(amount=self.balance, currency_code=self.currency)


class Transaction:

def __init__(self, monzo: MonzoOAuth, id: str, transaction: dict = None):
self.monzo = monzo

if not transaction:
transaction = monzo.query('transactions/' + id, {'expand[]': 'merchant'})['transaction']

self.id = transaction['id']
self.amount = transaction['amount']
self.currency = transaction['currency']
self.account_balance = transaction['account_balance']
self.created = transaction['created']
self.description = transaction['description']
self.merchant = transaction['merchant']
self.notes = transaction['notes']
self.settled = transaction['settled']

def get_amount(self):
return Price(amount=self.amount, currency_code=self.currency)


class Price:
def __init__(self, amount, currency_code):
self.amount = amount
self.currency_code = currency_code

0 comments on commit a28fc0e

Please sign in to comment.