简道云fastapi
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user