@@ -70,6 +70,8 @@ class Datastore:
7070 _api_root : str
7171 _api_is_dev : bool
7272
73+ Timeout = Union [int , float ]
74+
7375 def __init__ (
7476 self , project : Optional [str ] = None ,
7577 service_file : Optional [Union [str , IO [AnyStr ]]] = None ,
@@ -159,7 +161,7 @@ def make_mutation(
159161 async def allocateIds (
160162 self , keys : List [Key ],
161163 session : Optional [Session ] = None ,
162- timeout : int = 10 ,
164+ timeout : Timeout = 10 ,
163165 ) -> List [Key ]:
164166 project = await self .project ()
165167 url = f'{ self ._api_root } /projects/{ project } :allocateIds'
@@ -187,7 +189,7 @@ async def allocateIds(
187189 # TODO: support readwrite vs readonly transaction types
188190 async def beginTransaction (
189191 self , session : Optional [Session ] = None ,
190- timeout : int = 10 ,
192+ timeout : Timeout = 10 ,
191193 ) -> str :
192194 project = await self .project ()
193195 url = f'{ self ._api_root } /projects/{ project } :beginTransaction'
@@ -210,7 +212,7 @@ async def commit(
210212 transaction : Optional [str ] = None ,
211213 mode : Mode = Mode .TRANSACTIONAL ,
212214 session : Optional [Session ] = None ,
213- timeout : int = 10 ,
215+ timeout : Timeout = 10 ,
214216 ) -> Dict [str , Any ]:
215217 project = await self .project ()
216218 url = f'{ self ._api_root } /projects/{ project } :commit'
@@ -249,7 +251,7 @@ async def export(
249251 namespaces : Optional [List [str ]] = None ,
250252 labels : Optional [Dict [str , str ]] = None ,
251253 session : Optional [Session ] = None ,
252- timeout : int = 10 ,
254+ timeout : Timeout = 10 ,
253255 ) -> DatastoreOperation :
254256 project = await self .project ()
255257 url = f'{ self ._api_root } /projects/{ project } :export'
@@ -282,7 +284,7 @@ async def export(
282284 async def get_datastore_operation (
283285 self , name : str ,
284286 session : Optional [Session ] = None ,
285- timeout : int = 10 ,
287+ timeout : Timeout = 10 ,
286288 ) -> DatastoreOperation :
287289 url = f'{ self ._api_root } /{ name } '
288290
@@ -297,19 +299,21 @@ async def get_datastore_operation(
297299
298300 return self .datastore_operation_kind .from_repr (data )
299301
302+ # pylint: disable=too-many-locals
300303 # https://cloud.google.com/datastore/docs/reference/data/rest/v1/projects/lookup
301304 async def lookup (
302305 self , keys : List [Key ],
303306 transaction : Optional [str ] = None ,
304307 newTransaction : Optional [TransactionOptions ] = None ,
305308 consistency : Consistency = Consistency .STRONG ,
306- session : Optional [Session ] = None , timeout : int = 10 ,
309+ read_time : Optional [str ] = None ,
310+ session : Optional [Session ] = None , timeout : Timeout = 10 ,
307311 ) -> LookUpResult :
308312 project = await self .project ()
309313 url = f'{ self ._api_root } /projects/{ project } :lookup'
310314
311315 read_options = self ._build_read_options (
312- consistency , newTransaction , transaction )
316+ consistency , newTransaction , transaction , read_time )
313317
314318 payload = json .dumps ({
315319 'keys' : [k .to_repr () for k in keys ],
@@ -350,27 +354,35 @@ def _build_lookup_result(self, data: Dict[str, Any]) -> LookUpResult:
350354 if 'transaction' in data :
351355 new_transaction : str = data ['transaction' ]
352356 result ['transaction' ] = new_transaction
357+ if 'readTime' in data :
358+ read_time : str = data ['readTime' ]
359+ result ['readTime' ] = read_time
353360 return result
354361
355362 # https://cloud.google.com/datastore/docs/reference/data/rest/v1/ReadOptions
356363 def _build_read_options (self ,
357364 consistency : Consistency ,
358365 newTransaction : Optional [TransactionOptions ],
359- transaction : Optional [str ]) -> Dict [str , Any ]:
366+ transaction : Optional [str ],
367+ read_time : Optional [str ],
368+ ) -> Dict [str , Any ]:
360369 # TODO: expose ReadOptions directly to users
361370 if transaction :
362371 return {'transaction' : transaction }
363372
364373 if newTransaction :
365374 return {'newTransaction' : newTransaction .to_repr ()}
366375
376+ if read_time :
377+ return {'readTime' : read_time }
378+
367379 return {'readConsistency' : consistency .value }
368380
369381 # https://cloud.google.com/datastore/docs/reference/data/rest/v1/projects/reserveIds
370382 async def reserveIds (
371383 self , keys : List [Key ], database_id : str = '' ,
372384 session : Optional [Session ] = None ,
373- timeout : int = 10 ,
385+ timeout : Timeout = 10 ,
374386 ) -> None :
375387 project = await self .project ()
376388 url = f'{ self ._api_root } /projects/{ project } :reserveIds'
@@ -393,7 +405,7 @@ async def reserveIds(
393405 async def rollback (
394406 self , transaction : str ,
395407 session : Optional [Session ] = None ,
396- timeout : int = 10 ,
408+ timeout : Timeout = 10 ,
397409 ) -> None :
398410 project = await self .project ()
399411 url = f'{ self ._api_root } /projects/{ project } :rollback'
@@ -417,26 +429,26 @@ async def runQuery(
417429 self , query : BaseQuery ,
418430 explain_options : Optional [ExplainOptions ] = None ,
419431 transaction : Optional [str ] = None ,
432+ newTransaction : Optional [TransactionOptions ] = None ,
420433 consistency : Consistency = Consistency .EVENTUAL ,
434+ read_time : Optional [str ] = None ,
421435 session : Optional [Session ] = None ,
422- timeout : int = 10 ,
436+ timeout : Timeout = 10 ,
423437 ) -> QueryResult :
424438
425439 project = await self .project ()
426440 url = f'{ self ._api_root } /projects/{ project } :runQuery'
427441
428- if transaction :
429- options = {'transaction' : transaction }
430- else :
431- options = {'readConsistency' : consistency .value }
442+ read_options = self ._build_read_options (
443+ consistency , newTransaction , transaction , read_time )
432444
433445 payload_dict = {
434446 'partitionId' : {
435447 'projectId' : project ,
436448 'namespaceId' : self .namespace ,
437449 },
438450 query .json_key : query .to_repr (),
439- 'readOptions' : options ,
451+ 'readOptions' : read_options ,
440452 }
441453
442454 if explain_options :
0 commit comments