o
    gR                     @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ zd dlZW n eyK   dZY nw d	d
gZG dd
 d
ZG dd	 d	eZdS )    N)closingcontextmanager)urlparse)Response)FileAdapter)TransportError)get_media_typeget_version)etree_to_stringAsyncTransport	Transportc                   @   sX   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	e
dddZdd ZdS )r   a  The transport object handles all communication to the SOAP server.

    :param cache: The cache object to be used to cache GET requests
    :param timeout: The timeout for loading wsdl and xsd documents.
    :param operation_timeout: The timeout for operations (POST/GET). By
                              default this is None (no timeout).
    :param session: A :py:class:`request.Session()` object (optional)

    N,  c                 C   sZ   || _ || _|| _tt| _| | _|pt	 | _
| j
dt  dt  | j
jd< d S )Nzfile://Zeep/%s (www.python-zeep.org)
User-Agent)cacheload_timeoutoperation_timeoutlogging	getLogger__name__logger_close_sessionrequestsSessionsessionmountr   r	   headers)selfr   timeoutr   r    r   H/home/ubuntu/webapp/venv/lib/python3.10/site-packages/zeep/transports.py__init__"   s   zTransport.__init__c                 C   s   | j j|||| jd}|S )zProxy to requests.get()

        :param address: The URL for the request
        :param params: The query parameters
        :param headers: a dictionary with the HTTP headers.

        )paramsr   r   )r   getr   r   addressr"   r   responser   r   r    r#   /   s   
zTransport.getc                 C   s   | j tjr|}t|tr|d}| j d|| | jj	|||| j
d}| j tjrXt|jdd}|dkr>|j}n|j}t|trN||jpLd}| j d||j| |S )zProxy to requests.posts()

        :param address: The URL for the request
        :param message: The content for the body
        :param headers: a dictionary with the HTTP headers.

        zutf-8HTTP Post to %s:
%s)datar   r   zContent-Typeztext/xmlzmultipart/related&HTTP Response from %s (status: %d):
%s)r   isEnabledForr   DEBUG
isinstancebytesdecodedebugr   postr   r   r   r#   contentencodingstatus_code)r   r%   messager   log_messager&   
media_typer   r   r    r0   <   s0   



zTransport.postc                 C   s   t |}| |||S )a.  Post the envelope xml element to the given address with the headers.

        This method is intended to be overriden if you want to customize the
        serialization of the xml element. By default the body is formatted
        and encoded as utf-8. See ``zeep.wsdl.utils.etree_to_string``.

        )r
   r0   )r   r%   enveloper   r4   r   r   r    post_xmlc   s   zTransport.post_xmlc                 C   s   |st dt|j}|dv r/| jr| j|}|rt|S | |}| jr-| j|| |S tt	j
|d}| W  d   S 1 sGw   Y  dS )z#Load the content from the given URLzNo url given to load)httphttpsfilerbN)
ValueErrorr   schemer   r#   r-   _load_remote_dataaddopenospath
expanduserread)r   urlr>   r&   r1   fhr   r   r    loadn   s   

$zTransport.loadc                 C   sZ   | j d| | jj|| jd}t| |  |jW  d    S 1 s&w   Y  d S )NzLoading remote data from: %s)r   )r   r/   r   r#   r   r   raise_for_statusr1   )r   rF   r&   r   r   r    r?      s   
$zTransport._load_remote_datac                 c   s    | j }|| _ dV  || _ dS )ab  Context manager to temporarily overrule options.

        Example::

            transport = zeep.Transport()
            with transport.settings(timeout=10):
                client.service.fast_call()

        :param timeout: Set the timeout for POST/GET operations (not used for
                        loading external WSDL or XSD documents)

        N)r   )r   r   old_timeoutr   r   r    settings   s
   
zTransport.settingsc                 C   s   | j r
| j  d S d S N)r   r   closer   r   r   r    __del__   s   zTransport.__del__)Nr   NNrL   )r   
__module____qualname____doc__r!   r#   r0   r8   rH   r?   r   rK   rO   r   r   r   r    r      s    

'c                   @   sX   e Zd ZdZ							dddZdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dS )r   zAsynchronous Transport class using httpx.

    Note that loading the wsdl is still a sync process since and only the
    operations can be called via async.

    Nr   Tc                 C   s|   t d u rtdd| _|| _|pt j|||d| _|p"t j|||d| _t	t
| _ddt  i| j_ddt  i| j_d S )Nz/The AsyncTransport is based on the httpx moduleF)verifyproxiesr   r   r   )httpxRuntimeErrorr   r   Clientwsdl_clientAsyncClientclientr   r   r   r   r	   r   )r   rZ   rX   r   r   r   
verify_sslproxyr   r   r    r!      s&   


zAsyncTransport.__init__c                    s   | j  I d H  d S rL   )rZ   acloserN   r   r   r    r]      s   zAsyncTransport.aclosec                 C   s@   | j |}| }z|  W |S  tjy   t|jdw )N)r3   )rX   r#   rE   rI   rU   HTTPStatusErrorr   r3   )r   rF   r&   resultr   r   r    r?      s   
z AsyncTransport._load_remote_datac                    sF   | j d|| | jj|||dI d H }| j d||j|  |S )Nr'   )r1   r   r)   )r   r/   rZ   r0   r3   rE   )r   r%   r4   r   r&   r   r   r    r0      s   zAsyncTransport.postc                    s(   t |}| |||I d H }| |S rL   )r
   r0   new_response)r   r%   r7   r   r4   r&   r   r   r    r8      s   
zAsyncTransport.post_xmlc                    s$   | j j|||dI d H }| |S )N)r"   r   )rZ   r#   r`   r$   r   r   r    r#      s   
zAsyncTransport.getc                 C   s8   |  }t }||_|j|_|j|_|j|_|j|_|S )z@Convert an aiohttp.Response object to a requests.Response object)rE   r   _contentr3   r   cookiesr2   )r   r&   bodynewr   r   r    r`      s   zAsyncTransport.new_response)NNNr   NTN)r   rP   rQ   rR   r!   r]   r?   r0   r8   r#   r`   r   r   r   r    r      s     	
"
)r   rB   
contextlibr   r   urllib.parser   r   r   requests_filer   zeep.exceptionsr   
zeep.utilsr   r	   zeep.wsdl.utilsr
   rU   ImportError__all__r   r   r   r   r   r    <module>   s&     