HEX
Server: nginx/1.24.0
System: Linux ip-10-50-20-85 6.5.0-1022-aws #22~22.04.1-Ubuntu SMP Fri Jun 14 16:31:00 UTC 2024 x86_64
User: usefuloffices.org (1001)
PHP: 8.1.2-1ubuntu2.23
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/uaclient/__pycache__/serviceclient.cpython-310.pyc
o

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�eZdZdZdZeejdee	fdd���Z
eejdefdd���Zdde
ejddfd	d
�Zdd�Z					
	ddede
efdd�Zdefdd�Zdedee
eeefe
eeefffdd�ZdS)�UAServiceClient�N�returncC�dS)z1Set in subclasses to the type of API error raisedN���selfrr�8/usr/lib/python3/dist-packages/uaclient/serviceclient.py�
api_error_cls�zUAServiceClient.api_error_clscCr)z@String in subclasses, the UAConfig attribute containing base urlNrrrrr�cfg_url_base_attrrz!UAServiceClient.cfg_url_base_attr�cfgcCs|s	t��|_dS||_dS�N)r
�UAConfigr)rrrrr�__init__s
zUAServiceClient.__init__cCsd�t���ddd�S)NzUA-Client/{}�application/json)z
user-agent�accept�content-type)�formatr�get_versionrrrr�headers%s�zUAServiceClient.headersT�log_response_body�timeoutcCsp|�d�}|s|��}|�d�dkr|rtj|tjd��d�}tt	|j
|j�|�}|�|�\}	}
|	r6|	|
fS|rKdd�t
|���D�}|dt|�7}|durQ|n|j}ztj||||||d	�\}
}W|
|fStjy�}zDd}t|d
�rz|j}nt|d�r�|���d�}|r�z
tj|tjd�}Wnty�d}Ynw|r�|�||��tj|t	|dd�||d
��d}~ww)N�/r r)�clszutf-8cSsi|]\}}|dur||�qSrr)�.0�k�vrrr�
<dictcomp>Cs
��z/UAServiceClient.request_url.<locals>.<dictcomp>�?)�url�datar#�methodr%r$�body�read�code�r2r#r-)�lstripr#�get�json�dumpsr
�DatetimeAwareJSONEncoder�encode�urljoin�getattrrr�_get_fake_responses�sorted�itemsr	�url_timeout�readurlr�URLError�hasattrr0r1�decode�loads�DatetimeAwareJSONDecoder�
ValueErrorrr�UrlError)r�pathr.r#r/�query_paramsr$r%r-�
fake_response�fake_headers�filtered_params�timeout_to_use�response�er0�
error_detailsrrr�request_url,s`

�
���


�����zUAServiceClient.request_urlr-cCsf|jdur|j�|g�S|jj�d�}|si|_ntj�|�s#i|_n	t�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.
        N�serviceclient_url_responses)�_response_overlayr5r�features�osrH�existsr6rDr�	load_file)rr-�response_overlay_pathrrr�_get_response_overlayes
��z%UAServiceClient._get_response_overlaycCs�|�|�}|sdifSt|�dkr|d}n|�d�}|ddkr+|d|�di�fSt�|d�}tj||d|�di�|d�}|�d	�d
krQtj||dd��|�)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�rr2��rNr#r3�type�contract)�error_response)	rY�len�popr5rrArrG�ContractAPIError)rr-�	responsesrNrO�
url_exceptionrrrr<�s*



��z#UAServiceClient._get_fake_responsesr)NNNNTN)�__name__�
__module__�__qualname__r?rS�property�abc�abstractmethodr�	Exceptionr�strrrr
rrr#�bool�intrQrYrrrr<rrrrrs8
��
�9"�"�r)�	metaclass)rhr6rU�	posixpathrr:�typingrrrrr�urllibr�urllib.parser	�uaclientr
rrr
r�ABCMetarrrrr�<module>s