-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.py
More file actions
253 lines (193 loc) · 7.89 KB
/
API.py
File metadata and controls
253 lines (193 loc) · 7.89 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import time
import os
import requests
import json
from dotenv import load_dotenv
#from dotenv import load_dotenv
load_dotenv() # Load environment variables from .env
API_KEY =os.getenv("API_KEY")
user_id = os.getenv("user_id")
## class to manage CORE API's.
class Core:
def __init__(self, api_key):
self.api_key = api_key
def get_auth_token(self):
url = "https://au-api.basiq.io/token"
headers = {
"accept": "application/json",
"basiq-version": "3.0",
"content-type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + self.api_key
}
response = requests.post(url, headers=headers)
response_data = response.json() # Extract JSON content from the response
access_token = response_data["access_token"] # Get the access token from the JSON data
return access_token
#response = requests.post(url, headers=headers)
#access_token = json.loads(response)["access_token"] #strips just the bearer token from the json response
#return response.text
def get_user(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": "Bearer " f"{access_token}"
}
response = requests.get(url, headers=headers)
return response.text
def create_user(self, payload, access_token):
url = "https://au-api.basiq.io/users"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {access_token}"
}
response = requests.post(url, json=payload, headers=headers)
return response.text
def retreive_user(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}"
headers = {
"accept": "application/json",
"authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
return response.text
def update_user(self, user_data, access_token):
#example user_data
#"email": "email@gmail.com",
#"mobile": "+61423330000",
#"firstName": "First Name",
#"lastName": "Surname"
url = f"https://au-api.basiq.io/users/{user_id}"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {access_token}"
}
response = requests.post(url, json=user_data, headers=headers)
return response.text
def create_auth_link(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/auth_link"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {access_token}"
}
try:
response = requests.post(url, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.exceptions.RequestException as e:
return str(e)
def retrieve_auth_link(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/auth_link"
headers = {
"accept": "application/json",
"authorization": f"Bearer {access_token}"
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.exceptions.RequestException as e:
return str(e)
## End of CORE Class
class Data:
##__init__
def __init__(self):
pass
#gets all accounts that a user has. Requires user ID and access token.
def all_accounts(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/accounts"
headers = {
"accept": "application/json",
"authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
return response.text
#returns details on specific account. Requires account_ID and access token. Account ID returned in list of all accounts.
def get_account(self, user_id, account_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/accounts/{account_id}"
headers = {
"accept": "application/json",
"authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
return response.text
#gets last 500 transactions for the user
def get_transactions(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/transactions"
headers = {
"accept": "application/json",
"authorization": f"Bearer {access_token}"
}
response = requests.get(url, headers=headers)
return response.text
# gets details on a particular transaction
def get_transaction(self, user_id, transaction_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/transactions/{transaction_id}"
headers = {
"accept": "application/json",
"authorization": f"Bearer {access_token}"
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.exceptions.RequestException as e:
return str(e)
#create an affordability summary for a user
def get_affordability_report(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/affordability"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {access_token}"
}
try:
response = requests.post(url, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.exceptions.RequestException as e:
return str(e)
# create an expense summary for a user
def get_expenses(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/expenses"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {access_token}"
}
try:
response = requests.post(url, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.exceptions.RequestException as e:
return str(e)
# create an income summary for a user
def get_income(self, user_id, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/income"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {access_token}"
}
try:
response = requests.post(url, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.exceptions.RequestException as e:
return str(e)
# Error handling class
class HTTPError(Exception):
def __init__(self, response):
self.correlationId = response["correlationId"]
self.data = response["data"]
messages = []
for message in self.data:
messages.append(message["detail"])
self.msg = ", ".join(messages)
def get_message(self):
return self.msg
def __str__(self):
return "HTTPError: %s" % self.msg