
    g`L                        d dl Z d dl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
mZ d dlmZ d dlZddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ d
ZdZdZdej4                  dej6                  e   fdZdej<                  dedefdZ dej<                  de!fdZ"dejF                  e$ej6                  ejJ                     f   dej6                  ejJ                     dejF                  e$ejJ                  f   fdZ&dej6                  ejJ                     dej6                  e   dej6                  ejJ                     dej6                  ejJ                     fdZ'dej6                  ejJ                     dej6                  ejJ                     dej6                  e   dej6                  ejJ                     dejP                  ej6                  ejJ                     ej6                  ejJ                     f   f
dZ) G d d      Z* G d d      Z+y)     N)asynccontextmanagercontextmanager)random   )File!convert_file_dict_to_httpx_tuples)jsonable_encoder)encode_query)remove_none_from_dict)RequestOptionsg      ?
      response_headersreturnc                    | j                  d      }|	 |dkD  rt        |      dz  S dS | j                  d      }|yt        j                  d|      rt        |      }nkt        j                  j                  |      }|y|d   |dd dz   |d	d z   }t        j                  j                  |      }|t        j                         z
  }|dk  rd}|S # t        $ r Y w xY w)
z
    This function parses the `Retry-After` header in a HTTP response and returns the number of seconds to wait.

    Inspired by the urllib3 retry implementation.
    zretry-after-msNr   i  zretry-afterz^\s*[0-9]+\s*$	   )r   r   )getint	Exceptionrematchfloatemailutilsparsedate_tz	mktime_tztime)r   retry_after_msretry_aftersecondsretry_date_tuple
retry_dates         L/var/www/openai/venv/lib/python3.12/site-packages/cohere/core/http_client.py_parse_retry_afterr$      s    &))*:;N!	1?!1C3~&-JJ #&&}5K 
xx!;/$ !;;33K@#A&
  03d:=Mbc=RR[[**+;<
tyy{*{N9  		s   C C 	C C responseretriesc                     t        | j                        }||t        k  r|S t        t        t        d|      z  t              }|ddt               z  z
  z  }|dk\  r|S dS )a  
    Determine the amount of time to wait before retrying a request.
    This function begins by trying to parse a retry-after header from the response, and then proceeds to use exponential backoff
    with a jitter to determine the number of seconds to wait.
    g       @r   g      ?r   )r$   headers#MAX_RETRY_DELAY_SECONDS_FROM_HEADERminINITIAL_RETRY_DELAY_SECONDSpowMAX_RETRY_DELAY_SECONDSr   )r%   r&   r   retry_delaytimeouts        r#   _retry_timeoutr0   C   sl     %X%5%56K;2U#U 1CW4EEG^_K Q01Gl7))    c                 H    g d}| j                   dk\  xs | j                   |v S )N)i  i  i  i  )status_code)r%   retriable_400ss     r#   _should_retryr5   W   s(    $N3&P(*>*>.*PPr1   originalomitc                 V    || S i }| j                         D ]  \  }}||us|||<    |S N)items)r6   r7   newkeyvalues        r#   remove_omit_from_dictr>   \   s@     |(*Cnn&
UCH ' Jr1   datarequest_optionsc                    | #|t        |j                  di             xs i S d S t        | t        j                        st        |       }|S i t        t        | |            |t        |j                  di             xs i ni }|S )Nadditional_body_parameters)r	   r   
isinstancetypingMappingr>   )r?   r@   r7   data_contents       r#   maybe_filter_request_bodyrG   i   s    
 | * _001MrRSYWY	
 	

 fnn-'- 
 5dD AB
 #. !!4!45QSU!VW][]
 r1   jsonc                 j    d }d }|t        |||      }nt        | ||      }|i k7  r|nd |i k7  r|fS d fS r9   )rG   )rH   r?   r@   r7   	json_body	data_bodys         r#   get_request_bodyrL      sV     II-dOTJ	 .dOTJ	 #bId)r/Y[[W[[[r1   c                      e Zd Zdddej                  dej                  g ej                  e   f   dej                  g ej                  e
e
f   f   dej                  ej                  g e
f      fdZdej                  e
   d	e
fd
Z	 dddddddddddd
dej                  e
   de
dej                  e
   dej                  ej                  e
ej                  f      dej                  ej                     dej                  ej                     dej                  ej                  eej                   e   ej"                  e   f      dej                  ej                  e
ej                  ej                  eej&                  e   f      f      dej                  ej                  e
ej                  f      dej                  e   dedej                  ej                     d	ej,                  fdZe	 dddddddddddd
dej                  e
   de
dej                  e
   dej                  ej                  e
ej                  f      dej                  ej                     dej                  ej                     dej                  ej                  eej                   e   ej"                  e   f      dej                  ej                  e
ej                  ej                  eej&                  e   f      f      dej                  ej                  e
ej                  f      dej                  e   dedej                  ej                     d	ej                   ej,                     fd       Zy)
HttpClientNbase_urlhttpx_clientbase_timeoutbase_headersrP   c                <    || _         || _        || _        || _        y r9   rP   rR   rS   rQ   selfrQ   rR   rS   rP   s        r#   __init__zHttpClient.__init__   #     !(((r1   maybe_base_urlr   c                 `    |}| j                   || j                         }|t        d      |S NzNA base_url is required to make this request, please provide one and try again.rP   
ValueErrorrW   rZ   rP   s      r#   get_base_urlzHttpClient.get_base_url   8    !==$)9}}Hmnnr1   r   
rP   paramsrH   r?   contentfilesr(   r@   r&   r7   pathmethodrc   rH   r?   rd   re   r(   r@   r&   r7   c                `   | j                  |      }|
"|
j                  d      |
j                  d      n| j                         }t        |||
|      \  }}| j                  j                  |t        j                  j                  | d|      t        t        i | j                         |	|	ni |
|
j                  di       xs i ni             t        t        t        t        i ||ni |
|
j                  di       xs i ni |                        ||||"||urt        t        t        |      |            nd |	      }|
|
j                  dd      nd}t        |	      rD||kD  r?t!        j"                  t%        ||
             | j                  ||||||||	|
|dz   |      S |S Ntimeout_in_secondsrH   r?   r@   r7   /additional_headersadditional_query_parameters	rg   urlr(   rc   rH   r?   rd   re   r/   max_retriesr   )r%   )r%   r&   r   )rf   rg   rP   rc   rH   rd   re   r(   r@   r&   r7   )r`   r   rR   rL   rQ   requesturllibparseurljoinr	   r   rS   r
   r>   r   r5   r   sleepr0   rW   rf   rg   rP   rc   rH   r?   rd   re   r(   r@   r&   r7   r/   rJ   rK   r%   rq   s                     r#   rr   zHttpClient.request   s      $$X. */B/BCW/X/d  45""$ 	  0TVelpq	9$$,,$$z^T:$%++-&-&97r SbRm?../CRHNBsu   )--3-?6R (7'B %4$7$78UWY$Z$`^`)+ !
"  %%t*; 22GH]^cHdfj2klK - &
P ETD_?..}a@ef(+W$

>8WMN||!%!##$3#aK $   r1   c             #     K   | j                  |      }|
"|
j                  d      |
j                  d      n| j                         }t        |||
|      \  }}| j                  j                  |t        j                  j                  | d|      t        t        i | j                         |	|	ni |
|
j                  di       ni             t        t        t        t        i ||ni |
|
j                  di       ni |                        ||||"||urt        t        t        |      |            nd |	      5 }| d d d        y # 1 sw Y   y xY ww)Nrj   rk   rl   rm   rn   ro   r`   r   rR   rL   rQ   streamrs   rt   ru   r	   r   rS   r
   r>   r   rW   rf   rg   rP   rc   rH   r?   rd   re   r(   r@   r&   r7   r/   rJ   rK   rz   s                    r#   rz   zHttpClient.stream  s    " $$X. */B/BCW/X/d  45""$ 	  0TVelpq	9%%$$z^T:$%++-&-&97r M\Lg?../CRHmo   )--3-?6R (7'B %4$7$78UWY$Z)+ !
"  %%t*; 22GH]^cHdfj2klK & &
L LO&
 &
 &
s   E EE	EEEr9   )__name__
__module____qualname__httpxClientrD   CallableOptionalr   DictstrrX   r`   AnyUnionbytesIteratorAsyncIteratorr   Listr   r   Responserr   r   rz    r1   r#   rN   rN      s    ?C) ll) oob&//%*@&@A	)
 oob&++c3h*?&?@) //&//"c'":;)6??3+? C  &*S
 *.@D,0,0mqkoAE;?,0Sooc"S 	S
 //#&S CO <=S oofjj)S oofjj)S eV__U5KVMaMabgMh.h!ijS v{{3TSYS^S^_cSdMd@e0f+fghS S&**_!=>S  8S S oofjj)S 
Sj  &*@
 *.@D,0,0mqkoAE;?,0@ooc"@ 	@
 //#&@ CO <=@ oofjj)@ oofjj)@ eV__U5KVMaMabgMh.h!ij@ v{{3TSYS^S^_cSdMd@e0f+fgh@ S&**_!=>@  8@ @ oofjj)@ 
	(@ @r1   rN   c                      e Zd Zdddej                  dej                  g ej                  e   f   dej                  g ej                  e
e
f   f   dej                  ej                  g e
f      fdZdej                  e
   d	e
fd
Z	 dddddddddddd
dej                  e
   de
dej                  e
   dej                  ej                  e
ej                  f      dej                  ej                     dej                  ej                     dej                  ej                  eej                   e   ej"                  e   f      dej                  ej                  e
ej                  ej                  eej&                  e   f      f      dej                  ej                  e
ej                  f      dej                  e   dedej                  ej                     d	ej,                  fdZe	 dddddddddddd
dej                  e
   de
dej                  e
   dej                  ej                  e
ej                  f      dej                  ej                     dej                  ej                     dej                  ej                  eej                   e   ej"                  e   f      dej                  ej                  e
ej                  ej                  eej&                  e   f      f      dej                  ej                  e
ej                  f      dej                  e   dedej                  ej                     d	ej"                  ej,                     fd       Zy)AsyncHttpClientNrO   rQ   rR   rS   rP   c                <    || _         || _        || _        || _        y r9   rU   rV   s        r#   rX   zAsyncHttpClient.__init__G  rY   r1   rZ   r   c                 `    |}| j                   || j                         }|t        d      |S r\   r]   r_   s      r#   r`   zAsyncHttpClient.get_base_urlT  ra   r1   r   rb   rf   rg   rc   rH   r?   rd   re   r(   r@   r&   r7   c                  K   | j                  |      }|
"|
j                  d      |
j                  d      n| j                         }t        |||
|      \  }}| j                  j                  |t        j                  j                  | d|      t        t        i | j                         |	|	ni |
|
j                  di       xs i ni             t        t        t        t        i ||ni |
|
j                  di       xs i ni |                        ||||t        t        t        |      |            nd |	       d {   }|
|
j                  dd      nd}t        |	      rT||kD  rOt!        j"                  t%        ||
             d {    | j                  ||||||||	|
|dz   |       d {   S |S 7 |7 /7 
wri   )r`   r   rR   rL   rQ   rr   rs   rt   ru   r	   r   rS   r
   r>   r   r5   asynciorv   r0   rw   s                     r#   rr   zAsyncHttpClient.request]  sC      $$X. */B/BCW/X/d  45""$ 	  0TVelpq	9 **22$$z^T:$%++-&-&97r SbRm?../CRHNBsu   )--3-?6R (7'B %4$7$78UWY$Z$`^`)+ !
"  $ 22GH]^cHdfj2klK 3 &
 &
P ETD_?..}a@ef(+W$mmNHg$VWWW!\\!%!##$3#aK *    s&
V Xs7   EG	G
AGG&G?G
 GG
Gc                 K   | j                  |      }|
"|
j                  d      |
j                  d      n| j                         }t        |||
|      \  }}| j                  j                  |t        j                  j                  | d|      t        t        i | j                         |	|	ni |
|
j                  di       ni             t        t        t        t        i ||ni |
|
j                  di       ni |                        ||||t        t        t        |      |            nd |	      4 d {   }| d d d       d {    y 7 7 # 1 d {  7  sw Y   y xY ww)Nrj   rk   rl   rm   rn   )r7   ro   ry   r{   s                    r#   rz   zAsyncHttpClient.stream  s    " $$X. */B/BCW/X/d  45""$ 	  0TVelpq	9$$++$$z^T:$%++-&-&97r M\Lg?../CRHmo   )--3-?6R (7'B %4$7$78UWY$Z)+ "&
"  $ 22GH]^cHdfj2klK , &
 &
L LO&
 &
 &
 &
 &
 &
 &
sH   EE7EE7E"E7E E7 E7"E4(E+)E40E7r9   )r|   r}   r~   r   AsyncClientrD   r   r   r   r   r   rX   r`   r   r   r   r   r   r   r   r   r   r   rr   r   rz   r   r1   r#   r   r   F  s    ?C) '') oob&//%*@&@A	)
 oob&++c3h*?&?@) //&//"c'":;)6??3+? C  &*S
 *.@D,0,0mqkoAE;?,0Sooc"S 	S
 //#&S CO <=S oofjj)S oofjj)S eV__U5KVMaMabgMh.h!ijS v{{3TSYS^S^_cSdMd@e0f+fghS S&**_!=>S  8S S oofjj)S 
Sj  &*@
 *.@D,0,0mqkoAE;?,0@ooc"@ 	@
 //#&@ CO <=@ oofjj)@ oofjj)@ eV__U5KVMaMabgMh.h!ij@ v{{3TSYS^S^_cSdMd@e0f+fgh@ S&**_!=>@  8@ @ oofjj)@ 
		enn	-@ @r1   r   ),r   email.utilsr   rH   r   r   rD   urllib.parsers   
contextlibr   r   r   r   filer   r   r	   query_encoderr
   r   r@   r   r+   r-   r)   Headersr   r   r$   r   r   r0   boolr5   r   r   r   r>   rG   TuplerL   rN   r   r   r1   r#   <module>r      s      	    :   9 . ' 8 +!  &( #& &6??5;Q &R*U^^ *c *e *(QENN Qt Q

kk#vvzz::;

//&**
%
 [[fjj!

//&**
%__^4 //&**
% __VZZ 	4\
//&**
%\ //&**
%\ __^4	\
 //&**
%\ \\&//&**-vvzz/JJK\&m m`m mr1   