o
    h=                  7   @   s  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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 ddlmZ ddlmZ d|dd	Zd
edefddZd}dejdedefddZdededeje dej ej!eef  def
ddZ"d}dejdeddfddZ#dededeje dej ej!eef  ddf
ddZ$d
eddfddZ%ej!ej!eef df Z&ej!e&df Z'ej(eej)ee'e&f f Z*de*defd d!Z+	d}d"ed#ej,eej-f d$eddfd%d&Z.d
ed'ej/ddfd(d)Z0d*ej1d+ej)ej2ej3f d,ej-dej-fd-d.Z4d*ej1d+ej)ej2ej3f d,ej-dej-fd/d0Z5d*ej1d+ej)ej2ej3f d,ej-ddfd1d2Z6ej7dd3ej8d4ed5ej9d6d7d8ed9d:ej9d;d<d=eefd>d?d@ej9dAdBdCedDd:ej9dEdFdGeefd>dHd@ej9dIdJdKeej:dLdMfd>dNd@ej9dOdPdQee4dRdSej9dTdUdeefd>dVd@ej9dWdXeefd>dYd@ej9dZd[eefd\e5d]d^ej9d_d`eddadbej9dcdde;dedfdbej9dgdhd>ddidjej9dkdld>d>dmdjej9dnded>ddodpej9dqe:drdsd:ej9dtdued>ddvdpej9dwd>d>de6dxdyd4ed8ed=ej ej!eef  dCedGej ej!eef  dKej ej!eej:f  dQedej ej!eef  dXej ej!eef  d[ejej!eef  d`edde;dhedleded'ejej/ d$eddf$dzd{Z<dS )~    N   )Client)RequestError)Response)codesreturnc                  C   sT  t j } | jddd |   | jddd |   | jddd |   t jjjddd	}|jd
dddd |d |dd |dd |dd |dd |dd |dd |dd |dd |dd |d d! |d"d# |d$d% |d&d' |d(d) |d*d+ |d,d- |d.d/ | | d S )0Nz[bold]HTTPX :butterfly:center)justifyzA next generation HTTP client.z7Usage: [bold]httpx[/bold] [cyan]<URL> [OPTIONS][/cyan] leftr   T)paddingpad_edge	Parameterbold)no_wrapr	   styleDescriptionz-m, --method [cyan]METHODz{Request method, such as GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD.
[Default: GET, or POST if a request body is included]z#-p, --params [cyan]<NAME VALUE> .../Query parameters to include in the request URL.z-c, --content [cyan]TEXT,Byte content to include in the request body.z!-d, --data [cyan]<NAME VALUE> ...)Form data to include in the request body.z%-f, --files [cyan]<NAME FILENAME> ...*Form files to include in the request body.z-j, --json [cyan]TEXT)JSON data to include in the request body.z$-h, --headers [cyan]<NAME VALUE> .../Include additional HTTP headers in the request.z --cookies [cyan]<NAME VALUE> ..."Cookies to include in the request.z--auth [cyan]<USER PASS>Username and password to include in the request. Specify '-' for the password to use a password prompt. Note that using --verbose/-v will expose the Authorization header, including the password encoding in a trivially reversible format.z--proxies [cyan]URLISend the request via a proxy. Should be the URL giving the proxy address.z--timeout [cyan]FLOATzTimeout value to use for network operations, such as establishing the connection, reading some data, etc... [Default: 5.0]--follow-redirectsAutomatically follow redirects.--no-verifyDisable SSL verification.--http2@Send the request using HTTP/2, if the remote server supports it.z--download [cyan]FILE?Save the response content as a file, rather than displaying it.z-v, --verbosez1Verbose output. Show request as well as response.--helpShow this message and exit.)	richconsoleConsoleprinttableTablegrid
add_columnadd_row)r&   r)    r.   A/var/www/html/ai/venv/lib/python3.10/site-packages/httpx/_main.py
print_help   s|   

r0   responsec                 C   s^   | j d}|d ur-|d\}}}ztttj|	 j
W S  tjjy,   Y dS w dS )NzContent-Type; )headersget	partitiontypingcaststrpygmentslexersget_lexer_for_mimetypestripnameutilClassNotFound)r1   content_type	mime_type_r.   r.   r/   get_lexer_for_responseb   s   rD   Frequesthttp2c                    sh    rdnd} fdd| j D }| jd}| jjd}| d| d| gdd |D  }d|S )	NzHTTP/2zHTTP/1.1c                    s$   g | ]\}} r|  n||fqS r.   )lower.0r>   valuerF   r.   r/   
<listcomp>q   s    z*format_request_headers.<locals>.<listcomp>ascii c                 S   *   g | ]\}}| d  d| d  qS rM   : decoderH   r.   r.   r/   rL   v       
)r4   methodrS   urltargetjoin)rE   rF   versionr4   rV   rX   linesr.   rK   r/   format_request_headerso   s   

r\   http_versionstatusreason_phraser4   c                 C   sT   |  d}|d u rt|n| d}| d| d| gdd |D  }d|S )NrM   rN   c                 S   rO   rP   rR   rH   r.   r.   r/   rL      rT   z+format_response_headers.<locals>.<listcomp>rU   )rS   r   get_reason_phraserY   )r]   r^   r_   r4   rZ   reasonr[   r.   r.   r/   format_response_headers|   s   


rb   c                 C   sV   t j }t| |d}t jj|dddd}|| t jjddddd}|| d S )NrK   http	ansi_darkTtheme	word_wrapr3   )r%   r&   r'   r\   syntaxSyntaxr(   )rE   rF   r&   	http_textrh   r.   r.   r/   print_request_headers   s   

rk   c                 C   sX   t j }t| |||}t jj|dddd}|| t jjddddd}|| d S )Nrc   rd   Tre   r3   )r%   r&   r'   rb   rh   ri   r(   )r]   r^   r_   r4   r&   rj   rh   r.   r.   r/   print_response_headers   s   

rl   c                 C   s   t j }t| }|r?| dkr+z|  }tj|dd}W n ty*   | j}Y nw | j}t j	j
||ddd}|| d S |dt| j d d S )	Njson   )indentrd   Tre   <z bytes of binary data>)r%   r&   r'   rD   rG   rm   dumps
ValueErrortextrh   ri   r(   lencontent)r1   r&   
lexer_namedatars   rh   r.   r.   r/   print_response   s   

rx   .certc              
   C   s   g }|   D ]a\}}t|ttfr\|d| d |D ]>}|dv r7|D ]}|d|d  d|d  q$qt|trRt|dkrR|d|d  d|d  q|d| qq|d| d| qd	|S )
Nz*   :)subjectissuerz*     r   rQ   r      rU   )items
isinstancelisttupleappendrt   rY   )ry   r[   keyrJ   itemsub_itemr.   r.   r/   format_certificate   s     	
r   r>   infoverbosec                 C   s  t j }| dkr|r|d }|d| d S | dkr9|r9|d }|d}|d|d d	|d
   d S | dkr|r|d }|d}| }| }	| }
| }|d|d|	d  |d| |
r}|d |t	|
 d S d S | dkr|r|d }t
|dd d S | dkr|r|d }t
|dd d S | dkr|d \}}}}t|||| d S | dkr|d \}}d}d }t|||| d S d S )Nzconnection.connect_tcp.startedhostz* Connecting to zconnection.connect_tcp.completereturn_valueserver_addrz* Connected to r   z	 on port r   zconnection.start_tls.complete
ssl_objectz* SSL established using z / z* Selected ALPN protocol: z* Server certificate:z#http11.send_request_headers.startedrE   FrK   z"http2.send_request_headers.startedTz(http11.receive_response_headers.completez'http2.receive_response_headers.completes   HTTP/2)r%   r&   r'   r(   get_extra_inforZ   ciphergetpeercertselected_alpn_protocolr   rk   rl   )r>   r   r   r&   r   streamr   r   rZ   r   server_certalpnrE   r]   r^   r_   r4   r.   r.   r/   trace   sH   

"

r   downloadc                 C   s   t j }|  | jd}t jddt jjd dt j	 t j
 5}dt j|j }|j|t|p6d|d ud}|  D ]}|| |j|| jd qAW d    d S 1 s\w   Y  d S )	NzContent-Lengthz([progress.description]{task.description}z-[progress.percentage]{task.percentage:>3.0f}%)	bar_widthzDownloading [bold]r   )totalstart)	completed)r%   r&   r'   r(   r4   r5   progressProgress	BarColumnDownloadColumnTransferSpeedColumnmarkupescaper>   add_taskint
iter_byteswriteupdatenum_bytes_downloaded)r1   r   r&   content_lengthr   descriptiondownload_taskchunkr.   r.   r/   download_response   s,   


"r   ctxparamrJ   c                 C   s4   |d u rd S zt |W S  t jy   tdw )NzNot valid JSON)rm   loadsJSONDecodeErrorclickBadParameterr   r   rJ   r.   r.   r/   validate_json  s   
r   c                 C   s2   |dkrd S |\}}|dkrt jddd}||fS )NNN-PasswordT)
hide_input)r   prompt)r   r   rJ   usernamepasswordr.   r.   r/   validate_auth  s   r   c                 C   s    |r| j rd S t  |   d S )N)resilient_parsingr0   exitr   r.   r.   r/   handle_help(  s   
r   )add_help_optionrW   )typez--methodz-mrV   z{Request method, such as GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD. [Default: GET, or POST if a request body is included])r   helpz--paramsz-pparamsTr   )r   multipler   z	--contentz-cru   r   z--dataz-drw   r   z--filesz-ffilesrb)moder   z--jsonz-jrm   r   )r   callbackr   z	--headersz-hr   z	--cookiescookiesr   z--authauthr   r   )r   defaultr   r   z	--proxiesproxiesr   )r   r   r   z	--timeouttimeoutg      @r   r   follow_redirectsr   )is_flagr   r   r   verifyr   r    r!   )r   r   r   r   z
--downloadwbr"   z	--verbosez-vz*Verbose. Show request as well as response.r#   r$   )r   is_eagerexpose_valuer   r   c                 C   sJ  |s|s
|s
|s
|rdnd}z]t |
|||dK}|j|| t||t||||t||	|dtjt|did}|durAt|| n|  |j	rLt
| W d   n1 sVw   Y  W d   n1 sew   Y  W n* ty } ztj }|dt|j d	|  td
 W Y d}~nd}~ww t|jrd dS d
 dS )zU
    An HTTP command line client.
    Sends a request and displays the response.
    POSTGET)r   r   r   rF   r   )r   )
r   ru   rw   r   rm   r4   r   r   r   
extensionsNz[red]z[/red]: r   r   )r   r   r   dict	functoolspartialr   r   readru   rx   r   r%   r&   r'   r(   r   __name__sysr   
is_success)rW   rV   r   ru   rw   r   rm   r4   r   r   r   r   r   r   rF   r   r   clientr1   excr&   r.   r.   r/   main4  sV    #
r   )r   N)F)=r   rm   r   r7   r   httpcorepygments.lexersr:   pygments.utilrich.consoler%   rich.markuprich.progressrich.syntax
rich.table_clientr   _exceptionsr   _modelsr   _status_codesr   r0   r9   rD   Requestboolr\   bytesr   OptionalListTuplerb   rk   rl   rx   _PCTRTT_PCTRTTTDictUnion_PeerCertRetDictTyper   MappingAnyr   BinaryIOr   ContextOptionr   r   r   r   commandargumentoptionFilefloatr   r.   r.   r.   r/   <module>   s   
L
	

'





	

4