mvantellingen / python-zeep

A Python SOAP client
http://docs.python-zeep.org
Other
1.89k stars 586 forks source link

Server response is "The user has logged in correctly ", but function returns an error #177

Closed FrugoFruit90 closed 8 years ago

FrugoFruit90 commented 8 years ago

Hey! The information in the response (loginReturn ) means (in Polish): "The user has logged in correctly ". Nevertheless, I get the following error:

C:\Users\j.zysko\Envs\gabinet\lib\site-packages\requests\packages\urllib3\connectionpool.py:838: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/security.html
  InsecureRequestWarning)
zeep.transports: HTTP Response from https://test-dilo.nfz.gov.pl/ws-broker-server-pkus-auth/services/Auth (status: 200):
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><ns1:session xmlns:ns1="http://xml.kamsoft.pl/ws/common" id="24EB4A497C1527A93330482E4EF55A75" /><ns1:authToken xmlns:ns1="http://xml.kamsoft.pl/ws/common" id="BSqPf7Mc8nq_JIlJdcXMWQ" /></soapenv:Header><soapenv:Body><ns1:loginReturn xmlns:ns1="http://xml.kamsoft.pl/ws/kaas/login_types">[000] U&amp;#380;ytkownik zosta&amp;#322; prawid&amp;#322;owo zalogowany.</ns1:loginReturn></soapenv:Body></soapenv:Envelope>
Traceback (most recent call last):
  File "C:/Users/j.zysko/gabinet/dilo/utils.py", line 63, in <module>
    dilo_login(client)
  File "C:/Users/j.zysko/gabinet/dilo/utils.py", line 36, in dilo_login
    client.service.login(credentials=credentials, password=password)
  File "C:\Users\j.zysko\Envs\gabinet\lib\site-packages\zeep\client.py", line 24, in __call__
    self._op_name, args, kwargs)
  File "C:\Users\j.zysko\Envs\gabinet\lib\site-packages\zeep\wsdl\soap.py", line 92, in send
    return self.process_reply(client, operation_obj, response)
  File "C:\Users\j.zysko\Envs\gabinet\lib\site-packages\zeep\wsdl\soap.py", line 123, in process_reply
    return operation.process_reply(doc)
  File "C:\Users\j.zysko\Envs\gabinet\lib\site-packages\zeep\wsdl\soap.py", line 256, in process_reply
    return self.output.deserialize(body)
  File "C:\Users\j.zysko\Envs\gabinet\lib\site-packages\zeep\wsdl\messages.py", line 240, in deserialize
    assert elm is not None, '%s not found' % part.element.qname
AssertionError: {http://xml.kamsoft.pl/ws/common}session not found

Below is the server response - the one that is being parsed incorrectly (same as above but from SoapUI which formats it nicely):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <ns1:session id="37A6660D3B6ECAB97C1D4E9E757061B3" xmlns:ns1="http://xml.kamsoft.pl/ws/common"/>
      <ns1:authToken id="BSPApF6asPCpmmBUOuj_13" xmlns:ns1="http://xml.kamsoft.pl/ws/common"/>
   </soapenv:Header>
   <soapenv:Body>
      <ns1:loginReturn xmlns:ns1="http://xml.kamsoft.pl/ws/kaas/login_types">[000] U&amp;#380;ytkownik zosta&amp;#322; prawid&amp;#322;owo zalogowany.</ns1:loginReturn>
   </soapenv:Body>
</soapenv:Envelope>

The comment in the DocumentMessage(SoapMessage) function says: In the document message there are no additional wrappers, and the message parts appear directly under the SOAP Body element. So I guess the problem is that there is a wrapper inside the wrapper. Any idea how to fix it?

FrugoFruit90 commented 8 years ago

Any way I could help with addressing this? (mostly I can try to provide more information/ test things as I'm probably not qualified enough to create my own fix) :) Similar code works with suds-jurko, but unfortunately his project does not support empty elements, which gives me a lot of problems as this particular SOAP uses them a lot.

mvantellingen commented 8 years ago

Do you have the wsdl somewhere (I might be overlooking it).

So what I usually do first when an error occurs during the response handling is creating a script for it, like for example::

import pretend

from zeep import Client
from zeep.transports import Transport

transport = Transport()
client = Client('http://www.visahq.com/wsdl.wsdl', transport=transport)

response = pretend.stub(
    status_code=200,
    headers=[],
    content="""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:VisaHQTravelService" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:doGetCountriesResponse>
      <countries SOAP-ENC:arrayType="ns1:CountryItemType[232]" xsi:type="ns1:CountriesArrayType">
        <item xsi:type="ns1:CountryItemType">
          <code xsi:type="ns1:CountryCodeType">AF</code>
          <name xsi:type="xsd:string">Afghanistan</name>
        </item>
        <item xsi:type="ns1:CountryItemType">
          <code xsi:type="ns1:CountryCodeType">AL</code>
          <name xsi:type="xsd:string">Albania</name>
        </item>
        <item xsi:type="ns1:CountryItemType">
          <code xsi:type="ns1:CountryCodeType">DZ</code>
          <name xsi:type="xsd:string">Algeria</name>
        </item>
        <item xsi:type="ns1:CountryItemType">
          <code xsi:type="ns1:CountryCodeType">AS</code>
          <name xsi:type="xsd:string">American Samoa</name>
        </item>
        <item xsi:type="ns1:CountryItemType">
          <code xsi:type="ns1:CountryCodeType">AD</code>
          <name xsi:type="xsd:string">Andorra</name>
        </item>
        <item xsi:type="ns1:CountryItemType">
          <code xsi:type="ns1:CountryCodeType">AO</code>
          <name xsi:type="xsd:string">Angola</name>
        </item>
        <!-- ...and many more... -->
      </countries>
    </ns1:doGetCountriesResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
""")

result = client.service._binding.process_reply(
    client,
    client.service._binding._operations['doGetCountries'],
    response)

print(result)

If you could create something like that for your request I can probably fix it pretty quickly

FrugoFruit90 commented 8 years ago

Hello, I really appreciate your help! Here is the wsdl: https://test-dilo.nfz.gov.pl/ws-broker-server-pkus-auth/services/Auth?wsdl This is the code I used when trying to emulate your approach:

transport = Transport(verify=False)
client = Client('https://test-dilo.nfz.gov.pl/ws-broker-server-pkus-auth/services/Auth?wsdl', transport=transport)
response = pretend.stub(
    status_code=200,
    headers=[],
    content="""
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <ns1:session id="37A6660D3B6ECAB97C1D4E9E757061B3" xmlns:ns1="http://xml.kamsoft.pl/ws/common"/>
      <ns1:authToken id="BSPApF6asPCpmmBUOuj_13" xmlns:ns1="http://xml.kamsoft.pl/ws/common"/>
   </soapenv:Header>
   <soapenv:Body>
      <ns1:loginReturn xmlns:ns1="http://xml.kamsoft.pl/ws/kaas/login_types">[000] U&amp;#380;ytkownik zosta&amp;#322; prawid&amp;#322;owo zalogowany.</ns1:loginReturn>
   </soapenv:Body>
</soapenv:Envelope>
    """)
result = client.service._binding.process_reply(client,
                                               client.service._binding._operations['login'],
                                               response)

print(result)

and here we have the traceback (and nothing else in the console except the warning about skipping SSL verification:

Traceback (most recent call last):
  File "C:/Users/j.zysko/gabinet/dilo/zeep_utils.py", line 76, in <module>
    response)
  File "C:\Users\j.zysko\Envs\gabinet\lib\site-packages\zeep\wsdl\soap.py", line 143, in process_reply
    return operation.process_reply(doc)
  File "C:\Users\j.zysko\Envs\gabinet\lib\site-packages\zeep\wsdl\soap.py", line 276, in process_reply
    return self.output.deserialize(body)
  File "C:\Users\j.zysko\Envs\gabinet\lib\site-packages\zeep\wsdl\messages.py", line 241, in deserialize
    assert elm is not None, '%s not found' % part.element.qname
AssertionError: {http://xml.kamsoft.pl/ws/common}session not found

I guess I'm doing something wrong? Since it's the same traceback as the one that I posted above.

mvantellingen commented 8 years ago

Oh no you are doing it correctly! Thanks! Since now I can easily reproduce it I might be able to fix it pretty quickly. :-)

mvantellingen commented 8 years ago

This turns out to be related to #180. I'm closing this one in favour of that one.

FrugoFruit90 commented 8 years ago

Hey, this is excellent :) Waiting for the next version, keep up with the good work! Cheers

mvantellingen commented 8 years ago

@FrugoFruit90 are you able to test #205 ?

FrugoFruit90 commented 8 years ago

@mvantellingen yes the service in #177 now works correctly, thank you very much! And sorry for taking so long, was my first time cloning a pull request so I actually had to find an hour to figure out how to do it.