U
    lHJe                     @   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edddZ	de
ej ddddZd	d
 Zdee
e ejdddZedddZee
ej d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)cfgr   c                 C   s   |st  | _n|| _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-agentZacceptcontent-type)formatr
   Zget_versionr   r   r   r   headers   s    zUAServiceClient.headersT)log_response_bodytimeoutr   c                 C   s   | d}|s|  }|ddkr>|r>tj|tjdd}t	t
| j| j|}| |}	|	rf|	S |rdd t| D }
|dt|
 7 }|d k	r|n| j}tj||||||d	S )
N/r   r   )clszutf-8c                 S   s   i | ]\}}|d k	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	   ZDatetimeAwareJSONEncoderencode	posixpathjoingetattrr   r   _get_fake_responsessorteditemsr   url_timeoutr   Zreadurl)r   pathr%   r   r&   Zquery_paramsr   r   r$   Zfake_responseZfiltered_paramsZtimeout_to_user   r   r   request_url%   s2    



zUAServiceClient.request_url)r$   c                 C   sf   | j dk	r| j |g S | jjd}|s2i | _ n&tj|sFi | _ 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.
        NZserviceclient_url_responses)_response_overlayr(   r   Zfeaturesosr3   existsr)   loadsr   Z	load_file)r   r$   Zresponse_overlay_pathr   r   r   _get_response_overlayM   s    
z%UAServiceClient._get_response_overlay)r$   r   c                 C   s   |  |}|sdS t|dkr(|d }n
|d}i }g }|d }t|trR|}nt|tr`|}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)Z	sort_keys)r<   r   Zbody	json_dict	json_list)r9   lenpop
isinstancedictlistr   HTTPResponser(   r)   r*   )r   r$   Z	responsesr;   r=   r>   Zrespr   r   r   r/   o   s(    





z#UAServiceClient._get_fake_responses)N)NNNNTN)__name__
__module____qualname__r2   r5   propertyabcabstractmethodstrr   r   r   r   r   r   boolintr   rD   r4   r9   r/   r   r   r   r   r      s(   
      ("r   )	metaclass)rI   r)   r6   r,   typingr   r   r   Zurllib.parser   Zuaclientr   r   r   r	   r
   ABCMetar   r   r   r   r   <module>   s   