Skip to content

Commit 1cdd628

Browse files
committed
refactor(tool/email_organizer/test): cache auth records
1 parent 5d6ae81 commit 1cdd628

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

npiai/tools/email_organizer/__test__/invoice_organizer.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,45 @@
44
from npiai import Context
55
from npiai.tools.outlook import Outlook
66
from npiai.tools.email_organizer import EmailOrganizer
7-
from azure.identity import InteractiveBrowserCredential
7+
from azure.identity import (
8+
InteractiveBrowserCredential,
9+
TokenCachePersistenceOptions,
10+
AuthenticationRecord,
11+
)
12+
13+
token_cache = ".cache/outlook_token_cache.json"
814

915

1016
async def main():
17+
# authenticate
18+
authentication_record = None
19+
20+
if os.path.exists(token_cache):
21+
with open(token_cache, "r") as f:
22+
authentication_record = AuthenticationRecord.deserialize(f.read())
23+
1124
creds = InteractiveBrowserCredential(
1225
client_id=os.environ.get("AZURE_CLIENT_ID", None),
1326
# tenant_id=os.environ.get("AZURE_TENANT_ID", None),
1427
tenant_id="common",
28+
cache_persistence_options=TokenCachePersistenceOptions(),
29+
authentication_record=authentication_record,
1530
)
1631

32+
record = creds.authenticate(scopes=["Mail.Read", "Mail.Send"])
33+
34+
os.makedirs(os.path.dirname(token_cache), exist_ok=True)
35+
36+
with open(token_cache, "w") as f:
37+
f.write(record.serialize())
38+
1739
async with EmailOrganizer(provider=Outlook(creds)) as tool:
40+
# list emails
1841
email_list = [email async for email in tool.list_inbox_stream(limit=10)]
1942

2043
print("Raw email list:", json.dumps(email_list, indent=4, ensure_ascii=False))
2144

45+
# filter invoice-like emails
2246
filtered_emails = []
2347

2448
async for result in tool.filter_stream(
@@ -34,6 +58,7 @@ async def main():
3458
f'Subject: {result["email"]["subject"]}, Matched: {result["matched"]}'
3559
)
3660

61+
# summarize invoice-like emails
3762
async for item in tool.summarize_stream(
3863
ctx=Context(),
3964
email_or_id_list=filtered_emails,

0 commit comments

Comments
 (0)