a
    ž˜ëj  ã                   @  s–   d dl mZ d dlZd dlmZmZmZmZ d dlZd dl	m
Z
 d dlmZmZmZmZmZ d dlmZ d dlmZ e e
¡ZG dd	„ d	eeeƒZdS )
é    )ÚannotationsN)ÚOptionalÚTupleÚUnionÚDefaultDict)ÚUNSTRUCTURED_CLIENT_LOGGER_NAME)ÚAfterSuccessContextÚAfterErrorContextÚAfterErrorHookÚSDKInitHookÚAfterSuccessHook)Ú
HttpClient)Údefaultdictc                   @  sh   e Zd ZdZddœdd„Zdddd	œd
d„Zddddœdd„Zddddœdd„Zdddddœdd„ZdS )Ú
LoggerHookzHook providing custom loggingÚNone)Úreturnc                 C  s   t tƒ| _d S ©N)r   ÚintÚretries_counter)Úself© r   úl/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured_client/_hooks/custom/logger_hook.pyÚ__init__   s    zLoggerHook.__init__zOptional[httpx.Response]zOptional[Exception]Ústr)ÚresponseÚerrorÚoperation_idc                 C  sn   |durB|j d dkrBt d|j | j| ¡ |jrjt d|j¡ n(|durjt|tjƒrjt d|| j| ¡ dS )z3Log retries to give users visibility into requests.Néd   é   zpFailed to process a request due to API server error with status code %d. Attempting retry number %d after sleep.zServer message - %szaFailed to process a request due to connection error - %s. Attempting retry number %d after sleep.)Ústatus_codeÚloggerÚinfor   ÚtextÚ
isinstanceÚhttpxZConnectError)r   r   r   r   r   r   r   Úlog_retries   s    üüzLoggerHook.log_retriesr   zTuple[str, HttpClient])Úbase_urlÚclientr   c                 C  s   t jt jdd� ||fS )Nz%(levelname)s: %(message)s)ÚlevelÚformat)ÚloggingÚbasicConfigÚINFO)r   r&   r'   r   r   r   Úsdk_init2   s    zLoggerHook.sdk_initr   zhttpx.Responsez Union[httpx.Response, Exception])Úhook_ctxr   r   c                 C  s   | j  |jd ¡ |S r   )r   Úpopr   )r   r.   r   r   r   r   Úafter_success8   s    zLoggerHook.after_successr	   zFUnion[Tuple[Optional[httpx.Response], Optional[Exception]], Exception])r.   r   r   r   c                 C  sp   | j |j  d7  < |  |||j¡ |r:|jdkr:||fS |rPt d|j|j¡ |durhtjd||d� ||fS )z+Concrete implementation for AfterErrorHook.é   éÈ   zServer responded with %d - %sNzFollowing error occurred - %s)Úexc_info)r   r   r%   r   r    r   r"   )r   r.   r   r   r   r   r   Úafter_errorC   s    zLoggerHook.after_errorN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r-   r0   r4   r   r   r   r   r      s   r   )Ú
__future__r   r*   Útypingr   r   r   r   r$   Z(unstructured_client._hooks.custom.commonr   Z unstructured_client._hooks.typesr   r	   r
   r   r   Zunstructured_client.httpclientr   Úcollectionsr   Ú	getLoggerr    r   r   r   r   r   Ú<module>   s   
