
    f                         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e j                         Zy)    N)AnyDictOptional)	urlencode)confighttpsystemutilversionc                       e Zd ZdZdZeej                  defd              Z	dde
ej                     ddfdZd Z	 	 	 	 	 	 dded	e
e   dej$                  fd
ZdefdZdede
ej$                     fdZy)UAServiceClient   Nreturnc                      y)z@String in subclasses, the UAConfig attribute containing base urlN selfs    =/usr/lib/python3/dist-packages/uaclient/http/serviceclient.pycfg_url_base_attrz!UAServiceClient.cfg_url_base_attr   s     	    cfgc                 J    |st        j                         | _        y || _        y N)r   UAConfigr   )r   r   s     r   __init__zUAServiceClient.__init__   s    (DHDHr   c                 P    dj                  t        j                               dddS )NzUA-Client/{}application/json)z
user-agentacceptcontent-type)formatr   get_versionr   s    r   headerszUAServiceClient.headers   s)    (//0C0C0EF(.
 	
r   log_response_bodytimeoutc                 T   |j                  d      }|s| j                         }|j                  d      dk(  r6|r4t        j                  |t
        j                        j                  d      }t        j                  t        | j                  | j                        |      }| j                  |      }	|	r|	S |r@t        |j                               D 
ci c]  \  }
}|	|
| }}
}|dt!        |      z   z  }||n| j"                  }t%        j&                  ||||||      S c c}}
w )N/r   r   )clszutf-8?)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kvfiltered_paramstimeout_to_uses                 r   request_urlzUAServiceClient.request_url%   s    {{3llnG;;~&*<<::d(E(EFMMD nnWTXXt/E/EFM005   "((:(:(<!=A1O  3?333C$+$7T=M=M||"/
 	
s   
D$ D$r)   c                    | j                   | j                   j                  |g       S | j                  j                  j                  d      }|si | _         nTt        j
                  j                  |      si | _         n-t        j                  t        j                  |            | _         | j                   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.
        serviceclient_url_responses)_response_overlayr-   r   featuresosr:   existsr.   loadsr	   	load_file)r   r)   response_overlay_paths      r   _get_response_overlayz%UAServiceClient._get_response_overlayM   s    $ !!-))--c266 $ 1 1 5 5)!
 %%'D" 56%'D"%)ZZ  !67&D" %%))#r22r   c                 Z   | j                  |      }|syt        |      dk(  r|d   }n|j                  d      }i }g }|d   }t        |t              r|}nt        |t
              r|}t        j                  |d   |j                  di       t        j                  |d   d      ||	      S )
z7Return response if faked for this URL in uaclient.conf.N   r   responsecoder"   T)	sort_keys)rO   r"   body	json_dict	json_list)rK   lenpop
isinstancedictlistr   HTTPResponser-   r.   r/   )r   r)   	responsesrN   rR   rS   resps          r   r5   z#UAServiceClient._get_fake_responseso   s    ..s3	y>Q |H !}}Q'H		
#dD!Id#I  &!LLB/HZ0DA
 	
r   r   )NNNNTN)__name__
__module____qualname__r8   rD   propertyabcabstractmethodstrr   r   r   r   r   r"   boolintr   rY   rA   rK   r5   r   r   r   r   r      s    K 3   HV__5  
 "&!%&
  &
 #&
 
		&
P 3  3D
s 
x8I8I/J 
r   r   )	metaclass)r`   r.   rF   r2   typingr   r   r   urllib.parser   uaclientr   r   r	   r
   r   ABCMetar   r   r   r   <module>rj      s2    
  	  & & " 8 8
 
r   