Files
LitePan/drivers/OneDrive/api.py
T
KingandCursor 463045a4f1 新增123云盘Open与OneDrive驱动
- 接入123云盘Open与OneDrive基础驱动能力,补充OAuth映射与图标资源
- 优化账号连通性测试和驱动实例复用,避免临时测试串用旧驱动
- 增加上传速度显示,上传文件夹跳过空文件和常见系统垃圾文件
- 优化前台删除、重命名、移动后的列表刷新体验
- 调整添加账号弹窗、STRM空状态和深色模式细节

Co-authored-by: Cursor <[email protected]>
2026-06-05 21:10:12 +08:00

54 lines
1.8 KiB
Python

"""OneDrive / Microsoft Graph API 端点与响应辅助方法。"""
from typing import Any, Dict
class OneDriveAPI:
BASE_URL = "https://graph.microsoft.com/v1.0"
USER_AGENT = "LitePan/0.2.0-beta"
ENDPOINTS = {
"me": "/me",
"drive": "/me/drive",
"root": "/me/drive/root",
"root_children": "/me/drive/root/children",
"path_item": "/me/drive/root:/{item_path}:",
"path_children": "/me/drive/root:/{item_path}:/children",
"item": "/me/drive/items/{item_id}",
"item_children": "/me/drive/items/{item_id}/children",
"copy": "/me/drive/items/{item_id}/copy",
}
HEADERS = {
"User-Agent": USER_AGENT,
"Accept": "application/json",
}
class OneDriveApiHelper:
@staticmethod
def build_headers(access_token: str = "") -> Dict[str, str]:
headers = OneDriveAPI.HEADERS.copy()
if access_token:
headers["Authorization"] = f"Bearer {access_token}"
return headers
@staticmethod
def extract_error_message(response: Dict[str, Any]) -> str:
error = response.get("error") if isinstance(response, dict) else None
if isinstance(error, dict):
return str(error.get("message") or error.get("code") or error)
return str(response or "")
@staticmethod
def is_token_expired(status: int, response: Dict[str, Any] = None) -> bool:
if status in (401, 403):
return True
error = response.get("error") if isinstance(response, dict) else None
code = str(error.get("code") or "").lower() if isinstance(error, dict) else ""
message = str(error.get("message") or "").lower() if isinstance(error, dict) else ""
return (
"invalidauthenticationtoken" in code
or "token" in message and ("expired" in message or "invalid" in message)
)