Your IP : 216.73.216.14


Current Path : /usr/lib/python3/dist-packages/uaclient/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/uaclient/__pycache__/serviceclient.cpython-38.pyc

U

8�-d��@s�ddlZddlZddlZddlmZddlmZmZm	Z	m
Z
mZddlm
Z
ddlmZddlmZmZmZmZmZGdd�dejd	�ZdS)
�N)�join)�Any�Dict�Optional�Tuple�Type)�error)�	urlencode)�config�
exceptions�system�util�versionc@s�eZdZdZdZeejee	d�dd���Z
eejed�dd���Zde
ejdd�d	d
�Zdd�Zdee
ed�dd�Zed�dd�Zeee
eeefe
eeeffd�dd�ZdS)�UAServiceClient�N)�returncCsdS)z1Set in subclasses to the type of API error raisedN���selfrr�8/usr/lib/python3/dist-packages/uaclient/serviceclient.py�
api_error_clsszUAServiceClient.api_error_clscCsdS)z@String in subclasses, the UAConfig attribute containing base urlNrrrrr�cfg_url_base_attrsz!UAServiceClient.cfg_url_base_attr)�cfgrcCs|st��|_n||_dS�N)r
�UAConfigr)rrrrr�__init__szUAServiceClient.__init__cCsd�t���ddd�S)NzUA-Client/{}�application/json)z
user-agentZaccept�content-type)�formatrZget_versionrrrr�headers%s�zUAServiceClient.headersT)�log_response_body�timeoutcCs�|�d�}|s|��}|�d�dkr>|r>tj|tjd��d�}tt	|j
|j�|�}|�|�\}	}
|	rl|	|
fS|r�dd�t
|���D�}|dt|�7}|dk	r�|n|j}ztj||||||d	�\}
}Wn�tjk
�r~}z�d}t|d
�r�|j}nt|d��r|���d�}|�rTztj|tjd�}Wntk
�r@d}YnX|�rT|�||��tj|t	|dd�||d
��W5d}~XYnX|
|fS)N�/rr)�clszutf-8cSsi|]\}}|dk	r||�qSrr)�.0�k�vrrr�
<dictcomp>Csz/UAServiceClient.request_url.<locals>.<dictcomp>�?)�url�datar�methodr!r �body�read�code�r.rr))�lstripr�get�json�dumpsr
ZDatetimeAwareJSONEncoder�encode�urljoin�getattrrr�_get_fake_responses�sorted�itemsr	�url_timeoutZreadurlr�URLError�hasattrr,r-�decode�loadsZDatetimeAwareJSONDecoder�
ValueErrorrr�UrlError)r�pathr*rr+Zquery_paramsr r!r)Z
fake_responseZfake_headersZfiltered_paramsZtimeout_to_use�response�er,Z
error_detailsrrr�request_url,sb

�
��
�


�zUAServiceClient.request_url)r)cCsf|jdk	r|j�|g�S|jj�d�}|s2i|_n&tj�|�sFi|_nt�t	�
|��|_|j�|g�S)a.Return a list of fake response dicts for a given URL.

        serviceclient_url_responses in uaclient.conf should be a path
        to a json file which contains a dictionary keyed by full URL path.
        Each value will be a list of dicts representing each faked response
        for the given URL.

            The response dict item will have a code: <HTTP_STATUS_CODE> and
               response: "some string of content".
            The JSON string below fakes the available_resources URL on the
            contract server:
            '{"https://contracts.canonical.com/v1/resources":                [{"code": 200, "response": {"key": "val1", "key2": "val2"}}]}'

        :return: List of dicts for each faked response matching the url, or
           and empty list when no matching url found.
        NZserviceclient_url_responses)�_response_overlayr1rZfeatures�osrA�existsr2r>rZ	load_file)rr)Zresponse_overlay_pathrrr�_get_response_overlayes
��z%UAServiceClient._get_response_overlay)r)rcCs�|�|�}|sdifSt|�dkr,|d}n
|�d�}|ddkrV|d|�di�fSt�|d�}tj||d|�di�|d�}|�d	�d
kr�tj||dd��n|�dS)a�Return response and headers if faked for this URL in uaclient.conf.

        :return: A tuple of response and header dicts if the URL has an overlay
            response defined. Return (None, {}) otherwise.

        :raises exceptions.URLError: When faked response "code" is != 200.
            URLError reason will be "response" value and any optional
            "headers" provided.
        N�rr.��rBrr/�typeZcontract)Zerror_response)	rH�len�popr1rr;rr@ZContractAPIError)rr)Z	responsesrBrCZ
url_exceptionrrrr7�s*



��z#UAServiceClient._get_fake_responses)N)NNNNTN)�__name__�
__module__�__qualname__r:rE�property�abc�abstractmethodr�	Exceptionr�strrrr
rrr�bool�intrDrHrrrr7rrrrrs0
��9#"�r)�	metaclass)rRr2rF�	posixpathrr5�typingrrrrrZurllibrZurllib.parser	Zuaclientr
rrr
r�ABCMetarrrrr�<module>s