B
    `                 @   s   d Z ddlmZmZmZ eZddlZddlZyddl	m
Z
 W n  ek
r\   ddlm
Z
 Y nX yddlmZmZmZ W n( ek
r   ddlmZmZmZ Y nX ddlmZmZmZ ddlmZmZ G d	d
 d
ZG dd dZG dd deZdS )zq
Primitive replacement for requests to avoid extra dependency.
Avoids use of urllib2 due to lack of SNI support.
    )absolute_importdivisionprint_functionN)	urlencode)urlparse
urlunparseparse_qs   )ApplicationErrorSubprocessErrordisplay)CommonConfigrun_commandc               @   s>   e Zd ZdZdddZdd Zdd	 Zdd
dZdddZdS )
HttpClientzMake HTTP requests via curl.FNc             C   s(   || _ || _|| _|| _d| _d| _dS )zb
        :type args: CommonConfig
        :type always: bool
        :type insecure: bool
        N)argsalwaysinsecureproxyusernamepassword)selfr   r   r   r    r   J/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/http.py__init__&   s    zHttpClient.__init__c             C   s   |  d|S )z=
        :type url: str
        :rtype: HttpResponse
        GET)request)r   urlr   r   r   get4   s    zHttpClient.getc             C   s   |  d|S )z=
        :type url: str
        :rtype: HttpResponse
        DELETE)r   )r   r   r   r   r   delete;   s    zHttpClient.deletec             C   s   |  d|||S )z
        :type url: str
        :type data: str | None
        :type headers: dict[str, str] | None
        :rtype: HttpResponse
        PUT)r   )r   r   dataheadersr   r   r   putB   s    zHttpClient.putc          
   C   s  ddddd|g}| j r |dg7 }|dkr,i }d|d	< | jrv| jrhtj| j |d
d| j| jf g7 }n|d
| jg7 }x(| D ]}|dd||| f g7 }qW |dk	r|d|g7 }| jr|d| jg7 }||g7 }d}d}d}	d}
d}x|d7 }y t| j	|d| j
ddd }P W q tk
rf } z6|j|
krT||k rTtd|  t|	 w W dd}~X Y qX qW | j	jr| j
st||ddS |dd\}}|d}|d }|d}t|d }t||||S )z
        :type method: str
        :type url: str
        :type data: str | None
        :type headers: dict[str, str] | None
        :rtype: HttpResponse
        Zcurlz-sz-Sz-iz-Xz
--insecureN ZExpectz-uz%s:%sz-Hz%s: %sz-dz-xr      )   r	   T   )capturer   Zcmd_verbosityz%s   z

z
 )r   r   r   r   Z	sensitiveaddkeysr   r   r   r   r   statuswarningtimesleepexplainHttpResponsesplitint)r   methodr   r!   r"   cmdheaderattemptsZmax_attemptsZsleep_secondsZretry_on_statusstdoutexbodyresponse_headers
first_linehttp_responsestatus_coder   r   r   r   K   sT    




zHttpClient.request)FFN)NN)NN)	__name__
__module____qualname____doc__r   r   r   r#   r   r   r   r   r   r   $   s   

	r   c               @   s    e Zd ZdZdd Zdd ZdS )r2   zHTTP response from curl.c             C   s   || _ || _|| _|| _dS )zu
        :type method: str
        :type url: str
        :type status_code: int
        :type response: str
        N)r5   r   r?   response)r   r5   r   r?   rD   r   r   r   r      s    zHttpResponse.__init__c          	   C   sB   yt | jS  tk
r<   t| jd| j| j| jf Y nX dS )z
        :rtype: any
        z*Cannot parse response to %s %s as JSON:
%sN)jsonloadsrD   
ValueError	HttpErrorr?   r5   r   )r   r   r   r   rE      s    zHttpResponse.jsonN)r@   rA   rB   rC   r   rE   r   r   r   r   r2      s   r2   c                   s    e Zd ZdZ fddZ  ZS )rH   zHTTP response as an error.c                s"   t t| d||f  || _dS )z>
        :type status: int
        :type message: str
        z%s: %sN)superrH   r   r-   )r   r-   message)	__class__r   r   r      s    zHttpError.__init__)r@   rA   rB   rC   r   __classcell__r   r   )rK   r   rH      s   rH   )rC   
__future__r   r   r   type__metaclass__rE   r/   urllibr   ImportErrorurllib.parser   r   r   utilr
   r   r   Zutil_commonr   r   r   r2   rH   r   r   r   r   <module>   s"   q