AutoRpa/rpa_oms/app/inbound/envelope.py

27 lines
900 B
Python
Raw Permalink 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.

# 批量信封。缺 data、空数组、超过 100 条:整包 422,一条都不落。
#
# 单条字段错误不走这里,由各收件函数逐条 REJECTED,HTTP 仍 200。
MAX_BATCH = 100
# 信封不合法,对应 HTTP 422。
class EnvelopeError(Exception):
# 记下对外文案。
def __init__(self, message: str):
self.message = message
# 拆出 data 数组。不合法时抛 EnvelopeError,调用方整包拒绝。
def parse_envelope(payload: object) -> list:
if not isinstance(payload, dict) or "data" not in payload:
raise EnvelopeError("缺少批量数据节点 data")
data = payload["data"]
if not isinstance(data, list):
raise EnvelopeError("data 必须是数组")
if len(data) == 0:
raise EnvelopeError("data 不能为空")
if len(data) > MAX_BATCH:
raise EnvelopeError("一批最多 100 条")
return data