U
    dd=                  &   @   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)paddingZpad_edge	ParameterZbold)Z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tableZTableZgridZ
add_columnZadd_row)r#   r&    r'   //tmp/pip-unpacked-wheel-fkuvgui5/httpx/_main.py
print_help   s    
 
    r)   )responser   c                 C   s^   | j d}|d k	rZ|d\}}}ztttj|	 j
W S  tjjk
rX   Y nX dS )NzContent-Type; )headersget	partitiontypingcaststrpygmentsZlexersZget_lexer_for_mimetypestripnameutilZClassNotFound)r*   content_typeZ	mime_type_r'   r'   r(   get_lexer_for_responseb   s     r9   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.0r5   valuer;   r'   r(   
<listcomp>q   s    z*format_request_headers.<locals>.<listcomp>ascii c                 S   s*   g | ]"\}}| d  d| d  qS rB   : decoder=   r'   r'   r(   rA   v   s    
)r-   methodrG   urltargetjoin)r:   r;   versionr-   rI   rK   linesr'   r@   r(   format_request_headerso   s    
rO   )http_versionstatusreason_phraser-   r   c                 C   sT   |  d}|d krt|n| d}| d| d| gdd |D  }d|S )NrB   rC   c                 S   s*   g | ]"\}}| d  d| d  qS rD   rF   r=   r'   r'   r(   rA      s    z+format_response_headers.<locals>.<listcomp>rH   )rG   r   Zget_reason_phraserL   )rP   rQ   rR   r-   rM   reasonrN   r'   r'   r(   format_response_headers|   s    
rT   c                 C   sV   t j }t| |d}t jj|dddd}|| t jjddddd}|| d S )Nr@   http	ansi_darkTZthemeZ	word_wrapr,   )r"   r#   r$   rO   syntaxSyntaxr%   )r:   r;   r#   	http_textrX   r'   r'   r(   print_request_headers   s    

r[   c                 C   sX   t j }t| |||}t jj|dddd}|| t jjddddd}|| d S )NrU   rV   TrW   r,   )r"   r#   r$   rT   rX   rY   r%   )rP   rQ   rR   r-   r#   rZ   rX   r'   r'   r(   print_response_headers   s    

r\   c                 C   s   t j }t| }|r| dkrZz|  }tj|dd}W q` tk
rV   | j}Y q`X n| j}t j	j
||ddd}|| n|dt| j d d S )	Njson   )indentrV   TrW   <z bytes of binary data>)r"   r#   r$   r9   r<   r]   dumps
ValueErrortextrX   rY   r%   lencontent)r*   r#   Z
lexer_namedatarc   rX   r'   r'   r(   print_response   s    
rg   .)certr   c              
   C   s   g }|   D ]\}}t|ttfr|d| d |D ]|}|dk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   rE   r      rH   )items
isinstancelisttupleappendrd   rL   )rh   rN   keyr?   itemZsub_itemr'   r'   r(   format_certificate   s    " rs   )r5   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.startedr:   Fr@   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_inforM   ciphergetpeercertselected_alpn_protocolrs   r[   r\   )r5   rt   ru   r#   rv   streamrw   rx   rM   ry   Zserver_certZalpnr:   rP   rQ   rR   r-   r'   r'   r(   trace   sD    

"



r}   )r*   downloadr   c              	   C   s   t j }|  | jd}t jddt jjd dt j	 t j
 ^}dt j|j }|j|t|pld|d k	d}|  D ]}|| |j|| jd qW 5 Q R X d S )	NzContent-Lengthz([progress.description]{task.description}z-[progress.percentage]{task.percentage:>3.0f}%)Z	bar_widthzDownloading [bold]r   )totalstart)Z	completed)r"   r#   r$   r%   r-   r.   progressProgressZ	BarColumnZDownloadColumnZTransferSpeedColumnZmarkupescaper5   Zadd_taskintZ
iter_byteswriteupdateZnum_bytes_downloaded)r*   r~   r#   content_lengthr   descriptionZdownload_taskchunkr'   r'   r(   download_response   s(    


r   )ctxparamr?   r   c                 C   s>   |d krd S zt |W S  t jk
r8   tdY nX d S )NzNot valid JSON)r]   loadsJSONDecodeErrorclickZBadParameterr   r   r?   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   r?   usernamepasswordr'   r'   r(   validate_auth  s    r   c                 C   s    |r
| j rd S t  |   d S )N)Zresilient_parsingr)   exitr   r'   r'   r(   handle_help(  s    
r   )add_help_optionrJ   )typez--methodz-mrI   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-cre   r   z--dataz-drf   r   z--filesz-ffilesrb)moder   z--jsonz-jr]   r   )r   callbackr   z	--headersz-hr-   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   r;   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   )rJ   rI   r   re   rf   r   r]   r-   r   r   r   r   r   r   r;   r~   ru   r   c                 C   s  |s|s|s|s|rdnd}zt |
|||dx}|j|| t||t||||t||	|dtjt|did0}|dk	rt|| n|  |j	rt
| W 5 Q R X W 5 Q R X W nT tk
r } z4tj }|dt|j d	|  td
 W 5 d}~X Y nX 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   r;   r}   )ru   )
r   re   rf   r   r]   r-   r   r   r   
extensionsNz[red]z[/red]: r   r   )r   r|   rn   dict	functoolspartialr}   r   readre   rg   r   r"   r#   r$   r%   r   __name__sysr   Z
is_success)rJ   rI   r   re   rf   r   r]   r-   r   r   r   r   r   r   r;   r~   ru   clientr*   excr#   r'   r'   r(   main4  sH     # 
r   )F)F)F)=r   r]   r   r0   r   ZhttpcoreZpygments.lexersr3   Zpygments.utilZrich.consoler"   Zrich.markupZrich.progressZrich.syntaxZ
rich.tableZ_clientr   _exceptionsr   Z_modelsr   Z_status_codesr   r)   r2   r9   RequestboolrO   bytesr   OptionalListTuplerT   r[   r\   rg   Z_PCTRTTZ_PCTRTTTDictUnionZ_PeerCertRetDictTypers   MappingAnyr}   BinaryIOr   ContextOptionr   r   r   r   commandargumentoptionZFilefloatr   r'   r'   r'   r(   <module>   s  L
   '


	
