| Current Path : /usr/lib/python3/dist-packages/uaclient/__pycache__/ |
| Current File : //usr/lib/python3/dist-packages/uaclient/__pycache__/serviceclient.cpython-38.pyc |
U
8�-d� � @ s� d dl Z d dlZd dlZd dlmZ d dlmZmZm Z m
Z
mZ d dlm
Z
d dlmZ d dlmZmZmZmZmZ G dd� de jd �ZdS )
� N)�join)�Any�Dict�Optional�Tuple�Type)�error)� urlencode)�config�
exceptions�system�util�versionc @ s� e Zd ZdZdZeejee d�dd���Z
eejed�dd���Zde
ej dd�d d
�Zdd� Zdee
e d�dd�Zed�dd�Zeee
eeef e
eeef f d�dd�ZdS )�UAServiceClient� N)�returnc C s dS )z1Set in subclasses to the type of API error raisedN� ��selfr r �8/usr/lib/python3/dist-packages/uaclient/serviceclient.py�
api_error_cls s zUAServiceClient.api_error_clsc C s dS )z@String in subclasses, the UAConfig attribute containing base urlNr r r r r �cfg_url_base_attr s z!UAServiceClient.cfg_url_base_attr)�cfgr c C s |st �� | _n|| _d S �N)r
�UAConfigr )r r r r r �__init__ s zUAServiceClient.__init__c C s d� t�� �ddd�S )NzUA-Client/{}�application/json)z
user-agentZaccept�content-type)�formatr Zget_versionr r r r �headers% s �zUAServiceClient.headersT)�log_response_body�timeoutc C s� |� d�}|s| �� }|�d�dkr>|r>tj|tjd��d�}tt | j
| j�|�}| �|�\} }
| rl| |
fS |r�dd� t
|�� �D �}|dt|� 7 }|d k r�|n| j}ztj||||||d �\}
}W n� tjk
�r~ } z�d }t|d
�r�|j}nt|d��r|�� �d�}|�rTztj|tjd�}W n tk
�r@ d }Y nX |�rT| �||��tj|t |dd �||d
��W 5 d }~X Y nX |
|fS )N�/r r )�clszutf-8c S s i | ]\}}|d k r||�qS r r )�.0�k�vr r r �
<dictcomp>C s z/UAServiceClient.request_url.<locals>.<dictcomp>�?)�url�datar �methodr! r �body�read�code�r. r r) )�lstripr �get�json�dumpsr
ZDatetimeAwareJSONEncoder�encode�urljoin�getattrr r �_get_fake_responses�sorted�itemsr �url_timeoutZreadurlr �URLError�hasattrr, r- �decode�loadsZDatetimeAwareJSONDecoder�
ValueErrorr r �UrlError)r �pathr* r r+ Zquery_paramsr r! r) Z
fake_responseZfake_headersZfiltered_paramsZtimeout_to_use�response�er, Z
error_detailsr r r �request_url, sb
�
��
�
�zUAServiceClient.request_url)r) c C sf | j dk 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_overlayr1 r Zfeatures�osrA �existsr2 r>