fix(reports): Update examples to use new api

This commit is contained in:
Mike A.
2025-07-16 19:59:04 +02:00
parent bbdd784e6a
commit 17e2987ab3
4 changed files with 85 additions and 31 deletions

View File

@@ -1,15 +1,14 @@
# ruff: noqa: ASYNC230
from __future__ import annotations
from findmy.reports import (
AppleAccount,
AsyncAppleAccount,
BaseAnisetteProvider,
LoginState,
SmsSecondFactorMethod,
TrustedDeviceSecondFactorMethod,
)
ACCOUNT_STORE = "account.json"
from findmy.reports.anisette import LocalAnisetteProvider, RemoteAnisetteProvider
def _login_sync(account: AppleAccount) -> None:
@@ -66,27 +65,45 @@ async def _login_async(account: AsyncAppleAccount) -> None:
await method.submit(code)
def get_account_sync(anisette: BaseAnisetteProvider) -> AppleAccount:
def get_account_sync(
store_path: str,
anisette_url: str | None,
libs_path: str | None,
) -> AppleAccount:
"""Tries to restore a saved Apple account, or prompts the user for login otherwise. (sync)"""
acc = AppleAccount(anisette=anisette)
acc_store = "account.json"
try:
acc.from_json(acc_store)
acc = AppleAccount.from_json(store_path, anisette_libs_path=libs_path)
except FileNotFoundError:
ani = (
LocalAnisetteProvider(libs_path=libs_path)
if anisette_url is None
else RemoteAnisetteProvider(anisette_url)
)
acc = AppleAccount(ani)
_login_sync(acc)
acc.to_json(acc_store)
acc.to_json(store_path)
return acc
async def get_account_async(anisette: BaseAnisetteProvider) -> AsyncAppleAccount:
async def get_account_async(
store_path: str,
anisette_url: str | None,
libs_path: str | None,
) -> AsyncAppleAccount:
"""Tries to restore a saved Apple account, or prompts the user for login otherwise. (async)"""
acc = AsyncAppleAccount(anisette=anisette)
acc_store = "account.json"
try:
acc.from_json(acc_store)
acc = AsyncAppleAccount.from_json(store_path, anisette_libs_path=libs_path)
except FileNotFoundError:
ani = (
LocalAnisetteProvider(libs_path=libs_path)
if anisette_url is None
else RemoteAnisetteProvider(anisette_url)
)
acc = AsyncAppleAccount(ani)
await _login_async(acc)
acc.to_json(acc_store)
acc.to_json(store_path)
return acc