
    xh&                         d Z ddlZddlZddlmZ 	 ddlmZ ddlm	Z	 ddl
mZ n# e$ r ddlZddlmZ ddlm	Z	 Y nw xY w ej        e          Z G d de          Z G d	 d
e          ZdS )zHTTP Client library    N   )handle_error)	urlencode)	HTTPErrorc                   p    e Zd ZdZd Zed             Zed             Zed             Zed             Z	dS )Responsez$Holds the response from an API call.c                     |                                 | _        |                                | _        |                                | _        dS )z
        :param response: The return value from a open call
                         on a urllib.build_opener()
        :type response:  urllib response object
        N)getcode_status_coderead_bodyinfo_headers)selfresponses     L/var/www/html/what/lib/python3.11/site-packages/python_http_client/client.py__init__zResponse.__init__   s:     %,,..]]__
     c                     | j         S )z;
        :return: integer, status code of API call
        )r   r   s    r   status_codezResponse.status_code!   s    
   r   c                     | j         S )z0
        :return: response from the API
        )r   r   s    r   bodyzResponse.body(   s    
 zr   c                     | j         S )z3
        :return: dict of response headers
        )r   r   s    r   headerszResponse.headers/   s    
 }r   c                 l    | j         r,t          j        | j                             d                    S dS )z8
        :return: dict of response from the API
        utf-8N)r   jsonloadsdecoder   s    r   to_dictzResponse.to_dict6   s3    
 9 	:di..w778884r   N)
__name__
__module____qualname____doc__r   propertyr   r   r   r!    r   r   r   r      s        ..( ( ( ! ! X!   X   X   X  r   r   c                   f    e Zd ZdZh dZ	 	 	 	 	 ddZd Zd Zd Zdd	Z	dd
Z
d Zd Zd Zd ZdS )Clientz4Quickly and easily access any REST or REST-like API.>   getputpostpatchdeleteNFc                 b    || _         |pi | _        || _        |pg | _        || _        || _        dS )a  
        :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)hostrequest_headers_version	_url_pathappend_slashtimeout)r   r0   r1   versionurl_pathr4   r5   s          r   r   zClient.__init__G   s=    * 	.4"!R(r   c                 ^    d                     | j        t          | j                  |          S )zSubclass 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
        z{}/v{}{})formatr0   strr2   )r   urls     r   _build_versioned_urlzClient._build_versioned_urle   s(       C,>,>DDDr   c                    d}d}|t          | j                  k     r@|d                    | j        |                   z  }|dz  }|t          | j                  k     @| j        r|dz  }|rEt	          t          |                                          d          }d                    ||          }| j        r|                     |          }nd                    | j	        |          }|S )	zBuild the final URL to be passed to urllib

        :param query_params: A dictionary of all the query parameters
        :type query_params: dictionary
        :return: string
         r   z/{}r   /Tz{}?{}z{}{})
lenr3   r9   r4   r   sorteditemsr2   r<   r0   )r   query_paramsr;   count
url_valuess        r   
_build_urlzClient._build_urlo   s     c$.))))5<<u 5666CQJE c$.))))
  	3JC 	2"6,*<*<*>*>#?#?FFJ..j11C= 	0++C00CC--	3//C
r   c                 :    | j                             |           dS )zUpdate the headers for the request

        :param request_headers: headers to set for the API call
        :type request_headers: dictionary
        :return: dictionary
        N)r1   update)r   r1   s     r   _update_headerszClient._update_headers   s!     	##O44444r   c                     |r| j         |gz   n| j         }t          | j        | j        | j        || j        | j                  S )zMake a new Client object

        :param name: Name of the url segment
        :type name: string
        :return: A Client object
        )r0   r6   r1   r7   r4   r5   )r3   r)   r0   r2   r1   r4   r5   )r   namer7   s      r   _build_clientzClient._build_client   sR     /3F4>TF**49"m&*&:'#'#4"l, , , 	,r   c                 0   |p| j         }	 |                    ||          S # t          $ ri}t          |          }d|_        t
                              d                    |                                |j	        |j
                             |d}~ww xY w)a  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
        r5   N"{method} Response: {status} {body}methodstatusr   )r5   openr   r   	__cause___loggerdebugr9   
get_methodr   r   )r   openerrequestr5   errexcs         r   _make_requestzClient._make_request   s     )T\		;;w;888 	 	 	s##C CMMM>EE))++X F         I	s   " 
BA$BBc                 ,    |                      |          S )aD  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
        )rL   )r   rK   s     r   _zClient._   s     !!$'''r   c                      |dk    r fd}|S | j         v r!|                                	 	 	 	 d fd	}|S                      |          S )aD  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
        r6   c                  F    | d         _                                         S )z
                :param args: dict of settings
                :param kwargs: unused
                :return: string, version
                r   )r2   rL   )argskwargsr   s     r   get_versionz'Client.__getattr__.<locals>.get_version   s"     !%Q))+++r   Nc                    |r
                     |           | d}nrd
j        v r'
j        d         dk    r|                     d          }nB
j                            dd           t	          j        |                               d          }t          j                    }t          j        
	                    |          
j        |          }	fd|_
        t                              d                    	|                                                     |j        r3t                              d	                    |j        
                     t                              d                    |j                             t#          
                    |||                    }t                              d                    	|j        |j                             |S )a  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
                NzContent-Typezapplication/jsonr   )r   datac                       S Nr'   )rQ   s   r   <lambda>z:Client.__getattr__.<locals>.http_request.<locals>.<lambda>	  s    V r   z{method} Request: {url})rQ   r;   zPAYLOAD: {data})re   zHEADERS: {headers})r   rN   rO   rP   )rI   r1   encode
setdefaultr   dumpsurllibbuild_openerRequestrF   rW   rU   rV   r9   get_full_urlre   r   r   r\   r   r   )request_bodyrC   r1   r5   r^   re   rX   rY   r   rQ   r   s            r   http_requestz(Client.__getattr__.<locals>.http_request   s   ( # :((999'DD &)=== 0@./ /+227;;,77*,>@ @ @#z,77>>wGG,.. .OOL11 0  
 &4^^^"7>>!,,.. ? 0 0 1 1 1 < ,MM"3":":$\ #; #+ #+ , , ,299#O : - - . . . $&&vw&HH  BII!#/! J ( ( ) ) )
  r   )NNNN)methodsupperr^   )r   rK   rc   rq   rQ   s   `   @r   __getattr__zClient.__getattr__   s     9, , , , ,  4<ZZ\\F "&!%$( 	?  ?  ?  ?  ?  ?  ? B   66$<<r   c                     | j         S rg   __dict__r   s    r   __getstate__zClient.__getstate__$  s
    }r   c                     || _         d S rg   rv   )r   states     r   __setstate__zClient.__setstate__'  s    r   )NNNFNrg   )r"   r#   r$   r%   rr   r   r<   rF   rI   rL   r\   r^   rt   rx   r{   r'   r   r   r)   r)   A   s        >> 877G "&#   <E E E  65 5 5, , , ,   0
( 
( 
(\  \  \ |      r   r)   )r%   r   logging
exceptionsr   urllib.requestrY   rl   urllib.parser   urllib.errorr   ImportErrorurllib2	getLoggerr"   rU   objectr   r)   r'   r   r   <module>r      s;       $ $ $ $ $ $	!######&&&&&&&&&&&&& ! ! !!!!!!!        	! '
H
%
%* * * * *v * * *Zg g g g gV g g g g gs   % ==