【问题标题】:Python Zeep: How to determine whether message has an attachment?Python Zeep:如何判断邮件是否有附件?
【发布时间】:2021-12-22 10:53:04
【问题描述】:

我必须查询第三方 SOAP 端点。这个端点有时有一个附件,有时它只有纯 XML。在收到回复并进行检查之前,我无法知道会发生什么。

之前的开发者做了这样的事情:

result = client.service.download(
    # parameters
)

# TODO remove unresolved reference when I understand zeep library better
# noinspection PyUnresolvedReferences
message_type = zeep.wsdl.attachments.MessagePack
if isinstance(result, message_type):
    content = str(result.attachments[0].content, encoding='utf-8')
else:
    content = serialize_object(result)["fileContents"]

我想要一种方法来确定我得到的响应类型,而不必抑制来自我的 IDE 的“未解决的引用”投诉。避免使用异常进行流控制也很好,即在调用result.root 时处理AttributeError

Zeep 是否有一些规范的方法来确定响应是否有附件?我很难在文档中找到类似的东西。

【问题讨论】:

    标签: python zeep


    【解决方案1】:

    我发现如果我尝试序列化结果,我可以检查我是否有字典。

    result = client.service.download(
        # parameters
    )
    _result = serialize_object(result)
    
    if not isinstance(_result, dict):
        _result = serialize_object(result.root)
        _result["fileContents"] = str(result.attachments[0].content, encoding='utf-8')
    
    # use _result going forward
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2014-07-21
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      相关资源
      最近更新 更多