Files
abot/gewechat/response/gewe_resp.py
2025-04-23 16:11:06 +08:00

57 lines
1.4 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.
import typing
class GeweResponse:
"""
通用的 Gewechat 响应处理类
"""
def __init__(self, resp: dict):
"""
:param resp: gewechat接口返回的原始字典
"""
self.raw = resp
self.ret = resp.get("ret")
self.msg = resp.get("msg")
self.data = resp.get("data")
def is_success(self) -> bool:
"""
判断请求是否成功
:return: True/False
"""
return self.ret == 200
def get_msg(self) -> str:
"""
获取返回消息
:return: 消息字符串
"""
return self.msg or ""
def get_data(self) -> typing.Any:
"""
获取返回的数据对象类型可能为dict、list、str等
:return: data字段内容
"""
return self.data
def get_data_as_list(self) -> list:
"""
获取data字段为list类型若不是则返回空列表
"""
if isinstance(self.data, list):
return self.data
return []
def get_data_as_dict(self) -> dict:
"""
获取data字段为dict类型若不是则返回空字典
"""
if isinstance(self.data, dict):
return self.data
return {}
def __repr__(self):
return f"<GeweResponse ret={self.ret} msg={self.msg} data_type={type(self.data).__name__}>"