Files
gym-manage/test-suite/api/file_api.py
T

58 lines
1.8 KiB
Python

"""
文件管理 API 客户端
"""
from httpx import AsyncClient
import io
class FileAPI:
"""文件管理 API 客户端"""
def __init__(self, client: AsyncClient):
self.client = client
async def get_file_list(self):
"""获取文件列表"""
return await self.client.get('/api/files')
async def get_file_by_id(self, file_id):
"""根据ID获取文件"""
return await self.client.get(f'/api/files/{file_id}')
async def upload(self, file_path, upload_user):
"""上传文件"""
with open(file_path, 'rb') as f:
return await self.client.post('/api/files/upload', json={'file': file_path, 'uploadUser': upload_user})
async def upload_file(self, file_content, filename, upload_user="test_user"):
"""上传文件(内存方式)"""
files = {'file': (filename, file_content, 'text/plain')}
headers = {'X-Username': upload_user}
return await self.client.post('/api/files/upload', files=files, headers=headers)
async def download(self, file_id):
"""下载文件"""
return await self.client.get(f'/api/files/{file_id}/download')
async def delete(self, file_id):
"""删除文件"""
return await self.client.delete(f'/api/files/{file_id}')
async def get_file_info(self, file_id):
"""获取文件信息(别名)"""
return await self.get_file_by_id(file_id)
async def download_file(self, file_id):
"""下载文件(别名)"""
return await self.download(file_id)
async def delete_file(self, file_id):
"""删除文件(别名)"""
return await self.delete(file_id)
class SysFileAPI(FileAPI):
"""系统文件 API (别名)"""
pass