import asyncio
import logging
from authframework import AuthFrameworkClient
from authframework.exceptions import AuthenticationError, AuthFrameworkError
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def main() -> None:
client = AuthFrameworkClient("http://localhost:8080")
try:
logger.info("=== Login Example ===")
login_response = await client.auth.login("user@example.com", "password")
logger.info(
"Login successful! Token expires in %s seconds",
login_response.get("expires_in", "unknown"),
)
profile = await client.user.get_profile()
logger.info(
"Welcome, %s! (ID: %s)",
profile.get("display_name", "User"),
profile.get("user_id", "unknown"),
)
logger.info("=== Profile Update Example ===")
await client.user.update_profile(
profile_data={
"display_name": "Updated Name",
"preferences": {"theme": "dark", "notifications": True},
},
)
logger.info("Profile updated successfully!")
logger.info("=== MFA Setup Example ===")
if not profile["mfa_enabled"]:
mfa_setup = await client.mfa.enable_totp()
logger.info("MFA Secret: %s", mfa_setup["secret"])
logger.info("QR Code URL: %s", mfa_setup["qr_code"])
logger.info("Scan the QR code with your authenticator app")
logger.info("MFA enabled successfully!")
else:
logger.info("MFA is already enabled for this user")
logger.info("=== OAuth Example ===")
auth_url = await client.oauth.authorize(
client_id="example-app",
redirect_uri="https://example.com/callback",
scope="read write",
state="random-state-123",
)
logger.info("OAuth Authorization URL: %s", auth_url)
logger.info("=== Health Check Example ===")
health = await client.health_check()
logger.info("Service status: %s", health["status"])
logger.info("Service version: %s", health["version"])
logger.info("=== Admin Functions Example ===")
try:
stats = await client.admin.get_system_stats()
logger.info("Total users: %s", stats["total_users"])
logger.info("Active sessions: %s", stats["active_sessions"])
users = await client.user.get_users(limit=5)
logger.info("Found %s users", len(users.get("users", [])))
except AuthenticationError:
logger.info("User doesn't have admin permissions")
logger.info("=== Logout Example ===")
await client.auth.logout()
logger.info("Logged out successfully!")
except AuthenticationError as e:
logger.exception("Authentication failed: %s", e.message)
except AuthFrameworkError as e:
logger.exception("API error: %s (Status: %s)", e.message, e.status_code)
except Exception:
logger.exception("Unexpected error occurred")
finally:
await client.close()
async def context_manager_example() -> None:
logger.info("=== Context Manager Example ===")
try:
async with AuthFrameworkClient("http://localhost:8080") as client:
await client.auth.login("user@example.com", "password")
profile = await client.user.get_profile()
logger.info("User: %s", profile["display_name"])
except AuthFrameworkError:
logger.exception("API error occurred")
async def concurrent_requests_example() -> None:
logger.info("=== Concurrent Requests Example ===")
async with AuthFrameworkClient("http://localhost:8080") as client:
await client.auth.login("admin@example.com", "admin_password")
tasks = [
client.health_check(),
client.user.get_profile(),
client.admin.get_system_stats(),
]
try:
results = await asyncio.gather(*tasks, return_exceptions=True)
for i, result in enumerate(results):
if isinstance(result, Exception):
logger.error("Task %s failed: %s", i, result)
else:
logger.info("Task %s completed successfully", i)
except Exception:
logger.exception("Concurrent requests failed")
if __name__ == "__main__":
asyncio.run(main())
asyncio.run(context_manager_example())
asyncio.run(concurrent_requests_example())