o
    ÙKÕiê  ã                   @   st   d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZmZmZmZmZ G dd„ de jd�ZdS )é    N)ÚAnyÚDictÚOptional)Ú	urlencode)ÚconfigÚhttpÚsystemÚutilÚversionc                   @   sž   e Zd ZdZdZeejdefdd„ƒƒZ	dde
ej ddfdd„Zd	d
„ Z						ddede
e dejfdd„Zdefdd„Zdede
ej fdd„ZdS )ÚUAServiceClienté   NÚreturnc                 C   s   dS )z@String in subclasses, the UAConfig attribute containing base urlN© ©Úselfr   r   ú=/usr/lib/python3/dist-packages/uaclient/http/serviceclient.pyÚcfg_url_base_attr   s   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   
zUAServiceClient.__init__c                 C   s   d  t ¡ ¡dddœS )NzUA-Client/{}úapplication/json)z
user-agentÚacceptúcontent-type)Úformatr
   Úget_versionr   r   r   r   Úheaders   s   ýzUAServiceClient.headersTÚlog_response_bodyÚtimeoutc                 C   s¸   |  d¡}|s|  ¡ }| d¡dkr|rtj|tjd� d¡}t 	t
| j| jƒ|¡}|  |¡}	|	r3|	S |rHdd„ t| ¡ ƒD ƒ}
|dt|
ƒ 7 }|d urN|n| j}tj||||||d	�S )
Nú/r   r   )Úclszutf-8c                 S   s   i | ]\}}|d ur||“qS r   r   )Ú.0ÚkÚvr   r   r   Ú
<dictcomp>>   s    z/UAServiceClient.request_url.<locals>.<dictcomp>ú?)ÚurlÚdatar   Úmethodr   r   )Úlstripr   ÚgetÚjsonÚdumpsr	   ÚDatetimeAwareJSONEncoderÚencodeÚ	posixpathÚjoinÚgetattrr   r   Ú_get_fake_responsesÚsortedÚitemsr   Úurl_timeoutr   Úreadurl)r   Úpathr'   r   r(   Úquery_paramsr   r   r&   Úfake_responseÚfiltered_paramsÚtimeout_to_user   r   r   Úrequest_url%   s2   

ÿ

ÿúzUAServiceClient.request_urlr&   c                 C   sf   | j dur| j  |g ¡S | jj d¡}|si | _ ntj |¡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_overlayr*   r   ÚfeaturesÚosr7   Úexistsr+   Úloadsr   Ú	load_file)r   r&   Úresponse_overlay_pathr   r   r   Ú_get_response_overlayM   s   
ÿÿz%UAServiceClient._get_response_overlayc                 C   sŽ   |   |¡}|s	dS t|ƒdkr|d }n| d¡}i }g }|d }t|tƒr)|}nt|tƒr0|}tj|d | di ¡t	j
|d dd�||d	�S )
z7Return response if faked for this URL in uaclient.conf.Né   r   ÚresponseÚcoder   T)Ú	sort_keys)rH   r   ÚbodyÚ	json_dictÚ	json_list)rE   ÚlenÚpopÚ
isinstanceÚdictÚlistr   ÚHTTPResponser*   r+   r,   )r   r&   Ú	responsesrG   rK   rL   Úrespr   r   r   r2   o   s(   





ûz#UAServiceClient._get_fake_responsesr   )NNNNTN)Ú__name__Ú
__module__Ú__qualname__r5   r>   ÚpropertyÚabcÚabstractmethodÚstrr   r   r   r   r   r   ÚboolÚintr   rR   r<   rE   r2   r   r   r   r   r      s.    
øùø	
÷("r   )Ú	metaclass)rY   r+   r@   r/   Útypingr   r   r   Úurllib.parser   Úuaclientr   r   r   r	   r
   ÚABCMetar   r   r   r   r   Ú<module>   s    