login.py 1.5 KB

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