在Zeep中向元素添加可选属性

时间:2018-06-13 18:48:07

标签: xml soap zeep workday-api

我正在尝试使用Zeep调用Workday SOAP服务。

我能够成功调用服务,添加元素来过滤请求,并获得响应,但我需要在request元素中指定一个版本属性,我无法弄清楚如何添加它:

service = client.create_service(
        '{urn:com.workday/bsvc/'+Human_Resources+'}'+Human_Resources+'Binding',
        'http://my-endpoint.com')

service_operation = 'Get_Job_Profiles'

request_filter = {'Response_Filter' : {
          'Page': 1,
          'Count' : 100, }}
#other things are added to the request_filter dict as well

response = getattr(service,service_operation)(**request_filter)

这很好用。 Zeep产生:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Header>
        <ns0:Workday_Common_Header xmlns:ns0="urn:com.workday/bsvc">
            <ns0:Include_Reference_Descriptors_In_Response>true</ns0:Include_Reference_Descriptors_In_Response>
        </ns0:Workday_Common_Header>
# some other security related headers here #
    </soap-env:Header>
    <soap-env:Body xmlns:ns0="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" ns0:Id="id-3b0aa2ca-dc0f-417c-9168-498275645b16">
        <ns0:Get_Job_Profiles_Request xmlns:ns0="urn:com.workday/bsvc">
            <ns0:Request_Criteria>
                <ns0:Transaction_Log_Criteria_Data>
                    <ns0:Transaction_Date_Range_Data>
                        <ns0:Updated_From>2000-01-01T00:00:00</ns0:Updated_From>
                        <ns0:Updated_Through>2018-05-09T00:00:00</ns0:Updated_Through>
                    </ns0:Transaction_Date_Range_Data>
                </ns0:Transaction_Log_Criteria_Data>
            </ns0:Request_Criteria>
            <ns0:Response_Filter>
                <ns0:As_Of_Entry_DateTime>2018-05-09T00:00:00</ns0:As_Of_Entry_DateTime>
                <ns0:Page>2</ns0:Page>
                <ns0:Count>100</ns0:Count>
            </ns0:Response_Filter>
            <ns0:Response_Group>
                <ns0:Include_Reference>true</ns0:Include_Reference>
                <ns0:Include_Job_Profile_Basic_Data>true</ns0:Include_Job_Profile_Basic_Data>
                <ns0:Include_Job_Classification_Data>true</ns0:Include_Job_Classification_Data>
                <ns0:Include_Workers_Compensation_Data>true</ns0:Include_Workers_Compensation_Data>
                <ns0:Include_Job_Profile_Compensation_Data>true</ns0:Include_Job_Profile_Compensation_Data>
            </ns0:Response_Group>
        </ns0:Get_Job_Profiles_Request>
    </soap-env:Body>
</soap-env:Envelope>

但现在我需要添加一个属性:

<ns0:Get_Job_Profiles_Request xmlns:ns0="urn:com.workday/bsvc">

它需要看起来像这样:

<ns0:Get_Job_Profiles_Request xmlns:ns0="urn:com.workday/bsvc" ns0:version="v29">

以下是xsd的定义:

<xsd:complexType name="Get_Job_Profiles_RequestType">
<xsd:annotation>
    <xsd:documentation>Request element used to find and get Job Profiles and their associated data.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
    <xsd:element name="Request_References" type="wd:Job_Profile_Request_ReferencesType" minOccurs="0"/>
    <xsd:element name="Request_Criteria" type="wd:Job_Profile_Request_CriteriaType" minOccurs="0"/>
    <xsd:element name="Response_Filter" type="wd:Response_FilterType" minOccurs="0"/>
    <xsd:element name="Response_Group" type="wd:Job_Profile_Response_GroupType" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute ref="wd:version"/></xsd:complexType>

添加元素和值很容易,但我对如何向元素添加属性感到困惑。

1 个答案:

答案 0 :(得分:0)

Zeep非常聪明,我需要做的就是将其添加为kwarg:

在:

response = getattr(service,service_operation)(**request_filter)

后:

response = getattr(service,service_operation)(**request_filter, version='v29.1')

因此产生:

<ns0:Get_Job_Profiles_Request xmlns:ns0="urn:com.workday/bsvc" ns0:version="v29.1">
相关问题