o !V1f@stddlZddlZddlZddlZddlmZmZmZddlm Z ddl m Z m Z m Z mZmZGdddejdZdS)N)AnyDictOptional) urlencode)confighttpsystemutilversionc@seZdZdZdZeejdefddZ dde e j ddfddZ d d Z     dd ed e edejfddZdefddZdede ejfddZdS)UAServiceClientNreturncCsdS)z@String in subclasses, the UAConfig attribute containing base urlNselfrr=/usr/lib/python3/dist-packages/uaclient/http/serviceclient.pycfg_url_base_attrsz!UAServiceClient.cfg_url_base_attrcfgcCs|s t|_dS||_dSN)rUAConfigr)rrrrr__init__s zUAServiceClient.__init__cCsdtdddS)Nz UA-Client/{}application/json)z user-agentaccept content-type)formatr get_versionrrrrheaderss zUAServiceClient.headersTlog_response_bodytimeoutc Cs|d}|s |}|ddkr|rtj|tjdd}t t |j |j |}| |} | r3| S|rHddt|D} |dt| 7}|durN|n|j} tj||||| |d S) N/rr)clszutf-8cSsi|] \}}|dur||qSrr).0kvrrr >sz/UAServiceClient.request_url..?)urldatarmethodrr)lstriprgetjsondumpsr DatetimeAwareJSONEncoderencode posixpathjoingetattrrr_get_fake_responsessorteditemsr url_timeoutrreadurl) rpathr'rr( query_paramsrrr& fake_responsefiltered_paramstimeout_to_userrr request_url%s2   zUAServiceClient.request_urlr&cCsf|jdur |j|gS|jjd}|si|_ntj|s#i|_n tt ||_|j|gS)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: 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. Nserviceclient_url_responses) _response_overlayr*rfeaturesosr7existsr+loadsr load_file)rr&response_overlay_pathrrr_get_response_overlayMs  z%UAServiceClient._get_response_overlaycCs||}|s dSt|dkr|d}n|d}i}g}|d}t|tr)|}nt|tr0|}tj|d|dit j |ddd||d S) z7Return response if faked for this URL in uaclient.conf.NrresponsecoderT) sort_keys)rHrbody json_dict json_list) rElenpop isinstancedictlistr HTTPResponser*r+r,)rr& responsesrGrKrLresprrrr2os(       z#UAServiceClient._get_fake_responsesr)NNNNTN)__name__ __module__ __qualname__r5r>propertyabcabstractmethodstrrrrrrrboolintrrRr<rEr2rrrrr s.  ("r ) metaclass)rYr+r@r/typingrrr urllib.parseruaclientrrrr r ABCMetar rrrrs