ó
º9—^c           @   sÅ   d  Z  d d l Z d d l m Z y3 d d l j Z d d l m Z d d l	 m
 Z
 Wn= e k
 r” d d l Z d d l m
 Z
 d d l m Z n Xd e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d S(   s   HTTP Client libraryiÿÿÿÿNi   (   t   handle_error(   t	   urlencode(   t	   HTTPErrort   Responsec           B   sS   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   s$   Holds the response from an API call.c         C   s1   | j  ƒ  |  _ | j ƒ  |  _ | j ƒ  |  _ d S(   s¨   
        :param response: The return value from a open call
                         on a urllib.build_opener()
        :type response:  urllib response object
        N(   t   getcodet   _status_codet   readt   _bodyt   infot   _headers(   t   selft   response(    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   __init__   s    c         C   s   |  j  S(   s;   
        :return: integer, status code of API call
        (   R   (   R
   (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   status_code   s    c         C   s   |  j  S(   s0   
        :return: response from the API
        (   R   (   R
   (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   body&   s    c         C   s   |  j  S(   s3   
        :return: dict of response headers
        (   R	   (   R
   (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   headers-   s    c         C   s*   |  j  r" t j |  j  j d ƒ ƒ Sd Sd S(   s8   
        :return: dict of response from the API
        s   utf-8N(   R   t   jsont   loadst   decodet   None(   R
   (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   to_dict4   s    	(	   t   __name__t
   __module__t   __doc__R   t   propertyR   R   R   R   (    (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyR      s   	
t   Clientc           B   s’   e  Z d  Z d d d d d h Z d d d e d d „ Z d „  Z d „  Z d	 „  Z	 d d
 „ Z
 d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s4   Quickly and easily access any REST or REST-like API.t   deletet   gett   patcht   postt   putc         C   sF   | |  _  | p i  |  _ | |  _ | p* g  |  _ | |  _ | |  _ d S(   s‰  
        :param host: Base URL for the api. (e.g. https://api.sendgrid.com)
        :type host:  string
        :param request_headers: A dictionary of the headers you want
                                applied on all calls
        :type request_headers: dictionary
        :param version: The version number of the API.
                        Subclass _build_versioned_url for custom behavior.
                        Or just pass the version as part of the URL
                        (e.g. client._("/v3"))
        :type version: integer
        :param url_path: A list of the url path segments
        :type url_path: list of strings
        N(   t   hostt   request_headerst   _versiont	   _url_patht   append_slasht   timeout(   R
   R   R    t   versiont   url_pathR#   R$   (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyR   E   s    			c         C   s   d j  |  j t |  j ƒ | ƒ S(   sþ   Subclass this function for your own needs.
           Or just pass the version as part of the URL
           (e.g. client._('/v3'))
        :param url: URI portion of the full URL being requested
        :type url: string
        :return: string
        s   {}/v{}{}(   t   formatR   t   strR!   (   R
   t   url(    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   _build_versioned_urlc   s    c         C   sÌ   d } d } x= | t  |  j ƒ k  rK | d j |  j | ƒ 7} | d 7} q W|  j rb | d 7} n  | r˜ t t | j ƒ  ƒ t ƒ } d j | | ƒ } n  |  j r³ |  j	 | ƒ } n d j |  j
 | ƒ } | S(   s¹   Build the final URL to be passed to urllib

        :param query_params: A dictionary of all the query parameters
        :type query_params: dictionary
        :return: string
        t    i    s   /{}i   t   /s   {}?{}s   {}{}(   t   lenR"   R'   R#   R   t   sortedt   itemst   TrueR!   R*   R   (   R
   t   query_paramsR)   t   countt
   url_values(    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt
   _build_urlm   s    		c         C   s   |  j  j | ƒ d S(   s²   Update the headers for the request

        :param request_headers: headers to set for the API call
        :type request_headers: dictionary
        :return: dictionary
        N(   R    t   update(   R
   R    (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   _update_headersˆ   s    c         C   sY   | r |  j  | g n |  j  } t d |  j d |  j d |  j d | d |  j d |  j ƒ S(   s‹   Make a new Client object

        :param name: Name of the url segment
        :type name: string
        :return: A Client object
        R   R%   R    R&   R#   R$   (   R"   R   R   R!   R    R#   R$   (   R
   t   nameR&   (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   _build_client‘   s    			c         C   s[   | p |  j  } y | j | d | ƒSWn. t k
 rV } t | ƒ } d | _ | ‚ n Xd S(   s„  Make the API call and return the response. This is separated into
           it's own function, so we can mock it easily for testing.

        :param opener:
        :type opener:
        :param request: url payload to request
        :type request: urllib.Request object
        :param timeout: timeout value or None
        :type timeout: float
        :return: urllib response
        R$   N(   R$   t   openR   R    R   t	   __cause__(   R
   t   openert   requestR$   t   errt   exc(    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   _make_request    s    	c         C   s   |  j  | ƒ S(   sD  Add variable values to the url.
           (e.g. /your/api/{variable_value}/call)
           Another example: if you have a Python reserved word, such as global,
           in your url, you must use this method.

        :param name: Name of the url segment
        :type name: string
        :return: Client object
        (   R8   (   R
   R7   (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   _´   s    
c            sm   | d k r ‡ f d †  } | S| ˆ j  k r\ | j ƒ  ‰  d d d d ‡  ‡ f d † } | Sˆ j | ƒ Sd S(   sD  Dynamically add method calls to the url, then call a method.
           (e.g. client.name.name.method())
           You can also add a version number by using .version(<int>)

        :param name: Name of the url segment or method call
        :type name: string or integer if name == version
        :return: mixed
        R%   c             s   |  d ˆ  _  ˆ  j ƒ  S(   sŽ   
                :param args: dict of settings
                :param kwargs: unused
                :return: string, version
                i    (   R!   R8   (   t   argst   kwargs(   R
   (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   get_versionÊ   s    c            së   | r ˆ j  | ƒ n  |  d k r+ d } n_ d ˆ j k r_ ˆ j d d k r_ |  j d ƒ } n+ ˆ j j d d ƒ t j |  ƒ j d ƒ } t j ƒ  } t j	 ˆ j
 | ƒ d ˆ j d | ƒ} ‡  f d †  | _ t ˆ j | | d | ƒƒ S(	   s…  Make the API call
                :param timeout: HTTP request timeout. Will be propagated to
                    urllib client
                :type timeout: float
                :param request_headers: HTTP headers. Will be merged into
                    current client object state
                :type request_headers: dict
                :param query_params: HTTP query parameters
                :type query_params: dict
                :param request_body: HTTP request body
                :type request_body: string or json-serializable object
                :param kwargs:
                :return: Response object
                s   Content-Types   application/jsons   utf-8R   t   datac              s   ˆ  S(   N(    (    (   t   method(    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   <lambda>  R+   R$   N(   R6   R   R    t   encodet
   setdefaultR   t   dumpst   urllibt   build_openert   RequestR4   t
   get_methodR   R?   (   t   request_bodyR1   R    R$   R@   RD   R;   R<   (   RE   R
   (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   http_requestØ   s&    	
		
		N(   t   methodst   upperR   R@   (   R
   R7   RC   RO   (    (   RE   R
   s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   __getattr__À   s    	,c         C   s   |  j  S(   N(   t   __dict__(   R
   (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   __getstate__  s    c         C   s   | |  _  d  S(   N(   RS   (   R
   t   state(    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   __setstate__  s    N(   R   R   R   RP   R   t   FalseR   R*   R4   R6   R8   R?   R@   RR   RT   RV   (    (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyR   ?   s    	
					M	(   R   R   t
   exceptionsR    t   urllib.requestR<   RJ   t   urllib.parseR   t   urllib.errorR   t   ImportErrort   urllib2t   objectR   R   (    (    (    s;   /tmp/pip-unpacked-wheel-vZkhRb/python_http_client/client.pyt   <module>   s   -