优化api适配器
This commit is contained in:
parent
ae56a73c5e
commit
20aedb7347
@ -83,47 +83,6 @@ class ApiAdapter:
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def upload_file_to_jingrow(self, file_data: bytes, filename: str,
|
||||
attached_to_pagetype: Optional[str] = None,
|
||||
attached_to_name: Optional[str] = None,
|
||||
attached_to_field: Optional[str] = None) -> Dict[str, Any]:
|
||||
try:
|
||||
api_url = f"{self.api_url}/api/action/upload_file"
|
||||
headers = self._get_headers().copy()
|
||||
# 让 requests 自动设置 multipart/form-data
|
||||
if 'Content-Type' in headers:
|
||||
del headers['Content-Type']
|
||||
|
||||
files = {
|
||||
'file': (filename, file_data, 'application/octet-stream')
|
||||
}
|
||||
data: Dict[str, Any] = {
|
||||
'file_name': filename,
|
||||
'is_private': 0
|
||||
}
|
||||
if attached_to_pagetype and attached_to_name:
|
||||
data['pagetype'] = attached_to_pagetype
|
||||
data['docname'] = attached_to_name
|
||||
if attached_to_field:
|
||||
data['fieldname'] = attached_to_field
|
||||
|
||||
response = requests.post(api_url, files=files, data=data, headers=headers, timeout=30)
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
if result.get('session_expired'):
|
||||
return {'success': False, 'error': 'Session已过期,请重新登录'}
|
||||
if result.get('message'):
|
||||
return {
|
||||
'success': True,
|
||||
'file_url': result['message'].get('file_url'),
|
||||
'file_name': result['message'].get('file_name')
|
||||
}
|
||||
return {'success': False, 'error': 'API响应格式错误'}
|
||||
else:
|
||||
error_text = response.text
|
||||
return {'success': False, 'error': f'API请求失败 (HTTP {response.status_code}): {error_text}'}
|
||||
except Exception as e:
|
||||
return {'success': False, 'error': f'调用upload_file API异常: {str(e)}'}
|
||||
|
||||
def get_field_mapping_from_jingrow(self, pagetype: str) -> Dict[str, str]:
|
||||
try:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user