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

51 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
用户管理 API 客户端
"""
from httpx import AsyncClient
class UserAPI:
"""用户管理 API 客户端"""
def __init__(self, client: AsyncClient):
self.client = client
async def get_user_list(self):
"""获取用户列表"""
return await self.client.get('/api/users')
async def get_users_by_page(self, page: int = 0, size: int = 10, **kwargs):
"""分页获取用户列表,支持搜索和排序"""
params = {'page': page, 'size': size}
params.update(kwargs)
return await self.client.get('/api/users/page', params=params)
async def create_user(self, user_data):
"""创建用户"""
return await self.client.post('/api/users', json=user_data)
async def get_user_by_id(self, user_id):
"""根据ID获取用户"""
return await self.client.get(f'/api/users/{user_id}')
async def update_user(self, user_id, user_data):
"""更新用户"""
return await self.client.put(f'/api/users/{user_id}', json=user_data)
async def delete_user(self, user_id):
"""删除用户"""
return await self.client.delete(f'/api/users/{user_id}')
async def get_user_profile(self):
"""获取当前用户资料(调用get_user_by_id,使用token中的userId"""
return await self.client.get('/api/users/profile')
async def update_user_profile(self, profile_data):
"""更新当前用户资料(调用update_user,使用token中的userId"""
return await self.client.put('/api/users/profile', json=profile_data)
async def assign_roles(self, user_id, role_ids):
"""为用户分配角色"""
return await self.client.post(f'/api/users/{user_id}/roles', json=role_ids)