o
    gE                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZmZmZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZmZ d d
lmZmZ d dlmZ d dlmZm Z  ej!red dl"m#Z# e $e%Z&G dd deZ'G dd de'Z(G dd de'Z)G dd deZ*dS )    N)etree)MultipartDecoder)nspluginswsa)FaultTransportErrorXMLSyntaxError)	parse_xml)as_qnameget_media_type
qname_attr)MessagePack)Binding	Operation)DocumentMessage
RpcMessage)process_xop)etree_to_stringurl_http_to_https)
Definitionc                       st   e Zd ZdZ fddZedd Zdd Zdd	d
Zdd Z	dd Z
dd Zdd ZdddZedd Z  ZS )SoapBindingzSoap 1.1/1.2 bindingc                    s    t  ||| || _|| _dS )a[  The SoapBinding is the base class for the Soap11Binding and
        Soap12Binding.

        :param wsdl:
        :type wsdl:
        :param name:
        :type name: string
        :param port_name:
        :type port_name: string
        :param transport:
        :type transport: zeep.transports.Transport
        :param default_style:

        N)super__init__	transportdefault_style)selfwsdlname	port_namer   r   	__class__ P/home/ubuntu/webapp/venv/lib/python3.10/site-packages/zeep/wsdl/bindings/soap.pyr      s   
zSoapBinding.__init__c                 C   s   |j d| jd}|duS )zCheck if this binding instance should be used to parse the given
        node.

        :param node: The node to match against
        :type node: lxml.etree._Element

        soap:binding
namespacesN)findnsmap)clsnode	soap_noder"   r"   r#   match.   s   	zSoapBinding.matchc                 O   s   |  |||\}}|S N)_create)r   	operationargskwargsenvelopehttp_headersr"   r"   r#   create_message:   s   zSoapBinding.create_messageNc                 C   s   |  |}|std| |j|i |}| || |j}|j}	|re|s)|jj}|jj	r9t
 ||	||\}}	t|||	||\}}	|jret|jtr\|jD ]
}
|
||	\}}	qPn	|j||	\}}	|jjrp|	|jj ||	fS )zCreate the XML document to send to the server.

        Note that this generates the soap envelope without the wsse applied.

        zOperation %r not found)get
ValueErrorcreate_set_http_headerscontentheadersservice_binding_optionsabstract
wsa_actionr   WsAddressingPluginegressr   apply_egresswsse
isinstancelistapplysettingsextra_http_headersupdate)r   r/   r0   r1   clientoptionsoperation_obj
serializedr2   r3   rB   r"   r"   r#   r.   >   s4   


zSoapBinding._createc           
      C   sP   | j |||||d\}}|j|d ||}| |}	|jjr!|S | ||	|S )a  Called from the service

        :param client: The client with which the operation was called
        :type client: zeep.client.Client
        :param options: The binding options
        :type options: dict
        :param operation: The operation object from which this is a reply
        :type operation: zeep.wsdl.definitions.Operation
        :param args: The args to pass to the operation
        :type args: tuple
        :param kwargs: The kwargs to pass to the operation
        :type kwargs: dict

        rI   rJ   address)r.   r   post_xmlr5   rF   raw_responseprocess_reply
r   rI   rJ   r/   r0   r1   r2   r3   responserK   r"   r"   r#   sendl   s   


zSoapBinding.sendc           
         sX   | j |||||d\}}|j|d ||I dH }|jjr |S | |}	| ||	|S )a	  Called from the async service

        :param client: The client with which the operation was called
        :type client: zeep.client.Client
        :param options: The binding options
        :type options: dict
        :param operation: The operation object from which this is a reply
        :type operation: zeep.wsdl.definitions.Operation
        :param args: The args to pass to the operation
        :type args: tuple
        :param kwargs: The kwargs to pass to the operation
        :type kwargs: dict

        rM   rN   N)r.   r   rO   rF   rP   r5   rQ   rR   r"   r"   r#   
send_async   s   




zSoapBinding.send_asyncc              
   C   s|  |j dv r
|js
dS |j dkr|jstd|j  |j d|jdd}t|}d}|dkrOt|j||jp5d	}|jd
 j}t	|jdkrNt
|jdd d}n|j}zt|| j|jd}	W n tyy }
 ztd|j |
|jf |j |jdd}
~
ww |rt|	|rd}|jr|j|	 t||	|j|\}	}|	jd| jd}|j dks|dur| |	|S ||	}|r|| |S |S )a  Process the XML reply from the server.

        :param client: The client with which the operation was called
        :type client: zeep.client.Client
        :param operation: The operation object from which this is a reply
        :type operation: zeep.wsdl.definitions.Operation
        :param response: The response object returned by the remote server
        :type response: requests.Response

        )      N   z5Server returned HTTP status %d (no content available))status_codeContent-Typeztext/xmlzmultipart/relatedzutf-8r      )parts)rF   z?Server returned response (%s) with invalid XML: %s.
Content: %r)rY   r9   soap-env:Body/soap-env:Faultr%   )rY   r9   r   r:   r5   r   r   encodingr\   lenr   r
   r   rF   r	   r   rB   verifyr   apply_ingressr'   r(   process_errorrQ   	_set_root)r   rI   r/   rS   content_type
media_typemessage_packdecoderr9   docexcr3   
fault_noderesultr"   r"   r#   rQ      sb   	



zSoapBinding.process_replyc                 C   s   t r-   )NotImplementedError)r   rh   r/   r"   r"   r#   rb      s   zSoapBinding.process_errorFc                 C   s`   |j d| jd}|d u rtd d S |d}|r,|r,t|}||dkr,td d|iS )Nzsoap:addressr%   z'No valid soap:address found for servicelocationz&Forcing soap:address location to HTTPSrN   )r'   r(   loggerdebugr5   r   warning)r   
xmlelementforce_httpsaddress_noderm   r"   r"   r#   process_service_port   s   


z SoapBinding.process_service_portc                 C   s   t |d|j}t |d|j}|jd| jd}|d}ddg}||vr)td| |d	d
}| |j||||}	|jd| jdD ]}
tj	||
|	| jd}|	
| q@|	S )a  

        Definition::

            <wsdl:binding name="nmtoken" type="qname"> *
                <-- extensibility element (1) --> *
                <wsdl:operation name="nmtoken"> *
                   <-- extensibility element (2) --> *
                   <wsdl:input name="nmtoken"? > ?
                       <-- extensibility element (3) -->
                   </wsdl:input>
                   <wsdl:output name="nmtoken"? > ?
                       <-- extensibility element (4) --> *
                   </wsdl:output>
                   <wsdl:fault name="nmtoken"> *
                       <-- extensibility element (5) --> *
                   </wsdl:fault>
                </wsdl:operation>
            </wsdl:binding>
        r   typer$   r%   r   z$http://schemas.xmlsoap.org/soap/httpz-http://www.w3.org/2003/05/soap/bindings/HTTP/z:The binding transport %s is not supported (only soap/http)styledocumentzwsdl:operation)r(   )r   target_namespacer'   r(   r5   rl   r   findallSoapOperationparse_operation_add)r)   definitionsrq   r   r   r+   r   supported_transportsr   objr*   r/   r"   r"   r#   r{      s&   
zSoapBinding.parse)NN)F)__name__
__module____qualname____doc__r   classmethodr,   r4   r.   rT   rU   rQ   rb   rt   r{   __classcell__r"   r"   r    r#   r      s    

.H
r   c                   @   2   e Zd ZejejejejdZdd Z	dd Z
dS )Soap11Bindingsoapsoap-envr   xsdc                    s`   |j d| jd  d u rtdd d t|d fdd}t|d|d|d	 j d
 jdd)Nr]   r%   Unknown fault occuredmessagecodeactordetailc                    s"    j |  jd}|d ur|jS d S )Nr%   )r'   r(   textr   childrj   r"   r#   get_textD  s   z-Soap11Binding.process_error.<locals>.get_textfaultstring	faultcode
faultactorr   )r'   r(   r   r   )r   rh   r/   r   r"   r   r#   rb   9  s   zSoap11Binding.process_errorc                 C   s   d|j d< d S )Nztext/xml; charset=utf-8rZ   )r:   r   rL   r/   r"   r"   r#   r8   P  s   zSoap11Binding._set_http_headersN)r   r   r   r   SOAP_11SOAP_ENV_11WSDLXSDr(   rb   r8   r"   r"   r"   r#   r   1  s    r   c                   @   r   )Soap12Bindingr   c                    s   |j d| jd  d u rtdd d t|d fdd} jd| jd} jd| jd}g } j d	| jd}|d ur[|j d
| jd}t|j|jd }	||	 |j d| jd}|d us: j d| jd}
t||d |
|d)Nr]   r%   r   r   c                    s     | }|d ur|jS d S r-   )r'   r   r   r   r"   r#   r   g  s   
z-Soap12Binding.process_error.<locals>.get_textzsoap-env:Reason/soap-env:Textzsoap-env:Code/soap-env:Valuezsoap-env:Code/soap-env:Subcodezsoap-env:Valuezsoap-env:Subcodezsoap-env:Detail)r   r   r   r   subcodes)r'   r(   r   r   findtextr   r   append)r   rh   r/   r   r   r   r   subcode_elementsubcode_value_elementsubcode_qnamedetail_noder"   r   r#   rb   \  sN   

zSoap12Binding.process_errorc                 C   s    d ddd|j g|jd< d S )Nz; zapplication/soap+xmlzcharset=utf-8zaction="%s"rZ   )join
soapactionr:   r   r"   r"   r#   r8     s   zSoap12Binding._set_http_headersN)r   r   r   r   SOAP_12SOAP_ENV_12r   r   r(   rb   r8   r"   r"   r"   r#   r   T  s    3r   c                       sB   e Zd ZdZ fddZdd Zedd Zd fd
dZ  Z	S )rz   z3Represent's an operation within a specific binding.c                    s$   t  || || _|| _|| _d S r-   )r   r   r(   r   rv   )r   r   bindingr(   r   rv   r    r"   r#   r     s   
zSoapOperation.__init__c                 C   sF   t | jd d}|j|krtd|j|jf | jr!| j|S d S )Nr   EnvelopezpThe XML returned by the server does not contain a valid {%s}Envelope root element. The root element found is %s )r   QNamer(   tagr	   	namespaceoutputdeserialize)r   r2   envelope_qnamer"   r"   r#   rQ     s   

zSoapOperation.process_replyc                 C   s   | d}|jd|jd}d}|dur | d}| d|j}n|j}| |||||}	|dkr2t}
nt}
|D ])}t|jj	}|dvrDq6|
j
|||	||d	}|d
krY||	j|j< q6t|	|| q6|	S )a  

        Definition::

            <wsdl:operation name="nmtoken"> *
                <soap:operation soapAction="uri"? style="rpc|document"?>?
                <wsdl:input name="nmtoken"? > ?
                    <soap:body use="literal"/>
               </wsdl:input>
               <wsdl:output name="nmtoken"? > ?
                    <-- extensibility element (4) --> *
               </wsdl:output>
               <wsdl:fault name="nmtoken"> *
                    <-- extensibility element (5) --> *
               </wsdl:fault>
            </wsdl:operation>

        Example::

            <wsdl:operation name="GetLastTradePrice">
              <soap:operation soapAction="http://example.com/GetLastTradePrice"/>
              <wsdl:input>
                <soap:body use="literal"/>
              </wsdl:input>
              <wsdl:output>
              </wsdl:output>
              <wsdl:fault name="dataFault">
                <soap:fault name="dataFault" use="literal"/>
              </wsdl:fault>
            </operation>

        r   zsoap:operationr%   N
soapActionrv   rpc)inputr   fault)r}   rq   r/   r(   ru   r   )r5   r'   r(   r   r   r   r   r   r   	localnamer{   faultsr   setattr)r)   r}   rq   r   r(   r   r+   actionrv   r   message_classr*   tag_namemsgr"   r"   r#   r{     s4   
"
zSoapOperation.parser}   r   c                    sx   t  | | j D ]\}}|| jjv r||| jj|  q| jr,| j|| jj | jr:| j|| jj	 d S d S r-   )
r   resolver   itemsr=   fault_messagesr   output_messager   input_message)r   r}   r   r   r    r"   r#   r     s   zSoapOperation.resolve)r}   r   )
r   r   r   r   r   rQ   r   r{   r   r   r"   r"   r    r#   rz     s    
Frz   )+loggingtypinglxmlr   #requests_toolbelt.multipart.decoderr   zeepr   r   r   zeep.exceptionsr   r   r	   zeep.loaderr
   
zeep.utilsr   r   r   zeep.wsdl.attachmentsr   zeep.wsdl.definitionsr   r   zeep.wsdl.messagesr   r   zeep.wsdl.messages.xopr   zeep.wsdl.utilsr   r   TYPE_CHECKINGzeep.wsdl.wsdlr   	getLoggerr   rn   r   r   r   rz   r"   r"   r"   r#   <module>   s,    
  #E