U
    g                  
   @   s   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
 d dlmZ zd dlZW n, ek
r Z zedeW 5 dZ[X Y nX G dd	 d	e	ZdS )
    )OptionalUnion)cmstsp)LazySession)TimeStamperTimestampRequestError)set_tsp_headersNz?Install pyHanko with the [async_http] optional dependency groupc                       s|   e Zd Zdeejef eej d fddZ	e
ddd	Zejdd
dZejd fddZejejdddZ  ZS )AIOHttpTimeStamperF   N)sessionauthc                    sB   |r| dstd|| _|| _|| _|| _|| _t   dS )a  
        Initialise the timestamp client.

        :param url:
            URL where the server listens for timestamp requests.
        :param https:
            Enforce HTTPS.
        :param timeout:
            Timeout (in seconds)
        :param auth:
            `aiohttp.BasicAuth` object with authentication credentials.
        :param headers:
            Other headers to include.
        zhttps:zTimestamp URL is not HTTPS.N)	
startswith
ValueErrorurltimeoutr   headers_sessionsuper__init__)selfr   r   httpsr   r   r   	__class__ J/tmp/pip-unpacked-wheel-owvgwkas/pyhanko/sign/timestamps/aiohttp_client.pyr      s    zAIOHttpTimeStamper.__init__)returnc                    s   t | jp
i S )z
        Format the HTTP request headers.
        Subclasses can override this to perform their own header generation
        logic.

        :return:
            Header dictionary.
        )r	   r   )r   r   r   r   async_request_headers2   s    	z(AIOHttpTimeStamper.async_request_headersc                    s&   | j }t|tr| I d H S |S d S N)r   
isinstancer   get_session)r   r   r   r   r   r    =   s    
zAIOHttpTimeStamper.get_sessionc                    s   t  ||I d H S r   )r   async_timestamp)r   Zmessage_digestZmd_algorithmr   r   r   r!   D   s    z"AIOHttpTimeStamper.async_timestamp)reqr   c           
   
      s   |   I d H }tj| jd}|  I d H }z|j| j|| | jd|d4 I d H L}|	 I d H }|j
d}|dkrd| d}tj|j|j||j
dW 5 Q I d H R X W n. tjk
r }	 ztd	|	W 5 d }	~	X Y nX tj|S )
N)totalT)r   r   datar   raise_for_statusr   zContent-Typezapplication/timestamp-replyz?Bad content type. Expected application/timestamp-reply,but got .)messager   z(Error while contacting timestamp service)r    aiohttpZClientTimeoutr   r   postr   dumpr   readr   getZContentTypeErrorZrequest_infohistoryZClientErrorr   r   TimeStampRespload)
r   r"   r   Z
cl_timeoutr   responseZresponse_datactmsger   r   r   async_request_tsa_responseI   s>    
z-AIOHttpTimeStamper.async_request_tsa_response)Fr   NN)__name__
__module____qualname__r   r(   ZClientSessionr   r   Z	BasicAuthr   dictr   r    r   ZContentInfor!   r   ZTimeStampReqr.   r4   __classcell__r   r   r   r   r
      s        	r
   )typingr   r   Z
asn1cryptor   r   Z4pyhanko_certvalidator.fetchers.aiohttp_fetchers.utilr   Zpyhanko.sign.timestampsr   r   Z$pyhanko.sign.timestamps.common_utilsr	   r(   ImportError_er
   r   r   r   r   <module>   s   