
    OǻiK;                       d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
mZmZmZmZmZ d dlmZmZ d dlmZmZ d dlmZmZ g dZd d	lmZ d d
lmZ d dlmZ dZ dZ!h dZ"e G d d             Z# G d de$      Z% G d d      Z&y)    )annotationsN)	dataclass)AnyDictMappingOptionalTupleUnion)	HTTPErrorURLError)	urlencodeurljoin)Requesturlopen)
HttpClientHttpResponse	HttpErrorDEFAULT_TIMEOUT)ExponentialWithJitterBackoff)Retry
dummy_failz)HttpClient/1.0 (+https://example.invalid)g      >@>             c                  P    e Zd ZU ded<   ded<   ded<   ded<   ddd
ZddZddZy	)r   intstatusDict[str, str]headersstrurlbytescontentNc                d    |xs | j                         }| j                  j                  |d      S )Nreplace)errors)_get_encodingr&   decode)selfencodingencs      T/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/redis/http/http_client.pytextzHttpResponse.text    s/    .$,,.||""3y"99    c                h    t        j                  | j                  | j                                     S )N)r-   )jsonloadsr0   r*   )r,   s    r/   r3   zHttpResponse.json$   s%    zz$))T-?-?-A)BCCr1   c                   | j                   j                  dd      }|j                  d      D ]Z  }|j                         }|j	                         j                  d      s3|j                  dd      d   j                         xs dc S  y)Ncontent-type ;zcharset==   utf-8)r"   getsplitstriplower
startswith)r,   ctypepartps       r/   r*   zHttpResponse._get_encoding'   sw      4KK$ 	=D

Awwy##J/wwsAq)//1<W<	= r1   N)r-   Optional[str]returnr#   )rF   r   )rF   r#   )__name__
__module____qualname____annotations__r0   r3   r*    r1   r/   r   r      s'    K	HN:Dr1   r   c                  "     e Zd Zdd fdZ xZS )r   c                v    || _         || _        |xs d| d| | _        t        |   | j                         y )NzHTTP z for )r    r$   messagesuper__init__)r,   r    r$   rN   	__class__s       r/   rP   zHttpError.__init__3   s;    <E&se"<&r1   rD   )r    r   r$   r#   rN   rE   )rG   rH   rI   rP   __classcell__)rQ   s   @r/   r   r   2   s    ' 'r1   r   c                  D   e Zd ZdZdde e edd      d      d	dddddddef	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd
Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ
	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 d 	 	 	 	 	 d!dZd"dZ	 	 	 	 	 d#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$dZ	 d 	 	 	 	 	 d%dZ	 d&	 	 	 	 	 d'dZ	 	 	 	 	 	 d(dZd)dZd*dZy)+r   z7
    A lightweight HTTP client for REST API calls.
    r7   Nr:   
   )basecap   )backoffretriesTc                   |r$|j                  d      s|j                         dz   n|| _        |xs i j                         D ci c]  \  }}|j	                         | c}}| _        || _        || _        | j                  j                  t        t        t        j                  f       || _        || _        || _        || _        |	| _        |
| _        || _        || _        || _        yc c}}w )a  
        Initialize a new HTTP client instance.

        Args:
            base_url: Base URL for all requests. Will be prefixed to all paths.
            headers: Default headers to include in all requests.
            timeout: Default timeout in seconds for requests.
            retry: Retry configuration for failed requests.
            verify_tls: Whether to verify TLS certificates.
            ca_file: Path to CA certificate file for TLS verification.
            ca_path: Path to a directory containing CA certificates.
            ca_data: CA certificate data as string or bytes.
            client_cert_file: Path to client certificate for mutual TLS.
            client_key_file: Path to a client private key for mutual TLS.
            client_key_password: Password for an encrypted client private key.
            auth_basic: Tuple of (username, password) for HTTP basic auth.
            user_agent: User-Agent header value for requests.

        The client supports both regular HTTPS with server verification and mutual TLS
        authentication. For server verification, provide CA certificate information via
        ca_file, ca_path or ca_data. For mutual TLS, additionally provide a client
        certificate and key via client_cert_file and client_key_file.
        /N)endswithrstripbase_urlitemsr?   _default_headerstimeoutretryupdate_supported_errorsr   r   sslSSLError
verify_tlsca_fileca_pathca_dataclient_cert_fileclient_key_fileclient_key_password
auth_basic
user_agent)r,   r^   r"   ra   rb   rf   rg   rh   ri   rj   rk   rl   rm   rn   kvs                   r/   rP   zHttpClient.__init__?   s    Z  1 1# 6 OO# 	
 <C=b:O:O:Q R$!QA R


**Ix+NO$  0.#6 $$ !Ss   C+c           	     2    | j                  d||||d |      S )NGETparamsr"   ra   bodyexpect_json
_json_callr,   pathrt   r"   ra   rv   s         r/   r<   zHttpClient.get   s/     #  
 	
r1   c           	     2    | j                  d||||d |      S )NDELETErs   rw   ry   s         r/   deletezHttpClient.delete   s/     #  
 	
r1   c                T    | j                  d||||| j                  ||      |      S )NPOST	json_bodydatars   rx   _prepare_bodyr,   rz   r   r   rt   r"   ra   rv   s           r/   postzHttpClient.post   s?     ##id#C#  
 	
r1   c                T    | j                  d||||| j                  ||      |      S )NPUTr   rs   r   r   s           r/   putzHttpClient.put   s?     ##id#C#  
 	
r1   c                T    | j                  d||||| j                  ||      |      S )NPATCHr   rs   r   r   s           r/   patchzHttpClient.patch   s?     ##id#C#  
 	
r1   c                ,     j                  ||      } j                  ||      }t        |t              r|j	                  d      n|}	t        ||j                         |	|      d |j                         j                  d      r j                  rut        j                   j                   j                   j                         j                  rcj!                   j                   j"                   j$                         n0t        j                         d_        t        j(                  _        	  j,                  j/                   fdd  fd	      S # t0        $ r}
d
}	 |
j3                         }n# t4        $ r Y nw xY w|
j6                  xs i j9                         D ci c]  \  }}|j                         | nc c}}w }}} j;                  ||      }t=        |
dd      xs d}t?        ||||      }|cY d }
~
S d }
~
ww xY w)Nr;   )r$   methodr   r"   https)cafilecapathcadata)certfilekeyfilepasswordFc                 ,    j                         S )N)contextra   )_make_request)r   reqr,   ra   s   r/   <lambda>z$HttpClient.request.<locals>.<lambda>  s    **3*Q r1   c                    t               S rD   r   )_s    r/   r   z$HttpClient.request.<locals>.<lambda>  s    *, r1   c                &    j                  |       S rD   )_is_retryable_http_error)errorr,   s    r/   r   z$HttpClient.request.<locals>.<lambda>  s    d;;EB r1   r1   coder   r    r"   r$   r&   ) 
_build_url_prepare_headers
isinstancer#   encoder   upperr?   r@   rf   rd   create_default_contextrg   rh   ri   rj   load_cert_chainrk   rl   check_hostname	CERT_NONEverify_moderb   call_with_retryr   read	Exceptionr"   r_   _maybe_decompressgetattrr   )r,   r   rz   rt   r"   ru   ra   r$   all_headersr   eerr_bodyro   rp   headers_mapr    responser   r   s   `     `          @@r/   requestzHttpClient.request   s    oodF+++GT:'1$'<t{{7#$#flln4U,099;!!'*44<<<<<< ((++!%!6!6 $ 4 4!%!9!9 ,  446).&&)mm#	::--Q&B 
  	H668 56YY_"4K4K4MNDAq1779a<NNKN--hDHQ*/aF## 	H O!	sN   <&E# #	H,H/F ?H 	F	HF$H0G=HHHc                z   t        ||xs | j                  |      5 }|j                         }|j                  j	                         D ci c]  \  }}|j                         | }}}| j                  ||      }t        |j                  ||j                         |      cd d d        S c c}}w # 1 sw Y   y xY w)N)ra   r   r   )
r   ra   r   r"   r_   r?   r   r   r    geturl)	r,   r   r   ra   resprawro   rp   r   s	            r/   r   zHttpClient._make_request+  s     Wg&=wO 		SW))+C48LL4F4F4HIDAq1779a<IKI((k:C{{#KKM				 		I		 		s   .B1B+&;B1+B11B:c                Z    t        |t              r| j                  |j                        S y)NF)r   r   _should_retry_statusr   )r,   r   s     r/   r   z#HttpClient._is_retryable_http_error<  s$    eY',,UZZ88r1   c                    | j                  ||||||      }d|j                  cxk  rdk  s1n t        |j                  |j                  |j	                               |r|j                         S |S )N)r   rz   rt   r"   ru   ra      i  )r   r    r   r$   r0   r3   )	r,   r   rz   rt   r"   ra   ru   rv   r   s	            r/   rx   zHttpClient._json_callB  sm     ||  
 t{{(S(DKK499;??99;r1   c                X    ||t        d      |t        j                  |dd      S |S )Nz+Provide either json_body or data, not both.F),:)ensure_ascii
separators)
ValueErrorr3   dumps)r,   r   r   s      r/   r   zHttpClient._prepare_body\  s9      T%5JKK ::ie
SSr1   c                    t        | j                  xs d|      }|rFt        |j                         D ci c]  \  }}|	|| c}}d      }d|v rdnd}|r| | | n|}|S c c}}w )Nr7   T)doseq?&)r   r^   r   r_   )r,   rz   rt   r$   ro   rp   query	separators           r/   r   zHttpClient._build_urle  sx     dmm)r40"(,,.B$!QAMAB$E !$s
I05SE)UG,3C
	 Cs   
A%
 A%
c                    i }|j                  | j                         |j                  dd       |j                  d| j                         |j                  dd       |"t	        |t
              r|j                  dd       | j                  rSd|vrO| j                  \  }}t        j                  | d	| j                  d
            j                  d      }d| |d<   |r+|j                         D ]  \  }}|||j                         <    |S )Nacceptzapplication/jsonz
user-agentzaccept-encodingzgzip, deflater6   zapplication/json; charset=utf-8authorizationr   r;   asciizBasic )updater`   
setdefaultrn   r   r#   rm   base64	b64encoder   r+   r_   r?   )	r,   r"   ru   prepareduserpwdtokenro   rp   s	            r/   r   zHttpClient._prepare_headersv  s    $&--. 	H&89L$//:-? 
4 50QR ??h>ID#$$vQse_%;%;G%DELLWUE*0(8H_%  (1&'#(
 r1   c                    |t         v S rD   )RETRY_STATUS_CODES)r,   r    s     r/   r   zHttpClient._should_retry_status  s    +++r1   c                \   |s|S |j                  d      xs dj                         }	 d|v rt        j                  |      S d|v r&	 t	        j                  |t        j
                         S 	 |S # t        j                  $ r t	        j                  |      cY S w xY w# t        $ r |cY S w xY w)Nzcontent-encodingr7   gzipdeflate)r<   r?   r   
decompresszlib	MAX_WBITSr   r   )r,   r&   r"   r-   s       r/   r   zHttpClient._maybe_decompress  s    NKK 239r@@B	!w//H$4??7T^^ODD %  zz 4??7334 	N	s5   B B $A/ /(BB BB B+*B+)r^   r#   r"   Optional[Mapping[str, str]]ra   floatrb   r   rf   boolrg   rE   rh   rE   ri   zOptional[Union[str, bytes]]rj   rE   rk   rE   rl   rE   rm   zOptional[Tuple[str, str]]rn   r#   rF   None)NNNT)rz   r#   rt   GOptional[Mapping[str, Union[None, str, int, float, bool, list, tuple]]]r"   r   ra   Optional[float]rv   r   rF   Union[HttpResponse, Any])NNNNNT)rz   r#   r   Optional[Any]r   Optional[Union[bytes, str]]rt   r   r"   r   ra   r   rv   r   rF   r   )NNNN)r   r#   rz   r#   rt   r   r"   r   ru   r   ra   r   rF   r   )NN)r   r   r   zOptional[ssl.SSLContext]ra   r   )r   r   rF   r   )NNNNT)r   r#   rz   r#   rt   r   r"   r   ra   r   ru   r   rv   r   rF   r   )r   r   r   r   rF   r   rD   )rz   r#   rt   r   rF   r#   )r"   r   ru   r   rF   r!   )r    r   rF   r   )r&   r%   r"   zMapping[str, str]rF   r%   )rG   rH   rI   __doc__r   r   r   DEFAULT_USER_AGENTrP   r<   r}   r   r   r   r   r   r   rx   r   r   r   r   r   rK   r1   r/   r   r   :   s    /3(0aR@!
  !%!%/3*.)--104,%?%?% -?% 	?%
 ?% ?% ?% ?% -?% (?% '?%  +!?%" .#?%$ %?%& 
'?%N /3#' 



 -
 !
 
 
"
2 /3#' 



 -
 !
 
 
"
. $(,0 /3#' 

 !
 *	



 -
 !
 
 
"
2 $(,0 /3#' 

 !
 *	



 -
 !
 
 
"
2 $(,0 /3#' 

 !
 *	



 -
 !
 
 
"
: /3,0#'== =
	= -= *= != 
=D -1#'	 * !	" /3#',0  
	 - ! *  
"6 TX&5P	$ 
 
"2:U	@,r1   r   )'
__future__r   r   r   r3   rd   r   dataclassesr   typingr   r   r   r   r	   r
   urllib.errorr   r   urllib.parser   r   urllib.requestr   r   __all__redis.backoffr   redis.retryr   redis.utilsr   r   r   r   r   r   r   r   rK   r1   r/   <module>r      sy    "    
  ! = = , + +
H 6  "@ .    0'	 'o or1   