Skip to content

Commit

Permalink
Merge pull request #1255 from burnash/refactor/no-property-io
Browse files Browse the repository at this point in the history
Remove API calls from `creationTime`/`lastUpdateTime`
  • Loading branch information
alifeee committed Aug 9, 2023
2 parents 739b8fc + 4735370 commit 9746f8d
Show file tree
Hide file tree
Showing 89 changed files with 37,906 additions and 12,030 deletions.
2 changes: 1 addition & 1 deletion gspread/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def request(
else:
raise APIError(response)

def _get_file_drive_metadata(self, id):
def get_file_drive_metadata(self, id):
"""Get 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).
Expand Down
45 changes: 28 additions & 17 deletions gspread/spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""

import warnings

from .exceptions import WorksheetNotFound
from .urls import (
DRIVE_FILES_API_V3_URL,
Expand Down Expand Up @@ -34,6 +36,9 @@ def __init__(self, client, properties):
metadata = self.fetch_sheet_metadata()
self._properties.update(metadata["properties"])

drive_metadata = self.client.get_file_drive_metadata(self._properties["id"])
self._properties.update(drive_metadata)

@property
def id(self):
"""Spreadsheet ID."""
Expand All @@ -52,30 +57,27 @@ def url(self):
@property
def creationTime(self):
"""Spreadsheet Creation time."""
try:
return self._properties["createdTime"]
except KeyError:
metadata = self.client._get_file_drive_metadata(self.id)
self._properties.update(metadata)
return self._properties["createdTime"]
return self._properties["createdTime"]

@property
def lastUpdateTime(self):
"""Spreadsheet last updated time."""
try:
return self._properties["modifiedTime"]
except KeyError:
self.refresh_lastUpdateTime()
return self._properties["modifiedTime"]
"""Spreadsheet last updated time.
Only updated on initialisation.
For actual last updated time, use get_lastUpdateTime()."""
warnings.warn(
"""
This is only updated on initialisation and is probably outdated by the time you use it.
For an up to date last updated time, use get_lastUpdateTime().
"""
)
return self._properties["modifiedTime"]

@property
def updated(self):
""".. deprecated:: 2.0
This feature is not supported in Sheets API v4.
"""
import warnings

warnings.warn(
"Spreadsheet.updated() is deprecated, "
"this feature is not supported in Sheets API v4",
Expand Down Expand Up @@ -743,7 +745,16 @@ def list_protected_ranges(self, sheetid):

return sheet.get("protectedRanges", [])

def refresh_lastUpdateTime(self):
"""Refresh the lastUpdateTime property of the spreadsheet."""
metadata = self.client._get_file_drive_metadata(self.id)
def refresh_lastUpdateTime(self) -> None:
"""Updates the cached value of lastUpdateTime."""
# remove this and the below upon deprecation of lastUpdateTime @property
self._properties["modifiedTime"] = self.get_lastUpdateTime()

def get_lastUpdateTime(self) -> str:
"""Get the lastUpdateTime metadata from the Drive API.
Also updates the cached value in the _properties dict.
"""
metadata = self.client.get_file_drive_metadata(self.id)
# remove next line and the above upon deprecation of lastUpdateTime @property
self._properties["modifiedTime"] = metadata["modifiedTime"]
return metadata["modifiedTime"]
949 changes: 841 additions & 108 deletions tests/cassettes/CellTest.test_a1_value.json

Large diffs are not rendered by default.

967 changes: 853 additions & 114 deletions tests/cassettes/CellTest.test_define_named_range.json

Large diffs are not rendered by default.

963 changes: 851 additions & 112 deletions tests/cassettes/CellTest.test_delete_named_range.json

Large diffs are not rendered by default.

1,375 changes: 1,218 additions & 157 deletions tests/cassettes/CellTest.test_equality.json

Large diffs are not rendered by default.

1,389 changes: 1,228 additions & 161 deletions tests/cassettes/CellTest.test_merge_cells.json

Large diffs are not rendered by default.

1,276 changes: 1,129 additions & 147 deletions tests/cassettes/CellTest.test_numeric_value.json

Large diffs are not rendered by default.

834 changes: 744 additions & 90 deletions tests/cassettes/CellTest.test_properties.json

Large diffs are not rendered by default.

101 changes: 90 additions & 11 deletions tests/cassettes/ClientTest.test_access_non_existing_spreadsheet.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"x-goog-api-client": [
"cred-type/sa"
],
"x-identity-trust-boundary": [
"0"
],
"authorization": [
"<ACCESS_TOKEN>"
]
Expand All @@ -33,37 +36,113 @@
"message": "Not Found"
},
"headers": {
"Transfer-Encoding": [
"chunked"
"X-Frame-Options": [
"SAMEORIGIN"
],
"X-Content-Type-Options": [
"nosniff"
],
"Cache-Control": [
"private"
],
"X-XSS-Protection": [
"0"
],
"Date": [
"Thu, 20 Jul 2023 08:51:29 GMT"
],
"Content-Type": [
"application/json; charset=UTF-8"
],
"Server": [
"ESF"
],
"Transfer-Encoding": [
"chunked"
],
"Vary": [
"Origin",
"X-Origin",
"Referer"
],
"Alt-Svc": [
"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000"
],
"content-length": [
"114"
]
},
"body": {
"string": "{\n \"error\": {\n \"code\": 404,\n \"message\": \"Requested entity was not found.\",\n \"status\": \"NOT_FOUND\"\n }\n}\n"
}
}
},
{
"request": {
"method": "GET",
"uri": "https://sheets.googleapis.com/v4/spreadsheets/test?includeGridData=false",
"body": null,
"headers": {
"User-Agent": [
"python-requests/2.31.0"
],
"Accept-Encoding": [
"gzip, deflate"
],
"Accept": [
"*/*"
],
"Connection": [
"keep-alive"
],
"x-goog-api-client": [
"cred-type/sa"
],
"x-identity-trust-boundary": [
"0"
],
"authorization": [
"<ACCESS_TOKEN>"
]
}
},
"response": {
"status": {
"code": 404,
"message": "Not Found"
},
"headers": {
"Server": [
"ESF"
],
"Cache-Control": [
"private"
],
"Content-Type": [
"application/json; charset=UTF-8"
],
"Vary": [
"Origin",
"X-Origin",
"Referer"
],
"Alt-Svc": [
"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000"
],
"X-XSS-Protection": [
"0"
],
"X-Content-Type-Options": [
"nosniff"
],
"X-Frame-Options": [
"SAMEORIGIN"
],
"Date": [
"Thu, 08 Jun 2023 22:14:38 GMT"
"Transfer-Encoding": [
"chunked"
],
"Alt-Svc": [
"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000"
"Date": [
"Thu, 20 Jul 2023 08:54:49 GMT"
],
"Cache-Control": [
"private"
"X-Content-Type-Options": [
"nosniff"
],
"content-length": [
"114"
Expand Down
Loading

0 comments on commit 9746f8d

Please sign in to comment.