如何在Python中使用zeep设置默认xmlns? [英] How to set default xmlns with zeep in Python?

查看:149
本文介绍了如何在Python中使用zeep设置默认xmlns?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用zeep进行一些API调用. 预期的输出是:

I want to use zeep to make some API calls. The expected output is:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="https://cabapi-prod.service.dbrent.net/hal2_cabserver/">
  <soap:Body>
    <CABSERVER.listFreeBikes>
      <CommonParams>
        <UserData>
          <User>XXXXXXXXXXXXXX</User>
          <Password>XXXXXXXXXXXXXX</Password>
        </UserData>
        <LanguageUID>1</LanguageUID>
        <RequestTime>XXXXXXXXXXXXXX</RequestTime>
        <Version>2</Version>
      </CommonParams>
      <SearchPosition>
        <Longitude>XXXXXXXXXXXXXX</Longitude>
        <Latitude>XXXXXXXXXXXXXX</Latitude>
      </SearchPosition>
      <maxResults>100</maxResults>
      <searchRadius>844</searchRadius>
      <CustomerData>
        <Phone>XXXXXXXXXXXXXX</Phone>
        <Password>XXXXXXXXXXXXXX</Password>
        <PhoneUserEdited>false</PhoneUserEdited>
      </CustomerData>
    </CABSERVER.listFreeBikes>
  </soap:Body>
</soap:Envelope>

但是,我只能得到:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Body>
    <ns0:CABSERVER.listFreeBikes xmlns:ns0="https://xml.dbcarsharing-buchung.de/hal2_cabserver/">
      <CommonParams>
        <UserData>
          <User>XXXXXXXXXXXXXX</User>
          <Password>XXXXXXXXXXXXXX</Password>
        </UserData>
        <LanguageUID>1</LanguageUID>
        <RequestTime>XXXXXXXXXXXXXX</RequestTime>
        <Version>2</Version>
      </CommonParams>
      <CustomerData>
        <Phone>XXXXXXXXXXXXXX</Phone>
        <Password>XXXXXXXXXXXXXX</Password>
        <PhoneUserEdited>false</PhoneUserEdited>
      </CustomerData>
      <SearchPosition>
        <Longitude>XXXXXXXXXXXXXX</Longitude>
        <Latitude>XXXXXXXXXXXXXX</Latitude>
      </SearchPosition>
      <maxResults>100</maxResults>
      <searchRadius>844</searchRadius>
    </ns0:CABSERVER.listFreeBikes>
  </soap-env:Body>
</soap-env:Envelope>

服务器无法正确识别我的请求,我认为这是由于名称空间所致.我做了一些研究,但没有找到将默认名称空间(xmlns)设置为 https://的方法xml.dbcarsharing-buchung.de/hal2_cabserver/.而是将其添加为额外的命名空间.

The server is not able to recognize my request correctly and I think it is because of the namespace. I did some research but did not find a way to set the default namespace (xmlns) to https://xml.dbcarsharing-buchung.de/hal2_cabserver/. Instead, it gets added as extra namespace.

这是我当前的代码:

with client.settings(raw_response=True):
    ## Common Params related
    ### UserData related
    userNameType = client.get_type('ns0:Type_UserName')
    userPasswordType = client.get_type('ns0:Type_UserPassword')
    userName = userNameType('XXXXXXXXXXXXXX')
    userPassword = userPasswordType('XXXXXXXXXXXXXX')
    userDataType = client.get_type('ns0:Type_UserData')
    userData = userDataType(User = userName, Password = userPassword)


    current_time = time.localtime()
    current_time_str = time.strftime('%Y-%m-%dT%H:%M:%S.GMT+02:00', current_time)

    commonParamsType = client.get_type('ns0:Type_CommonParams')
    commonParams = commonParamsType(UserData = userData, LanguageUID = 1, RequestTime = current_time_str, Version = 2)

    ## CustomerData related
    customerPhoneType = client.get_type('ns0:Type_CustomerPhone')
    customerPhone = customerPhoneType('XXXXXXXXXXXXXX') # Get from config
    customerPasswordType = client.get_type('ns0:Type_CustomerPassword')
    customerPassword = customerPasswordType('XXXXXXXXXXXXXX') #Get from config
    customerDataType = client.get_type('ns0:Type_CustomerData')
    customerData = customerDataType(Phone = customerPhone, Password = customerPassword, PhoneUserEdited = False)

    ## SearchPosition related
    geoPositionType = client.get_type('ns0:Type_GeoPosition')
    geoPosition = geoPositionType(Longitude = XXXXXXXXXXXXXX, Latitude = XXXXXXXXXXXXXX)

    # DEBUG
    node = client.create_message(client.service, 'CABSERVER.listFreeBikes', CommonParams = commonParams, CustomerData = customerData, SearchPosition = geoPosition, maxResults = 100, searchRadius = 844)
    from lxml import etree as ET
    tree = ET.ElementTree(node)
    tree.write('test.xml', pretty_print=True)

到目前为止我尝试过的事情:

What I tried so far:

client.set_ns_prefix('ns0', '')

但是那不能解决我的问题.我的想法不多了.谁能指出我正确的方向?

However that does not solve my issue. I am running out of ideas. Anyone who can point me into the right direction?

可以在以下位置找到WSDL: https://xml.dbcarsharing- buchung.de/hal2_cabserver/definitions/HAL2_CABSERVER_3.wsdl

The WSDL can be found here: https://xml.dbcarsharing-buchung.de/hal2_cabserver/definitions/HAL2_CABSERVER_3.wsdl

推荐答案

您已经关闭

client.set_ns_prefix('ns0', '')

应阅读

client.set_ns_prefix(None, "https://xml.dbcarsharing-buchung.de/hal2_cabserver/")

然后应使用默认名称空间.

Then the default namespace should be used.

这篇关于如何在Python中使用zeep设置默认xmlns?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆