a
    |f}X                     @   s~  d Z ddlZddlmZ ddlmZmZmZmZm	Z	m
Z
mZmZmZmZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( e
e)eee)e*e+e,ee) f  f Z-eee
e)ee f e
e)ee)ee f f e
e)ee)ee e)f f e
e)ee)ee e)e
e)e)f f f f  Z.G dd dZ/G dd de/Z0ee/ Z1dS )z{
gspread.http_client
~~~~~~~~~~~~~~

This module contains HTTPClient class responsible for communicating with
Google API.

    N)
HTTPStatus)
IOAnyDictListMappingMutableMappingOptionalTupleTypeUnion)Credentials)AuthorizedSession)ResponseSession   )APIErrorUnSupportedExportFormat)DRIVE_FILES_API_V3_URLDRIVE_FILES_UPLOAD_API_V2_URLSPREADSHEET_BATCH_UPDATE_URLSPREADSHEET_SHEETS_COPY_TO_URLSPREADSHEET_URLSPREADSHEET_VALUES_APPEND_URL"SPREADSHEET_VALUES_BATCH_CLEAR_URL#SPREADSHEET_VALUES_BATCH_UPDATE_URLSPREADSHEET_VALUES_BATCH_URLSPREADSHEET_VALUES_CLEAR_URLSPREADSHEET_VALUES_URL)ExportFormatconvert_credentialsquotec                   @   s  e Zd ZdZdAeee ddddZddddZee	e
ee
e
f f  dd	d
dZdBeeee ee eeeef  eeeeef  edddZeeeeef  edddZdCeeee eeeef  edddZeeeeeeef  edddZeeedddZdDeee eeeef  edddZdEeeee edddZdFeee ee ed d!d"ZdGeeeeef  edd#d$ZdHeee ed%d&d'Zee eed(d)d*Z!dIeee eeef d%d+d,Z"eed-d.d/Z#e$j%feeed0d1d2Z&dJeee ee ee e'ee e'ed5d6d7Z(eee)ee	ee'f f  d8d9d:Z*eedd;d<d=Z+ee	eef ed>d?d@Z,dS )K
HTTPClienta  An instance of this class communicates with Google API.

    :param Credentials auth: An instance of google.auth.Credentials used to authenticate requests
        created by either:

        * gspread.auth.oauth()
        * gspread.auth.oauth_from_dict()
        * gspread.auth.service_account()
        * gspread.auth.service_account_from_dict()

    :param Session session: (Optional) An OAuth2 credential object. Credential objects
        created by `google-auth <https://github.com/googleapis/google-auth-library-python>`_.

        You can pass you own Session object, simply pass ``auth=None`` and ``session=my_custom_session``.

    This class is not intended to be created manually.
    It will be created by the gspread.Client class.
    N)authsessionreturnc                 C   s0   |d ur|| _ nt|| _t| j| _ d | _d S )N)r$   r    r#   r   timeout)selfr#   r$    r(   MD:\Projects\storyit_web\backend\venv\Lib\site-packages\gspread/http_client.py__init__M   s
    
zHTTPClient.__init__)r%   c                 C   s<   ddl m} | j|| j | jjdd| jj i d S )Nr   )RequestAuthorizationz	Bearer %s)google.auth.transport.requestsr+   r#   refreshr$   headersupdatetoken)r'   r+   r(   r(   r)   loginV   s    zHTTPClient.login)r&   r%   c                 C   s
   || _ dS )zHow long to wait for the server to send
        data before giving up, as a float, or a ``(connect timeout,
        read timeout)`` tuple.

        Use value ``None`` to restore default timeout

        Value for ``timeout`` is in seconds (s).
        N)r&   )r'   r&   r(   r(   r)   set_timeout]   s    	zHTTPClient.set_timeout)methodendpointparamsdatajsonfilesr/   r%   c           	   
   C   s4   | j j|||||||| jd}|jr(|S t|d S )N)r4   urlr8   r6   r7   r9   r/   r&   )r$   requestr&   okr   )	r'   r4   r5   r6   r7   r8   r9   r/   responser(   r(   r)   r;   h   s    
zHTTPClient.request)idbodyr%   c                 C   s   | j dt| |d}| S )a  Lower-level method that directly calls `spreadsheets/<ID>:batchUpdate <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate>`_.

        :param dict body: `Batch Update Request body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate#request-body>`_.
        :returns: `Batch Update Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate#response-body>`_.
        :rtype: dict

        .. versionadded:: 3.0
        postr8   )r;   r   r8   )r'   r>   r?   rr(   r(   r)   batch_update   s    	zHTTPClient.batch_update)r>   ranger6   r?   r%   c                 C   s*   t |t|f }| jd|||d}| S )ay  Lower-level method that directly calls `PUT spreadsheets/<ID>/values/<range> <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update>`_.

        :param str range: The `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_ of the values to update.
        :param dict params: (optional) `Values Update Query parameters <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update#query-parameters>`_.
        :param dict body: (optional) `Values Update Request body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update#request-body>`_.
        :returns: `Values Update Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update#response-body>`_.
        :rtype: dict

        Example::

            sh.values_update(
                'Sheet1!A2',
                params={
                    'valueInputOption': 'USER_ENTERED'
                },
                body={
                    'values': [[1, 2, 3]]
                }
            )

        .. versionadded:: 3.0
        putr6   r8   r   r!   r;   r8   r'   r>   rD   r6   r?   r:   rB   r(   r(   r)   values_update   s    zHTTPClient.values_updatec                 C   s*   t |t|f }| jd|||d}| S )a  Lower-level method that directly calls `spreadsheets/<ID>/values:append <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append>`_.

        :param str range: The `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_
                          of a range to search for a logical table of data. Values will be appended after the last row of the table.
        :param dict params: `Values Append Query parameters <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#query-parameters>`_.
        :param dict body: `Values Append Request body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#request-body>`_.
        :returns: `Values Append Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#response-body>`_.
        :rtype: dict

        .. versionadded:: 3.0
        r@   rF   )r   r!   r;   r8   rH   r(   r(   r)   values_append   s    zHTTPClient.values_append)r>   rD   r%   c                 C   s$   t |t|f }| d|}| S )a   Lower-level method that directly calls `spreadsheets/<ID>/values:clear <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear>`_.

        :param str range: The `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_ of the values to clear.
        :returns: `Values Clear Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear#response-body>`_.
        :rtype: dict

        .. versionadded:: 3.0
        r@   )r   r!   r;   r8   )r'   r>   rD   r:   rB   r(   r(   r)   values_clear   s    	zHTTPClient.values_clear)r>   r6   r?   r%   c                 C   s"   t | }| jd|||d}| S )a  Lower-level method that directly calls `spreadsheets/<ID>/values:batchClear`

        :param dict params: (optional) `Values Batch Clear Query parameters <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchClear#path-parameters>`_.
        :param dict body: (optional) `Values Batch Clear request body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchClear#request-body>`_.
        :rtype: dict
        r@   rF   )r   r;   r8   )r'   r>   r6   r?   r:   rB   r(   r(   r)   values_batch_clear   s    zHTTPClient.values_batch_clear)r>   rD   r6   r%   c                 C   s(   t |t|f }| jd||d}| S )a  Lower-level method that directly calls `GET spreadsheets/<ID>/values/<range> <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get>`_.

        :param str range: The `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_ of the values to retrieve.
        :param dict params: (optional) `Values Get Query parameters <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get#query-parameters>`_.
        :returns: `Values Get Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get#response-body>`_.
        :rtype: dict

        .. versionadded:: 3.0
        getr6   rG   )r'   r>   rD   r6   r:   rB   r(   r(   r)   
values_get   s    zHTTPClient.values_get)r>   rangesr6   r%   c                 C   s4   |du ri }||d< t | }| jd||d}| S )a  Lower-level method that directly calls `spreadsheets/<ID>/values:batchGet <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchGet>`_.

        :param list ranges: List of ranges in the `A1 notation <https://developers.google.com/sheets/api/guides/concepts#a1_notation>`_ of the values to retrieve.
        :param dict params: (optional) `Values Batch Get Query parameters <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchGet#query-parameters>`_.
        :returns: `Values Batch Get Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchGet#response-body>`_.
        :rtype: dict
        NrP   rM   rN   )r   r;   r8   )r'   r>   rP   r6   r:   rB   r(   r(   r)   values_batch_get   s    
zHTTPClient.values_batch_getc                 C   s    t | }| jd||d}| S )a"  Lower-level method that directly calls `spreadsheets/<ID>/values:batchUpdate <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate>`_.

        :param dict body: (optional) `Values Batch Update Request body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate#request-body>`_.
        :returns: `Values Batch Update Response body <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate#response-body>`_.
        :rtype: dict
        r@   rA   )r   r;   r8   )r'   r>   r?   r:   rB   r(   r(   r)   values_batch_update  s    	zHTTPClient.values_batch_update)r>   r6   r%   c                 C   s    t | }| jd||d}| S )zA method stub that directly calls `spreadsheets.get <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get>`_.rM   rN   r   r;   r8   r'   r>   r6   r:   rB   r(   r(   r)   spreadsheets_get  s    zHTTPClient.spreadsheets_get)r>   sheet_iddestination_spreadsheet_idr%   c                 C   s,   t ||f }d|i}| jd||d}| S )zLower-level method that directly calls `spreadsheets.sheets.copyTo <https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.sheets/copyTo>`_.ZdestinationSpreadsheetIdr@   rA   )r   r;   r8   )r'   r>   rV   rW   r:   r?   rB   r(   r(   r)   spreadsheets_sheets_copy_to  s    z&HTTPClient.spreadsheets_sheets_copy_toc                 C   s0   |du rddi}t | }| jd||d}| S )a  Similar to :method spreadsheets_get:`gspread.http_client.spreadsheets_get`,
        get the spreadsheet form the API but by default **does not get the cells data**.
        It only retrieve the the metadata from the spreadsheet.

        :param str id: the spreadsheet ID key
        :param dict params: (optional) the HTTP params for the GET request.
            By default sets the parameter ``includeGridData`` to ``false``.
        :returns: The raw spreadsheet
        :rtype: dict
        NZincludeGridDatafalserM   rN   rS   rT   r(   r(   r)   fetch_sheet_metadata  s
    zHTTPClient.fetch_sheet_metadata)r>   r%   c                 C   s2   t d| }dddd}| jd||d}| S )zGet the metadata from the Drive API for a specific file
        This method is mainly here to retrieve the create/update time
        of a file (these metadata are only accessible from the Drive API).
        z/{}Tz id,name,createdTime,modifiedTime)supportsAllDrivesZincludeItemsFromAllDrivesfieldsrM   rN   )r   formatr;   r8   )r'   r>   r:   r6   resr(   r(   r)   get_file_drive_metadata5  s    z"HTTPClient.get_file_drive_metadata)file_idr]   r%   c                 C   s6   |t vrtdt|}d|i}| jd||d}|jS )a  Export the spreadsheet in the given format.

        :param str file_id: The key of the spreadsheet to export

        :param str format: The format of the resulting file.
            Possible values are:

                * ``ExportFormat.PDF``
                * ``ExportFormat.EXCEL``
                * ``ExportFormat.CSV``
                * ``ExportFormat.OPEN_OFFICE_SHEET``
                * ``ExportFormat.TSV``
                * ``ExportFormat.ZIPPED_HTML``

            See `ExportFormat`_ in the Drive API.

        :type format: :class:`~gspread.utils.ExportFormat`

        :returns bytes: The content of the exported file.

        .. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats
        z{}/{}/exportZmimeTyperM   rN   )r   r   r]   r   r;   content)r'   r`   r]   r:   r6   rB   r(   r(   r)   exportG  s    zHTTPClient.exportTF)r`   email_address	perm_typerolenotifyemail_message	with_linkr%   c                 C   s~   d t|}|||d}	ddi}
|dkr2||	d< n:|dv rT||	d< ||
d< ||
d	< n|d
kr^ntd || jd||	|
dS )a  Creates a new permission for a file.

        :param str file_id: a spreadsheet ID (aka file ID).
        :param email_address: user or group e-mail address, domain name
            or None for 'anyone' type.
        :type email_address: str, None
        :param str perm_type: (optional) The account type.
            Allowed values are: ``user``, ``group``, ``domain``, ``anyone``
        :param str role: (optional) The primary role for this user.
            Allowed values are: ``owner``, ``writer``, ``reader``
        :param bool notify: Whether to send an email to the target
            user/domain. Default ``True``.
        :param str email_message: (optional) An email message to be sent
            if ``notify=True``.
        :param bool with_link: Whether the link is required for this
            permission to be active. Default ``False``.

        :returns dict: the newly created permission

        Examples::

            # Give write permissions to otto@example.com

            gc.insert_permission(
                '0BmgG6nO_6dprnRRUWl1UFE',
                'otto@example.org',
                perm_type='user',
                role='writer'
            )

            # Make the spreadsheet publicly readable

            gc.insert_permission(
                '0BmgG6nO_6dprnRRUWl1UFE',
                None,
                perm_type='anyone',
                role='reader'
            )

        {}/{}/permissions)typere   ZwithLinkr[   truedomain>   groupuserZemailAddressZsendNotificationEmailZemailMessageZanyonezInvalid permission type: {}r@   )r8   r6   )r]   r   
ValueErrorr;   )r'   r`   rc   rd   re   rf   rg   rh   r:   payloadr6   r(   r(   r)   insert_permissioni  s"    2

zHTTPClient.insert_permission)r`   r%   c                 C   sf   d t|}ddd}d}g }|durb|r2||d< | jd||d	 }||d
  |dd}q|S )zpRetrieve a list of permissions for a file.

        :param str file_id: a spreadsheet ID (aka file ID).
        ri   TznextPageToken,permissions)r[   r\    NZ	pageTokenrM   rN   permissionsZnextPageToken)r]   r   r;   r8   extendrM   )r'   r`   r:   r6   r1   rs   rB   r(   r(   r)   list_permissions  s    zHTTPClient.list_permissions)r`   permission_idr%   c                 C   s*   d t||}ddi}| jd||d dS )zDeletes a permission from a file.

        :param str file_id: a spreadsheet ID (aka file ID.)
        :param str permission_id: an ID for the permission.
        z{}/{}/permissions/{}r[   TdeleterN   N)r]   r   r;   )r'   r`   rv   r:   r6   r(   r(   r)   remove_permission  s
    zHTTPClient.remove_permission)r`   r7   r%   c                 C   sL   t |tr|d}ddi}dt|}| jd||dddd|d	}| S )
a  Imports data into the first page of the spreadsheet.

        :param str data: A CSV string of data.

        Example:

        .. code::

            # Read CSV file contents
            content = open('file_to_import.csv', 'r').read()

            gc.import_csv(spreadsheet.id, content)

        .. note::

           This method removes all other worksheets and then entirely
           replaces the contents of the first worksheet.

        zutf-8zContent-Typeztext/csvz{}/{}rE   ZmediaT)Z
uploadTypeconvertr[   )r7   r6   r/   )
isinstancestrencoder]   r   r;   r8   )r'   r`   r7   r/   r:   r^   r(   r(   r)   
import_csv  s    

zHTTPClient.import_csv)N)NNNNN)NN)NN)N)N)N)N)N)TNF)-__name__
__module____qualname____doc__r   r	   r   r*   r2   r   floatr
   r3   r{   
ParamsTypebytesr   r   FileTyper   r   r;   rC   rI   rJ   rK   rL   rO   r   rQ   rR   rU   intrX   rZ   r_   r   ZPDFrb   boolrq   r   ru   rx   r}   r(   r(   r(   r)   r"   9   s   	$       "   
   
(   I$r"   c                       s\   e Zd ZU dZejejgZee e	d< dZ
ee	d< dZee	d< eeed fdd	Z  ZS )
BackOffHTTPClientaf  BackoffClient is a gspread client with exponential
    backoff retries.

    In case a request fails due to some API rate limits,
    it will wait for some time, then retry the request.

    This can help by trying the request after some time and
    prevent the application from failing (by raising an APIError exception).

    .. Warning::
        This Client is not production ready yet.
        Use it at your own risk !

    .. note::
        To use with the `auth` module, make sure to pass this backoff
        client factory using the ``client_factory`` parameter of the
        method used.

    .. note::
        Currently known issues are:

        * will retry exponentially even when the error should
          raise instantly. Due to the Drive API that raises
          403 (Forbidden) errors for forbidden access and
          for api rate limit exceeded._HTTP_ERROR_CODESr   _NR_BACKOFF   _MAX_BACKOFF)argskwargsr%   c           	   
      s   t tttf t td fdd}zt j|i |W S  ty } z||j}|j	}  j
d7  _
td j
  j}||||du rt|  j|i |}d _
|W  Y d }~S |W Y d }~n
d }~0 0 d S )N)codeerrorwaitr%   c                    sH   d|v r*| t jkr*|d d d dkr*dS |  jv s>| t jkoF| jkS )Nerrorsr   rl   ZusageLimitsT)r   	FORBIDDENr   INTERNAL_SERVER_ERRORr   )r   r   r   r'   r(   r)   _should_retry(  s    	
z0BackOffHTTPClient.request.<locals>._should_retryr      Tr   )r   r   r{   r   r   superr;   r   r   r   r   minr   timesleep)	r'   r   r   r   errr   r   r   r=   	__class__r   r)   r;   &  s$    

zBackOffHTTPClient.request)r~   r   r   r   r   REQUEST_TIMEOUTTOO_MANY_REQUESTSr   r   __annotations__r   r   r   r   r   r;   __classcell__r(   r(   r   r)   r     s   
r   )2r   r   httpr   typingr   r   r   r   r   r   r	   r
   r   r   Zgoogle.auth.credentialsr   r-   r   requestsr   r   
exceptionsr   r   urlsr   r   r   r   r   r   r   r   r   r   r   utilsr   r    r!   r{   r   r   r   r   r   r"   r   ZHTTPClientTyper(   r(   r(   r)   <module>   s2   	04""
   NV