func.py 744 B

12345678910111213141516171819202122232425262728
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from uuid import uuid4
  4. from httpx import Response
  5. from typing import Dict
  6. from digitalHuman.utils import logger
  7. __all__ = ['generateId', 'checkResponse']
  8. def generateId() -> str:
  9. return str(uuid4())
  10. def checkResponse(response: Response, module: str, note: str = "") -> Dict:
  11. """
  12. 校验请求响应是否正常
  13. 不正常直接抛错
  14. """
  15. if response.status_code == 200:
  16. return response.json()
  17. logger.error(f"[{module}] {note}, status code: {response.status_code}, data: {response.text}", exc_info=True)
  18. # 优先提取message错误信息
  19. try:
  20. message = response.json()['message']
  21. except:
  22. message = response.text
  23. raise RuntimeError(message)