Files
any-auto-register/api/outlook.py
2026-04-07 01:09:14 +08:00

54 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import Any, Dict, List
from services.mail_imports import MailImportExecuteRequest, mail_import_registry
router = APIRouter(prefix="/outlook", tags=["微软邮箱Outlook / Hotmail"])
class OutlookBatchImportRequest(BaseModel):
data: str
enabled: bool = True
class OutlookBatchImportResponse(BaseModel):
total: int
success: int
failed: int
accounts: List[Dict[str, Any]]
errors: List[str]
@router.post("/batch-import", response_model=OutlookBatchImportResponse)
def batch_import_outlook(request: OutlookBatchImportRequest):
"""
批量导入微软邮箱Outlook / Hotmail账户
支持两种格式(每行一个账户,字段用 ---- 分隔):
- 邮箱----密码
- 邮箱----密码----client_id----refresh_token
运行时默认优先使用 Graph 后端读取邮件;若账号缺少 OAuth 凭据则自动回退到 IMAP。
"""
try:
strategy = mail_import_registry.get("microsoft")
result = strategy.execute(
MailImportExecuteRequest(
type="microsoft",
content=request.data,
enabled=request.enabled,
)
)
return OutlookBatchImportResponse(
total=result.summary.total,
success=result.summary.success,
failed=result.summary.failed,
accounts=list(result.meta.get("accounts") or []),
errors=result.errors,
)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
except RuntimeError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc