style: 统一 Ruff 代码格式

This commit is contained in:
yml2213
2026-08-30 21:04:52 +08:00
parent c891ac982e
commit 47e19ed7b2
90 changed files with 5574 additions and 2350 deletions
+53 -28
View File
@@ -44,7 +44,8 @@ class FishFinRechargeConfig:
app_key=os.getenv("FISH_FIN_RECHARGE_APP_KEY", "").strip(),
notify_url=os.getenv("FISH_FIN_RECHARGE_NOTIFY_URL", "").strip(),
timeout=(8, timeout),
debug=os.getenv("FISH_FIN_RECHARGE_DEBUG", "false").strip().lower() in {"1", "true", "yes"},
debug=os.getenv("FISH_FIN_RECHARGE_DEBUG", "false").strip().lower()
in {"1", "true", "yes"},
)
def validate(self) -> None:
@@ -86,7 +87,9 @@ class FishFinRechargeClient:
def _sign_value(value: Any) -> str:
"""将参数转为待签名文本;对象按稳定紧凑 JSON 表示。"""
if isinstance(value, (dict, list, tuple)):
return json.dumps(value, ensure_ascii=False, separators=(",", ":"), sort_keys=True)
return json.dumps(
value, ensure_ascii=False, separators=(",", ":"), sort_keys=True
)
if isinstance(value, bool):
return "true" if value else "false"
return str(value)
@@ -143,7 +146,9 @@ class FishFinRechargeClient:
received = str(payload.get("sign") or "").strip().lower()
return bool(received) and received == self.sign(payload, method)
def _request(self, method: str, path: str, params: Mapping[str, Any] | None = None) -> dict[str, Any]:
def _request(
self, method: str, path: str, params: Mapping[str, Any] | None = None
) -> dict[str, Any]:
"""补齐公共参数、签名并执行一次 JSON 请求。"""
request_params: dict[str, Any] = {
"app_id": self.config.app_id,
@@ -167,27 +172,37 @@ class FishFinRechargeClient:
if key != "sign"
},
# 只记录摘要,便于关联排查而不暴露可重放的签名。
"sign_digest": hashlib.sha256(request_params["sign"].encode("utf-8")).hexdigest()[:12],
"sign_digest": hashlib.sha256(
request_params["sign"].encode("utf-8")
).hexdigest()[:12],
}
if self.config.debug:
sign_params = self.normalized_params(request_params)
sign_query = "&".join(f"{key}={sign_params[key]}" for key in sorted(sign_params))
trace_event.update({
"url": url,
"content_type": "application/json",
"json_body": self._debug_payload(request_params),
"sign_params": sign_params,
# 不把可重放签名原文或 AppSecret 写入日志,只记录待签名串摘要。
"sign_source_digest": hashlib.sha256(
f"{sign_query}{method.upper()}".encode("utf-8")
).hexdigest()[:12],
})
sign_query = "&".join(
f"{key}={sign_params[key]}" for key in sorted(sign_params)
)
trace_event.update(
{
"url": url,
"content_type": "application/json",
"json_body": self._debug_payload(request_params),
"sign_params": sign_params,
# 不把可重放签名原文或 AppSecret 写入日志,只记录待签名串摘要。
"sign_source_digest": hashlib.sha256(
f"{sign_query}{method.upper()}".encode("utf-8")
).hexdigest()[:12],
}
)
self.trace(trace_event)
try:
if method.upper() == "GET":
response = self.session.get(url, params=request_params, timeout=self.config.timeout)
response = self.session.get(
url, params=request_params, timeout=self.config.timeout
)
else:
response = self.session.post(url, json=request_params, timeout=self.config.timeout)
response = self.session.post(
url, json=request_params, timeout=self.config.timeout
)
response.raise_for_status()
payload = response.json()
except requests.RequestException as exc:
@@ -205,18 +220,22 @@ class FishFinRechargeClient:
"message": payload.get("msg") or payload.get("message") or "",
"out_order_id": self._response_value(payload, "out_order_id", "outOrderId"),
"order_id": self._response_value(payload, "order_id", "orderId"),
"order_status": self._response_value(payload, "order_status", "orderStatus"),
"order_status": self._response_value(
payload, "order_status", "orderStatus"
),
"fail_reason": self._response_value(payload, "fail_reason", "failReason"),
}
if self.config.debug:
trace_event.update({
"response_headers": {
key: value
for key, value in response.headers.items()
if key.lower() in {"content-type", "x-request-id", "request-id"}
},
"response_body": self._debug_payload(payload),
})
trace_event.update(
{
"response_headers": {
key: value
for key, value in response.headers.items()
if key.lower() in {"content-type", "x-request-id", "request-id"}
},
"response_body": self._debug_payload(payload),
}
)
self.trace(trace_event)
return payload
@@ -263,7 +282,11 @@ class FishFinRechargeClient:
raise ValueError("product_id 不能为空")
if not isinstance(buy_num, int) or isinstance(buy_num, bool) or buy_num < 1:
raise ValueError("buy_num 必须是不小于 1 的整数")
if not isinstance(order_type, int) or isinstance(order_type, bool) or order_type not in {0, 1, 2, 3}:
if (
not isinstance(order_type, int)
or isinstance(order_type, bool)
or order_type not in {0, 1, 2, 3}
):
raise ValueError("order_type 必须是 0 至 3 的整数")
if not isinstance(recharge_arg, list) or not recharge_arg:
raise ValueError("recharge_arg 必须是非空数组")
@@ -287,7 +310,9 @@ class FishFinRechargeClient:
out_order_id = str(out_order_id).strip()
if not out_order_id:
raise ValueError("out_order_id 不能为空")
return self._request("GET", self.QUERY_ORDER_PATH, {"out_order_id": out_order_id})
return self._request(
"GET", self.QUERY_ORDER_PATH, {"out_order_id": out_order_id}
)
def account_info(self) -> dict[str, Any]:
"""查询商户账户信息。"""