a
    !f3                     @   sh   d Z ddlZddlmZ ddlZddlmZ ddlZddlm	Z	 dZ
G dd deZG d	d
 d
eZdS )z4Shared implementation of connections to API servers.    N)get_distribution)	urlencode)make_exceptionzhttps://www.googleapis.comc                   @   sR   e Zd ZdZdedjZdZdddZ	e
dd Ze
d	d
 Zedd ZdS )
Connectiona  A generic connection to Google Cloud Platform.

    Subclasses should understand only the basic types in method arguments,
    however they should be capable of returning advanced types.

    If no value is passed in for ``http``, a :class:`httplib2.Http` object
    will be created and authorized with the ``credentials``. If not, the
    ``credentials`` and ``http`` need not be related.

    Subclasses may seek to use the private key from ``credentials`` to sign
    data.

    A custom (non-``httplib2``) HTTP object must have a ``request`` method
    which accepts the following arguments:

    * ``uri``
    * ``method``
    * ``body``
    * ``headers``

    In addition, ``redirections`` and ``connection_type`` may be used.

    Without the use of ``credentials.authorize(http)``, a custom ``http``
    object will also need to be able to add a bearer token to API
    requests and handle token refresh on 401 errors.

    :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                       :class:`NoneType`
    :param credentials: The OAuth2 Credentials to use for this connection.

    :type http: :class:`httplib2.Http` or class that defines ``request()``.
    :param http: An optional HTTP object to make requests.
    zgcloud-python/{0}ZgcloudNc                 C   s   || _ | || j| _d S )N)_http_create_scoped_credentialsSCOPE_credentials)selfcredentialshttp r   R/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/connection.py__init__K   s    zConnection.__init__c                 C   s   | j S )zGetter for current credentials.

        :rtype: :class:`oauth2client.client.OAuth2Credentials` or
                :class:`NoneType`
        :returns: The credentials object associated with this connection.
        )r	   r
   r   r   r   r   P   s    zConnection.credentialsc                 C   s0   | j du r*t | _ | jr*| j| j | _ | j S )zA getter for the HTTP transport used in talking to the API.

        :rtype: :class:`httplib2.Http`
        :returns: A Http object used to transport data.
        N)r   httplib2ZHttpr	   Z	authorizer   r   r   r   r   Z   s
    

zConnection.httpc                 C   s2   | r.z|   r| |} W n ty,   Y n0 | S )a"  Create a scoped set of credentials if it is required.

        :type credentials: :class:`oauth2client.client.OAuth2Credentials` or
                           :class:`NoneType`
        :param credentials: The OAuth2 Credentials to add a scope to.

        :type scope: list of URLs
        :param scope: the effective service auth scopes for the connection.

        :rtype: :class:`oauth2client.client.OAuth2Credentials` or
                :class:`NoneType`
        :returns: A new credentials object that has a scope added (if needed).
        )Zcreate_scoped_requiredZcreate_scopedAttributeError)r   scoper   r   r   r   g   s    z%Connection._create_scoped_credentials)NN)__name__
__module____qualname____doc__formatr   version
USER_AGENTr   r   propertyr   r   staticmethodr   r   r   r   r   r      s   "

	
r   c                   @   sF   e Zd ZdZdZdZdZedddZdddZ	dd Z
dd
dZdS )JSONConnectiona  A connection to a Google JSON-based API.

    These APIs are discovery based. For reference:

        https://developers.google.com/discovery/

    This defines :meth:`api_request` for making a generic JSON
    API request and API requests are created elsewhere.

    The class constants

    * :attr:`API_BASE_URL`
    * :attr:`API_VERSION`
    * :attr:`API_URL_TEMPLATE`

    must be updated by subclasses.
    Nc                 C   s>   | j j|p| j|p| j|d}|p$i }|r:|dt| 7 }|S )a  Construct an API url given a few components, some optional.

        Typically, you shouldn't need to use this method.

        :type path: string
        :param path: The path to the resource (ie, ``'/b/bucket-name'``).

        :type query_params: dict or list
        :param query_params: A dictionary of keys and values (or list of
                             key-value pairs) to insert into the query
                             string of the URL.

        :type api_base_url: string
        :param api_base_url: The base URL for the API endpoint.
                             Typically you won't have to provide this.

        :type api_version: string
        :param api_version: The version of the API to call.
                            Typically you shouldn't provide this and instead
                            use the default for the library.

        :rtype: string
        :returns: The URL assembled from the pieces provided.
        )api_base_urlapi_versionpath?)API_URL_TEMPLATEr   API_BASE_URLAPI_VERSIONr   )clsr    query_paramsr   r   urlr   r   r   build_api_url   s    zJSONConnection.build_api_urlc                 C   sZ   |pi }d|d< |r"t t|}nd}t||d< |r>||d< | j|d< | |||||S )a  A low level method to send a request to the API.

        Typically, you shouldn't need to use this method.

        :type method: string
        :param method: The HTTP method to use in the request.

        :type url: string
        :param url: The URL to send the request to.

        :type data: string
        :param data: The data to send as the body of the request.

        :type content_type: string
        :param content_type: The proper MIME type of the data provided.

        :type headers: dict
        :param headers: A dictionary of HTTP headers to send with the request.

        :type target_object: object or :class:`NoneType`
        :param target_object: Argument to be used by library callers.
                              This can allow custom behavior, for example, to
                              defer an HTTP request and complete initialization
                              of the object at a later time.

        :rtype: tuple of ``response`` (a dictionary of sorts)
                and ``content`` (a string).
        :returns: The HTTP response object and the content of the response,
                  returned by :meth:`_do_request`.
        gzipzAccept-Encodingr   zContent-LengthzContent-Typez
User-Agent)lenstrr   _do_request)r
   methodr'   datacontent_typeheaderstarget_objectcontent_lengthr   r   r   _make_request   s     
zJSONConnection._make_requestc                 C   s   | j j||||dS )an  Low-level helper:  perform the actual API request over HTTP.

        Allows batch context managers to override and defer a request.

        :type method: string
        :param method: The HTTP method to use in the request.

        :type url: string
        :param url: The URL to send the request to.

        :type headers: dict
        :param headers: A dictionary of HTTP headers to send with the request.

        :type data: string
        :param data: The data to send as the body of the request.

        :type target_object: object or :class:`NoneType`
        :param target_object: Unused ``target_object`` here but may be used
                              by a superclass.

        :rtype: tuple of ``response`` (a dictionary of sorts)
                and ``content`` (a string).
        :returns: The HTTP response object and the content of the response.
        )urir-   r0   body)r   request)r
   r-   r'   r0   r.   r1   r   r   r   r,      s    zJSONConnection._do_requestTc
                 C   s   | j ||||d}
|r.t|tr.t|}d}| j||
|||	d\}}d|j  kr\dk stn t|||d |
 dtj	tj
f}|r|rt||r|dd	}|dstd
| t|tj	r|d}t|S |S )a	  Make a request over the HTTP transport to the API.

        You shouldn't need to use this method, but if you plan to
        interact with the API using these primitives, this is the
        correct one to use.

        :type method: string
        :param method: The HTTP method name (ie, ``GET``, ``POST``, etc).
                       Required.

        :type path: string
        :param path: The path to the resource (ie, ``'/b/bucket-name'``).
                     Required.

        :type query_params: dict or list
        :param query_params: A dictionary of keys and values (or list of
                             key-value pairs) to insert into the query
                             string of the URL.

        :type data: string
        :param data: The data to send as the body of the request. Default is
                     the empty string.

        :type content_type: string
        :param content_type: The proper MIME type of the data provided. Default
                             is None.

        :type api_base_url: string
        :param api_base_url: The base URL for the API endpoint.
                             Typically you won't have to provide this.
                             Default is the standard API base URL.

        :type api_version: string
        :param api_version: The version of the API to call.  Typically
                            you shouldn't provide this and instead use
                            the default for the library.  Default is the
                            latest API version supported by
                            gcloud-python.

        :type expect_json: bool
        :param expect_json: If True, this method will try to parse the
                            response as JSON and raise an exception if
                            that cannot be done.  Default is True.

        :type _target_object: :class:`object` or :class:`NoneType`
        :param _target_object: Protected argument to be used by library
                               callers. This can allow custom behavior, for
                               example, to defer an HTTP request and complete
                               initialization of the object at a later time.

        :raises: Exception if the response code is not 200 OK.
        :rtype: dict or str
        :returns: The API response payload, either as a raw string or
                  a dictionary if the response is valid JSON.
        )r    r&   r   r   zapplication/json)r-   r'   r.   r/   r1      i,   )
error_infozcontent-type zExpected JSON, got %szutf-8)r(   
isinstancedictjsondumpsr3   statusr   sixbinary_type	text_typeget
startswith	TypeErrordecodeloads)r
   r-   r    r&   r.   r/   r   r   Zexpect_jsonZ_target_objectr'   responsecontentZstring_or_bytesr   r   r   api_request  s0    ;





zJSONConnection.api_request)NNN)NNNN)NNNNNTN)r   r   r   r   r#   r$   r"   classmethodr(   r3   r,   rJ   r   r   r   r   r      s"     %  
2    r   )r   r=   pkg_resourcesr   r@   Zsix.moves.urllib.parser   r   Zgcloud.exceptionsr   r#   objectr   r   r   r   r   r   <module>   s   `