-
Notifications
You must be signed in to change notification settings - Fork 0
/
starling.py
78 lines (70 loc) · 3.06 KB
/
starling.py
1
2
3
4
5
6
7
8
9
10
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
72
73
74
75
76
77
78
from datetime import datetime
import requests
user_agent = "ElasticStar"
class Starling(object):
def __init__(self, access_token, sandbox=True):
self.access_token = access_token
if sandbox:
self.base_url = "https://api-sandbox.starlingbank.com/api/v2/"
else:
self.base_url = "https://api.starlingbank.com/api/v2/"
self.timestamp_format = "%Y-%m-%dT%H:%M:%SZ"
def get_accounts(self):
"""
Get an account holder's bank accounts
:return: A list of the accounts that can be accessed using the access_token.
"""
headers = {
'Authorization': "Bearer " + self.access_token,
'User-Agent': user_agent
}
response = requests.get(self.base_url + "accounts", headers=headers)
response.raise_for_status()
return response.json()['accounts']
def get_saving_spaces(self, account_uid):
"""
Get an account holder's saving spaces
:param account_uid The unique identifier for this account
:return: A list of the saving spaces that can be accessed using the access_token in the given account.
"""
headers = {
'Authorization': "Bearer " + self.access_token,
'User-Agent': user_agent
}
response = requests.get(self.base_url + "account/" +
account_uid + "/savings-goals",
headers=headers)
response.raise_for_status()
return response.json()
def get_transaction_feed(self, account_uid):
"""
Gets all transactions generated from the given account
:param account_uid: The unique identifier for this account
:return: A list of the transactions that can be accessed using the access_token in the given account.
"""
headers = {
'Authorization': "Bearer " + self.access_token,
'User-Agent': user_agent
}
response = requests.get(self.base_url + "feed/account/" + account_uid + "/settled-transactions-between?" +
"minTransactionTimestamp=" + "1000-01-01T00:00:00Z" +
"&"
"maxTransactionTimestamp=" + datetime.utcnow().strftime(self.timestamp_format),
headers=headers
)
response.raise_for_status()
return response.json()['feedItems']
@staticmethod
def generate_elastic_bulk_actions(transaction_feed):
"""
Converts the given transaction feed into a function that generates actions for interacting with the Elastic API
:param transaction_feed: A list of the transactions
:return: Actions for the Elastic API. To be used with the bulk helper function in the Elastic Python API
"""
for feedItem in transaction_feed:
document = {
"_id": feedItem['feedItemUid']
}
feedItem.pop('feedItemUid')
document.update(feedItem)
yield document