-
Notifications
You must be signed in to change notification settings - Fork 948
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1392 from burnash/bugfix/client_export
bugfix/client export
- Loading branch information
Showing
7 changed files
with
1,146 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,24 @@ | |
""" | ||
from http import HTTPStatus | ||
from typing import IO, Any, List, Mapping, MutableMapping, Optional, Tuple, Type, Union | ||
from typing import ( | ||
IO, | ||
Any, | ||
Dict, | ||
List, | ||
Mapping, | ||
MutableMapping, | ||
Optional, | ||
Tuple, | ||
Type, | ||
Union, | ||
) | ||
|
||
from google.auth.credentials import Credentials | ||
from google.auth.transport.requests import AuthorizedSession | ||
from requests import Response, Session | ||
|
||
from .exceptions import APIError | ||
from .exceptions import APIError, UnSupportedExportFormat | ||
from .urls import ( | ||
DRIVE_FILES_API_V3_URL, | ||
SPREADSHEET_BATCH_UPDATE_URL, | ||
|
@@ -26,7 +37,7 @@ | |
SPREADSHEET_VALUES_CLEAR_URL, | ||
SPREADSHEET_VALUES_URL, | ||
) | ||
from .utils import convert_credentials, quote | ||
from .utils import ExportFormat, convert_credentials, quote | ||
|
||
ParamsType = MutableMapping[str, Optional[Union[str, int, bool, float, List[str]]]] | ||
|
||
|
@@ -306,6 +317,153 @@ def get_file_drive_metadata(self, id: str) -> Any: | |
|
||
return res.json() | ||
|
||
def export(self, file_id: str, format: str = ExportFormat.PDF) -> bytes: | ||
"""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 | ||
""" | ||
|
||
if format not in ExportFormat: | ||
raise UnSupportedExportFormat | ||
|
||
url = "{}/{}/export".format(DRIVE_FILES_API_V3_URL, file_id) | ||
|
||
params: ParamsType = {"mimeType": format} | ||
|
||
r = self.request("get", url, params=params) | ||
return r.content | ||
|
||
def insert_permission( | ||
self, | ||
file_id: str, | ||
email_address: Optional[str], | ||
perm_type: Optional[str], | ||
role: Optional[str], | ||
notify: bool = True, | ||
email_message: Optional[str] = None, | ||
with_link: bool = False, | ||
) -> Response: | ||
"""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 [email protected] | ||
gc.insert_permission( | ||
'0BmgG6nO_6dprnRRUWl1UFE', | ||
'[email protected]', | ||
perm_type='user', | ||
role='writer' | ||
) | ||
# Make the spreadsheet publicly readable | ||
gc.insert_permission( | ||
'0BmgG6nO_6dprnRRUWl1UFE', | ||
None, | ||
perm_type='anyone', | ||
role='reader' | ||
) | ||
""" | ||
url = "{}/{}/permissions".format(DRIVE_FILES_API_V3_URL, file_id) | ||
payload = { | ||
"type": perm_type, | ||
"role": role, | ||
"withLink": with_link, | ||
} | ||
params: ParamsType = { | ||
"supportsAllDrives": "true", | ||
} | ||
|
||
if perm_type == "domain": | ||
payload["domain"] = email_address | ||
elif perm_type in {"user", "group"}: | ||
payload["emailAddress"] = email_address | ||
params["sendNotificationEmail"] = notify | ||
params["emailMessage"] = email_message | ||
elif perm_type == "anyone": | ||
pass | ||
else: | ||
raise ValueError("Invalid permission type: {}".format(perm_type)) | ||
|
||
return self.request("post", url, json=payload, params=params) | ||
|
||
def list_permissions(self, file_id: str) -> List[Dict[str, Union[str, bool]]]: | ||
"""Retrieve a list of permissions for a file. | ||
:param str file_id: a spreadsheet ID (aka file ID). | ||
""" | ||
url = "{}/{}/permissions".format(DRIVE_FILES_API_V3_URL, file_id) | ||
|
||
params: ParamsType = { | ||
"supportsAllDrives": True, | ||
"fields": "nextPageToken,permissions", | ||
} | ||
|
||
token = "" | ||
|
||
permissions = [] | ||
|
||
while token is not None: | ||
if token: | ||
params["pageToken"] = token | ||
|
||
r = self.request("get", url, params=params).json() | ||
permissions.extend(r["permissions"]) | ||
|
||
token = r.get("nextPageToken", None) | ||
|
||
return permissions | ||
|
||
def remove_permission(self, file_id: str, permission_id: str) -> None: | ||
"""Deletes a permission from a file. | ||
:param str file_id: a spreadsheet ID (aka file ID.) | ||
:param str permission_id: an ID for the permission. | ||
""" | ||
url = "{}/{}/permissions/{}".format( | ||
DRIVE_FILES_API_V3_URL, file_id, permission_id | ||
) | ||
|
||
params: ParamsType = {"supportsAllDrives": True} | ||
self.request("delete", url, params=params) | ||
|
||
|
||
class BackOffHTTPClient(HTTPClient): | ||
"""BackoffClient is a gspread client with exponential | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.