File manager - Edit - /usr/lib/python3/dist-packages/uaclient/__pycache__/serviceclient.cpython-310.pyc
Back
o |Cvd� � @ 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 ZdZdZeejdee fdd���Z eejdefdd���Zdde ej ddfd d �Zdd� Z ddede e fdd�Zdefdd�Zdedee eeef e eeef f fdd�ZdS )�UAServiceClient� N�returnc C � 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 � zUAServiceClient.api_error_clsc C r )z@String in subclasses, the UAConfig attribute containing base urlNr r r r r �cfg_url_base_attr r z!UAServiceClient.cfg_url_base_attr�cfgc C s |s t �� | _d S || _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-agent�accept�content-type)�formatr �get_versionr r r r �headers% s �zUAServiceClient.headersT�log_response_body�timeoutc C sp |� d�}|s| �� }|�d�dkr|rtj|tjd��d�}tt | j | j�|�}| �|�\} } | r6| | fS |rKdd� t |�� �D �}|dt|� 7 }|d urQ|n| j}ztj||||||d �\} }W | |fS tjy� } zDd }t|d �rz|j}nt|d�r�|�� �d�}|r�z tj|tjd�}W n ty� d }Y nw |r�| �||��tj|t |dd �||d ��d }~ww )N�/r r )�clszutf-8c S s i | ]\}}|d ur||�qS r r )�.0�k�vr r r � <dictcomp>C s z/UAServiceClient.request_url.<locals>.<dictcomp>�?)�url�datar# �methodr% r$ �body�read�code�r2 r# r- )�lstripr# �get�json�dumpsr �DatetimeAwareJSONEncoder�encode�urljoin�getattrr r �_get_fake_responses�sorted�itemsr �url_timeout�readurlr �URLError�hasattrr0 r1 �decode�loads�DatetimeAwareJSONDecoder� ValueErrorr r �UrlError)r �pathr. r# r/ �query_paramsr$ r% r- � fake_response�fake_headers�filtered_params�timeout_to_use�response�er0 � error_detailsr r r �request_url, s` � ��� �����zUAServiceClient.request_urlr- c C sf | j dur| 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_overlayr5 r �features�osrH �existsr6 rD r � load_file)r r- �response_overlay_pathr r r �_get_response_overlaye s ��z%UAServiceClient._get_response_overlayc C s� | � |�}|sdi fS t|�dkr|d }n|�d�}|d dkr+|d |�di �fS t�|d �}tj||d |�di �|d�}|�d �d krQtj||d d��|�)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� r r2 �� rN r# r3 �type�contract)�error_response) rY �len�popr5 r rA r rG �ContractAPIError)r r- � responsesrN rO � url_exceptionr r r r<