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

183 Views Asked by At

I have data that I want to send as a SOAP request through WSDL. Up until now, I could do it with the structure below, however now a tag has become required and I want to send the package without that one. I have read the documentation and it says that the best option is zeep.xsd.SkipValue for any tag I want to omit from validation, as zeep validates everything. The problem is that I use zeep.xsd.SkipValue and the xml produced is filling SkipValue as the tag's value, for example 'projectAa'='SkipValue'

My code is

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)

When I execute response = client.service.addTechSheetEvaluation(**applicationData) the xml produced is produced with projectAa equal to the string SkipValue instead of skipping validation of the specific tag.

...
<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"/>
...
1

There are 1 best solutions below

0
hehe On

You need to use None instead of 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)

However, if that does not work you can also try:

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)