login.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import requests
  2. from typing import Dict, Any
  3. from common.setting import Config
  4. class LoginApi:
  5. def __init__(self):
  6. self.token = None
  7. def login_bmc(self, username: str, password: str, client: str = "RIIL") -> Dict[str, Any]:
  8. """
  9. 登录BMC接口方法。
  10. :param base_url: API的基地址,例如"http://example.com"
  11. :param username: 用户名
  12. :param password: 密码
  13. :param client: 调用接口的客户端,默认为"RIIL"
  14. :return: 包含响应数据的字典
  15. :raises: 请求错误或响应异常
  16. """
  17. url = f"{Config.BASE_URL}/login"
  18. # 构造请求参数
  19. params = {
  20. "username": username,
  21. "password": password,
  22. "_openCLIENT": client
  23. }
  24. try:
  25. # 发送GET请求
  26. response = requests.get(url, params=params)
  27. # 处理响应
  28. if response.status_code == 200:
  29. return {
  30. "success": True,
  31. "data": response.json(),
  32. "cookies": response.cookies.get_dict()
  33. }
  34. return {
  35. "success": False,
  36. "data": response.json(),
  37. "cookies": None
  38. }
  39. except requests.RequestException as e:
  40. raise RuntimeError(f"请求失败: {str(e)}")