-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnotebook.py
More file actions
244 lines (193 loc) · 5.58 KB
/
notebook.py
File metadata and controls
244 lines (193 loc) · 5.58 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
"""
ubank marimo notebook
Run:
uv run marimo run notebook.py
Edit:
uv run marimo edit notebook.py
"""
import marimo
__generated_with = "0.11.7"
app = marimo.App(width="medium")
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _(mo):
mo.md("""# ubank API Explo ~~it~~ rer""")
return
@app.cell
def _(mo):
uploaded_passkey = mo.ui.file(kind="area")
username = mo.ui.text(placeholder="ubank username")
password = mo.ui.text(placeholder="ubank password", kind="password")
register = mo.ui.run_button(kind="success", label="Register")
mo.md(
f"""
## Authentication
Access to ubank's API requires a passkey.
"""
)
return password, register, uploaded_passkey, username
@app.cell
def _(mo, password, register, uploaded_passkey, username):
import io
from cryptography.fernet import InvalidToken
from httpx import HTTPStatusError
from ubank import Passkey, add_passkey
# stub out for testing
# def add_passkey(username: str, password: str, passkey_name: str) -> Passkey:
# assert username
# assert passkey_name
# password = input("Enter security code ")
# return Passkey(passkey_name)
new_passkey_file = io.BytesIO()
if register.value:
try:
add_passkey(username.value, password.value, passkey_name="notebook.py").dump(
new_passkey_file, password=password.value
)
except HTTPStatusError as e:
print(f"Passkey registration failed: {e.__notes__[0]}")
new_passkey = mo.download(
new_passkey_file.getvalue(),
filename="passkey.txt",
label="passkey",
)
load_status = "⚪️"
if uploaded_passkey.contents():
try:
Passkey.load(io.BytesIO(uploaded_passkey.contents()), password.value)
load_status = "🟢"
except InvalidToken:
load_status = "🔴"
mo.hstack(
[
mo.md(
f"""
### Create passkey
{register} a new {new_passkey} with ubank:
{username} {password}
You will be prompted for your security code.
"""
),
mo.md(
f"""
### Load passkey {load_status}
Decrypt an existing passkey file with your {password}:
{uploaded_passkey}
"""
),
],
justify="start",
gap=2.5,
)
return (
HTTPStatusError,
InvalidToken,
Passkey,
add_passkey,
io,
load_status,
new_passkey,
new_passkey_file,
)
@app.cell
def _(mo):
create_client = mo.ui.run_button(label="Create")
mo.md(
f"""
## API
{create_client} an API client with your passkey.
"""
)
return (create_client,)
@app.cell
def _(
InvalidToken,
Passkey,
create_client,
io,
mo,
new_passkey_file,
password,
uploaded_passkey,
):
from ubank import Client
if create_client.value:
try:
with mo.status.spinner():
passkey = Passkey.load(
# Coalesce bytes from uploaded or new passkey.
io.BytesIO(uploaded_passkey.contents() or new_passkey_file.getvalue()),
password.value,
)
client = Client(passkey)
devices = client.get_devices(deviceUuid=passkey.device_id)
mo.output.append(
mo.md(
f"""
### Devices
You've authenticated using passkey *{passkey.name}*.
The following security devices are registered with ubank:
{mo.ui.table([device.model_dump() for device in devices])}
"""
)
)
except InvalidToken:
pass
return Client, client, devices, passkey
@app.cell
def _(mo):
get_balances = mo.ui.run_button(label="Get")
mo.md(
f"""
### Accounts
{get_balances} account balances.
"""
)
return (get_balances,)
@app.cell
def _(client, get_balances, mo):
if get_balances.value:
with mo.status.spinner():
bank = client.get_linked_banks().linkedBanks[0]
mo.output.append(
mo.tree(
[
account.model_dump(include={"number", "label", "type", "balance"})
for account in bank.accounts
]
)
)
return (bank,)
@app.cell
def _(mo):
from ubank import Filter
search_transactions = mo.ui.run_button(label="Search")
from_date = mo.ui.date()
to_date = mo.ui.date()
limit = mo.ui.number(value=5)
mo.md(
f"""
### Transactions
{search_transactions} for transactions between {from_date} and {to_date}.
Limit the number of results to {limit}.
"""
)
return Filter, from_date, limit, search_transactions, to_date
@app.cell
def _(Filter, client, from_date, mo, search_transactions, to_date):
if search_transactions.value:
with mo.status.spinner():
search_response = client.summarise_transactions(
body=Filter(fromDate=from_date.value, toDate=to_date.value)
)
mo.output.append(
mo.ui.table(
search_response.model_dump(mode="json", exclude_none=True)["transactions"]
)
)
return (search_response,)
if __name__ == "__main__":
app.run()