简道云fastapi

This commit is contained in:
z66
2025-11-07 17:48:49 +08:00
commit 073f0646a1
30 changed files with 5933 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
"""
核心模块初始化
统一初始化请求头管理器、模块注册表等核心组件
"""
from typing import Dict, Any, Callable
from app.core.header_manager import HeaderManager
from app.core.module_registry import ModuleRegistry, registry
class CoreManager:
"""核心管理器 - 统一管理所有核心组件"""
def __init__(self):
self.header_manager = HeaderManager
self.registry = registry
def initialize_modules(self, modules: Dict[str, Any]):
"""
初始化并注册所有模块
Args:
modules: 模块字典,格式为 {模块名: 模块实例}
"""
for module_name, module_instance in modules.items():
self.registry.register_module(module_name, module_instance)
def register_action(
self,
action_name: str,
handler: Callable,
module_name: str = "default",
**kwargs
):
"""
便捷方法:注册操作
Args:
action_name: 操作名称
handler: 处理函数
module_name: 模块名称
**kwargs: 其他配置参数
"""
self.registry.register_action(
action_name=action_name,
handler=handler,
module_name=module_name,
**kwargs
)
# 全局核心管理器实例
core_manager = CoreManager()
# 导出常用类和函数
__all__ = [
'core_manager',
'HeaderManager',
'ModuleRegistry',
'registry',
]
+118
View File
@@ -0,0 +1,118 @@
"""
请求头管理器
统一管理不同模块的请求头配置
"""
from typing import Dict, Optional
from dataclasses import dataclass, field
@dataclass
class HeaderConfig:
"""请求头配置"""
referer: Optional[str] = None
user_agent: Optional[str] = None
content_type: Optional[str] = None
authorization: Optional[str] = None
custom_headers: Dict[str, str] = field(default_factory=dict)
def to_dict(self) -> Dict[str, str]:
"""转换为字典格式"""
headers = {}
if self.referer:
headers['Referer'] = self.referer
if self.user_agent:
headers['User-Agent'] = self.user_agent
if self.content_type:
headers['Content-Type'] = self.content_type
if self.authorization:
headers['Authorization'] = self.authorization
# 添加自定义请求头
headers.update(self.custom_headers)
return headers
class HeaderManager:
"""请求头管理器"""
# 默认请求头配置
DEFAULT_HEADERS = HeaderConfig(
referer='https://yunxiu.f6car.cn/erp/view/index.html',
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
)
# F6系统登录请求头
F6_LOGIN_HEADERS = HeaderConfig(
referer='https://yunxiu.f6car.com/kzf6/login/confirm',
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/129.0.0.0'
)
# 简道云API请求头
JIANDAOYUN_API_HEADERS = HeaderConfig(
content_type='application/json',
# authorization 应该从配置中获取,这里只是示例
)
# 模块特定的请求头配置
_module_headers: Dict[str, HeaderConfig] = {
'default': DEFAULT_HEADERS,
'f6_login': F6_LOGIN_HEADERS,
'jiandaoyun_api': JIANDAOYUN_API_HEADERS,
}
@classmethod
def get_headers(cls, module_name: str = 'default', **overrides) -> Dict[str, str]:
"""
获取指定模块的请求头
Args:
module_name: 模块名称
**overrides: 覆盖的请求头配置
Returns:
请求头字典
"""
# 获取基础配置
config = cls._module_headers.get(module_name, cls.DEFAULT_HEADERS)
# 创建配置副本
header_config = HeaderConfig(
referer=overrides.get('referer', config.referer),
user_agent=overrides.get('user_agent', config.user_agent),
content_type=overrides.get('content_type', config.content_type),
authorization=overrides.get('authorization', config.authorization),
custom_headers={**config.custom_headers, **overrides.get('custom_headers', {})}
)
return header_config.to_dict()
@classmethod
def register_module_headers(cls, module_name: str, config: HeaderConfig):
"""
注册模块的请求头配置
Args:
module_name: 模块名称
config: 请求头配置
"""
cls._module_headers[module_name] = config
@classmethod
def update_module_headers(cls, module_name: str, **updates):
"""
更新模块的请求头配置
Args:
module_name: 模块名称
**updates: 要更新的配置项
"""
if module_name in cls._module_headers:
config = cls._module_headers[module_name]
for key, value in updates.items():
if hasattr(config, key):
setattr(config, key, value)
else:
config.custom_headers[key] = value
+116
View File
@@ -0,0 +1,116 @@
"""
模块注册和路由管理
提供统一的模块注册机制,方便添加新功能模块
"""
from typing import Dict, Callable, Optional, Any
from dataclasses import dataclass
@dataclass
class ActionConfig:
"""操作配置"""
handler: Callable # 处理函数
module_name: str # 所属模块名称
description: Optional[str] = None # 描述
requires_auth: bool = True # 是否需要认证
header_module: Optional[str] = None # 使用的请求头模块名称
class ModuleRegistry:
"""模块注册表"""
def __init__(self):
self._actions: Dict[str, ActionConfig] = {}
self._modules: Dict[str, Dict[str, Any]] = {}
def register_action(
self,
action_name: str,
handler: Callable,
module_name: str = "default",
description: Optional[str] = None,
requires_auth: bool = True,
header_module: Optional[str] = None
):
"""
注册操作
Args:
action_name: 操作名称
handler: 处理函数
module_name: 模块名称
description: 描述
requires_auth: 是否需要认证
header_module: 请求头模块名称
"""
config = ActionConfig(
handler=handler,
module_name=module_name,
description=description,
requires_auth=requires_auth,
header_module=header_module
)
self._actions[action_name] = config
def get_action(self, action_name: str) -> Optional[ActionConfig]:
"""
获取操作配置
Args:
action_name: 操作名称
Returns:
操作配置,如果不存在返回None
"""
return self._actions.get(action_name)
def get_all_actions(self) -> Dict[str, ActionConfig]:
"""获取所有注册的操作"""
return self._actions.copy()
def register_module(self, module_name: str, module_instance: Any, **metadata):
"""
注册模块实例
Args:
module_name: 模块名称
module_instance: 模块实例
**metadata: 模块元数据
"""
self._modules[module_name] = {
'instance': module_instance,
**metadata
}
def get_module(self, module_name: str) -> Optional[Any]:
"""
获取模块实例
Args:
module_name: 模块名称
Returns:
模块实例,如果不存在返回None
"""
module_info = self._modules.get(module_name)
return module_info['instance'] if module_info else None
def get_actions_by_module(self, module_name: str) -> Dict[str, ActionConfig]:
"""
获取指定模块的所有操作
Args:
module_name: 模块名称
Returns:
操作字典
"""
return {
name: config
for name, config in self._actions.items()
if config.module_name == module_name
}
# 全局模块注册表实例
registry = ModuleRegistry()