Python zeep - 发送带有 zeep.xsd.SkipValue 的包

Python zeep - send a package with zeep.xsd.SkipValue

提问人:lios 提问时间:10/3/2023 最后编辑:Dmitrylios 更新时间:10/16/2023 访问量:132

问:

我有数据要通过 WSDL 作为 SOAP 请求发送。到目前为止,我可以使用下面的结构来做到这一点,但是现在需要一个标签,我想在没有那个标签的情况下发送包裹。我已经阅读了文档,它说对于我想从验证中省略的任何标签,最好的选择是 zeep.xsd.SkipValue,因为 zeep 会验证所有内容。问题是我使用 zeep.xsd.SkipValue,并且生成的 xml 将 SkipValue 填充为标签的值,例如“projectAa”=“SkipValue”

我的代码是

applicationData = {
            'techSheet': {
                'code': "test",
                'projectAa': zeep.xsd.SkipValue,
                'email': "test",
                'TechList': {
                    'Tech': {
                        'publicValue': "0",
                        'privateValue': "0",
                        'requestedValue': "0",
                        'TechBudgetList': {
                            'TechBudget': expenses
                        },
                    }
                }
            },
            'techSheetEvaluation': {
                'projectAa':zeep.xsd.SkipValue,
                'decisionNumber': '-',
                'evaluationStatus': '1',  
            }
        }

print(zeep.xsd.SkipValue)
response = client.service.addTechnicalSheetAndEvaluation(**applicationData)
print("OPSAA CODE IS ", response)

当我执行时,生成的 xml 是用 projectAa 等于字符串 SkipValue 生成的,而不是跳过特定标记的验证。response = client.service.addTechSheetEvaluation(**applicationData)

...
<techSheet email="" contactEmail="" code="0000356060" projectAa="SkipValue">
    <TechList>
        <Tech requestedValue="0" privateValue="0" publicValue="0">
            <TechBudgetList>
                <TechBudget xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" aa="1" investmentDescription="Δαπάνες Πρόωρης Συνταξιοδότησης" requestedValue="2470.84" privateValue="0" publicValue="2470.84" actionCode="97.1_21A" expenseSubcategoryCode="M113" xsi:type="ns0:techBudgetAnalysisBase"/>
            </TechBudgetList>
        </Tech>
    </TechList>
</techSheet>
<techSheetEvaluation evaluationStatus="1" decisionNumber="-" projectAa="SkipValue"/>
...
python-3.x xml soap zeep

评论

0赞 Hermann12 10/4/2023
应用程序数据是 json 而不是 xml。请更正您的标记,您将获得更多帮助。
0赞 lios 10/10/2023
我已经编辑了这个问题,以便显示生成的xml。

答:

0赞 hehe 10/16/2023 #1

您需要使用 None 而不是 zeep.xsd.SkipValue

#import everything
from zeep import Client, Settings, xsd
applicationData = {
            'techSheet': {
                'code': "test",
                'projectAa': None,
                'email': "test",
                'TechList': {
                    'Tech': {
                        'publicValue': "0",
                        'privateValue': "0",
                        'requestedValue': "0",
                        'TechBudgetList': {
                            'TechBudget': expenses
                        },
                    }
                }
            },
            'techSheetEvaluation': {
                'projectAa':None,
                'decisionNumber': '-',
                'evaluationStatus': '1',  
            }
        }

print(zeep.xsd.SkipValue)
response = client.service.addTechnicalSheetAndEvaluation(**applicationData)
print("OPSAA CODE IS ", response)

但是,如果这不起作用,您也可以尝试:

class CustomSerializer(xsd.Element):
    def _render(self, parent, value, xsd_type, render_path):
        # If the value is None, simply omit the element
        if value is None:
            return

        super(CustomSerializer, self)._render(parent, value, xsd_type, render_path)

# Define a custom element with the desired behavior
custom_projectAa_element = CustomSerializer(
    name='projectAa',
    type=xsd.String(),
    default=zeep.xsd.SkipValue
)

# Create a new data structure with the custom serializer
custom_data = {
    'techSheet': {
        'code': "test",
        'projectAa': zeep.xsd.SkipValue,  # You can still use zeep.xsd.SkipValue
        'email': "test",
        'TechList': {
            'Tech': {
                'publicValue': "0",
                'privateValue': "0",
                'requestedValue': "0",
            }
        }
    },
    'techSheetEvaluation': {
        'projectAa': zeep.xsd.SkipValue,  # You can still use zeep.xsd.SkipValue
        'decisionNumber': '-',
        'evaluationStatus': '1',
    }
}

# Create a Zeep client with the custom serializer
custom_client = Client('your_wsdl_url', settings=Settings(extra_elements=[custom_projectAa_element]))

# Send the request using the custom data and client
response = custom_client.service.addTechnicalSheetAndEvaluation(**custom_data)
print("OPSAA CODE IS", response)