a
    |f=                  &   @   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d	Zeed
ddZdwejeedddZeeeje ej ej!eef  edddZ"dxejeddddZ#eeeje ej ej!eef  ddddZ$edd
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*e*edddZ+dyeej,eej-f edddd Z.eej/dd!d"d#Z0ej1ej)ej2ej3f ej-ej-d$d%d&Z4ej1ej)ej2ej3f ej-ej-d$d'd(Z5ej1ej)ej2ej3f ej-dd$d)d*Z6ej7dd+ej8d,ed-ej9d.d/d0ed1d2ej9d3d4d5eefd6d7d8ej9d9d:d;ed<d2ej9d=d>d?eefd6d@d8ej9dAdBdCeej:dDdEfd6dFd8ej9dGdHdIee4dJdKej9dLdMdNeefd6dOd8ej9dPdQeefd6dRd8ej9dSdTeefdUe5dVdWej9dXdYeddZd[ej9d\d]e;d^d_d[ej9d`dad6ddbdcej9ddded6d6dfdcej9dgdhed6ddidjej9dke:dldmd2ej9dndoed6ddpdjej9dqd6d6de6drdseeej ej!eef  eej ej!eef  ej ej!eej:f  eej ej!eef  ej ej!eef  ejej!eef  ee;eeeejej/ eddtdudvZ<dS )z    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.   ED:\Projects\storyit_web\backend\venv\Lib\site-packages\httpx/_main.py
print_help   s|    

r0   )responser   c                 C   s\   | j d}|d urX|d\}}}ztttj|	 j
W S  tjjyV   Y n0 dS )NzContent-Type; )headersget	partitiontypingcaststrpygmentslexersget_lexer_for_mimetypestripnameutilClassNotFound)r1   content_typeZ	mime_type_r.   r.   r/   get_lexer_for_responseb   s    rC   F)requesthttp2r   c                    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rE   r.   r/   
<listcomp>q   s   z*format_request_headers.<locals>.<listcomp>ascii c                 S   s*   g | ]"\}}| d  d| d  qS rL   : decoderG   r.   r.   r/   rK   v   s   
)r4   methodrQ   urltargetjoin)rD   rE   versionr4   rS   rU   linesr.   rJ   r/   format_request_headerso   s    
rY   )http_versionstatusreason_phraser4   r   c                 C   sT   |  d}|d u rt|n| d}| d| d| gdd |D  }d|S )NrL   rM   c                 S   s*   g | ]"\}}| d  d| d  qS rN   rP   rG   r.   r.   r/   rK      s   z+format_response_headers.<locals>.<listcomp>rR   )rQ   r   Zget_reason_phraserV   )rZ   r[   r\   r4   rW   reasonrX   r.   r.   r/   format_response_headers|   s    
r^   c                 C   sV   t j }t| |d}t jj|dddd}|| t jjddddd}|| d S )NrJ   http	ansi_darkTtheme	word_wrapr3   )r%   r&   r'   rY   syntaxSyntaxr(   )rD   rE   r&   	http_textrd   r.   r.   r/   print_request_headers   s    

rg   c                 C   sX   t j }t| |||}t jj|dddd}|| t jjddddd}|| d S )Nr_   r`   Tra   r3   )r%   r&   r'   r^   rd   re   r(   )rZ   r[   r\   r4   r&   rf   rd   r.   r.   r/   print_response_headers   s    

rh   c                 C   s   t j }t| }|r~| dkrXz|  }tj|dd}W q^ tyT   | j}Y q^0 n| j}t j	j
||ddd}|| n|dt| j d d S )	Njson   )indentr`   Tra   <z bytes of binary data>)r%   r&   r'   rC   rF   ri   dumps
ValueErrortextrd   re   r(   lencontent)r1   r&   
lexer_namedataro   rd   r.   r.   r/   print_response   s    
rt   .)certr   c              
   C   s   g }|   D ]\}}t|ttfr|d| d |D ]|}|dv rn|D ]"}|d|d  d|d  qHq8t|trt|dkr|d|d  d|d  q8|d| q8q|d| d| qd	|S )
Nz*   :)subjectZissuerz*     r   rO   r      rR   )items
isinstancelisttupleappendrp   rV   )ru   rX   keyrI   itemZsub_itemr.   r.   r/   format_certificate   s    " r   )r>   infoverboser   c                 C   s  t j }| dkr2|r2|d }|d| nh| dkrr|rr|d }|d}|d|d d	|d
   n(| dkr|r|d }|d}| }| }	| }
| }|d|d|	d  |d| |
r|d |t	|
 n| dkr|r|d }t
|dd n|| dkrD|rD|d }t
|dd nV| dkrn|d \}}}}t|||| n,| dkr|d \}}d}d }t|||| d S )Nzconnection.connect_tcp.startedhostz* Connecting to zconnection.connect_tcp.completeZ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.startedrD   FrJ   z"http2.send_request_headers.startedTz(http11.receive_response_headers.completez'http2.receive_response_headers.completes   HTTP/2)r%   r&   r'   r(   Zget_extra_inforW   ciphergetpeercertselected_alpn_protocolr   rg   rh   )r>   r   r   r&   r   streamr   r   rW   r   Zserver_certZalpnrD   rZ   r[   r\   r4   r.   r.   r/   trace   sD    

"



r   )r1   downloadr   c                 C   s   t j }|  | jd}t jddt jjd dt j	 t j
 h}dt j|j }|j|t|pld|d ud}|  D ]}|| |j|| jd qW d    n1 s0    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intZ
iter_byteswriteupdateZnum_bytes_downloaded)r1   r   r&   content_lengthr   descriptionZdownload_taskchunkr.   r.   r/   download_response   s(    


r   )ctxparamrI   r   c                 C   s<   |d u rd S zt |W S  t jy6   tdY n0 d S )NzNot valid JSON)ri   loadsJSONDecodeErrorclickZBadParameterr   r   rI   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-ZPasswordT)Z
hide_input)r   prompt)r   r   rI   usernamepasswordr.   r.   r/   validate_auth  s    r   c                 C   s    |r
| j rd S t  |   d S )N)Zresilient_parsingr0   exitr   r.   r.   r/   handle_help(  s    
r   )add_help_optionrT   )typez--methodz-mrS   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   Zmultipler   z	--contentz-crq   r   z--dataz-drs   r   z--filesz-ffilesrb)moder   z--jsonz-jri   r   )r   callbackr   z	--headersz-hr4   r   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    rE   r!   )r   r   r   r   z
--downloadwbr"   z	--verbosez-vz*Verbose. Show request as well as response.r#   r$   )r   Zis_eagerZexpose_valuer   r   )rT   rS   r   rq   rs   r   ri   r4   r   r   r   r   r   r   rE   r   r   r   c                 C   sH  |s|s|s|s|rdnd}zt |
|||d}|j|| t||t||||t||	|dtjt|did:}|durt|| n|  |j	rt
| W d   n1 s0    Y  W d   n1 s0    Y  W nV ty, } z<tj }|dt|j d	|  td
 W Y d}~n
d}~0 0 t|jr>dnd
 dS )zU
    An HTTP command line client.
    Sends a request and displays the response.
    POSTGET)r   r   r   rE   r   )r   )
r   rq   rs   r   ri   r4   r   r   r   
extensionsNz[red]z[/red]: r   r   )r   r   r{   dict	functoolspartialr   r   readrq   rt   r   r%   r&   r'   r(   r   __name__sysr   Z
is_success)rT   rS   r   rq   rs   r   ri   r4   r   r   r   r   r   r   rE   r   r   clientr1   excr&   r.   r.   r/   main4  sH     #H
 r   )F)F)F)=r   ri   r   r7   r   ZhttpcoreZpygments.lexersr:   Zpygments.utilZrich.consoler%   Zrich.markupZrich.progressZrich.syntaxZ
rich.tableZ_clientr   _exceptionsr   Z_modelsr   Z_status_codesr   r0   r9   rC   RequestboolrY   bytesr   OptionalListTupler^   rg   rh   rt   Z_PCTRTTZ_PCTRTTTDictUnionZ_PeerCertRetDictTyper   MappingAnyr   BinaryIOr   ContextOptionr   r   r   r   commandZargumentoptionFilefloatr   r.   r.   r.   r/   <module>   s  L
 '


	
