@@ -267,7 +267,7 @@ def delete_record(self, key: str) -> None:
267
267
timeout_secs = _SMALL_TIMEOUT ,
268
268
)
269
269
270
- def get_record_public_rul (self , key : str ) -> str :
270
+ def get_record_public_url (self , key : str ) -> str :
271
271
"""Generate a URL that can be used to access key-value store record.
272
272
273
273
If the client has permission to access the key-value store's URL signing key, the URL will include a signature
@@ -282,13 +282,20 @@ def get_record_public_rul(self, key: str) -> str:
282
282
if self .resource_id is None :
283
283
raise ValueError ('resource_id cannot be None when generating a public URL' )
284
284
285
- public_url = f'{ self ._api_public_base_url } /key-value-stores/{ self .resource_id } /records/{ key } '
286
- metadata = self .get_metadata ()
285
+ metadata = self .get ()
287
286
288
- if metadata .url_signing_secret_key is not None :
289
- public_url = public_url .with_query (signature = create_hmac_signature (metadata .url_signing_secret_key , key ))
287
+ request_params = self ._params ()
290
288
291
- return str (public_url )
289
+ if metadata and 'urlSigningSecretKey' in metadata :
290
+ request_params ['signature' ] = create_hmac_signature (metadata ['urlSigningSecretKey' ], key )
291
+
292
+ key_public_url = urlparse (self ._url (f'records/{ key } ' , public = True ))
293
+ filtered_params = {k : v for k , v in request_params .items () if v is not None }
294
+
295
+ if filtered_params :
296
+ key_public_url = key_public_url ._replace (query = urlencode (filtered_params ))
297
+
298
+ return urlunparse (key_public_url )
292
299
293
300
def create_keys_public_url (
294
301
self ,
@@ -313,7 +320,7 @@ def create_keys_public_url(
313
320
Returns:
314
321
The public key-value store keys URL.
315
322
"""
316
- store = self .get ()
323
+ metadata = self .get ()
317
324
318
325
request_params = self ._params (
319
326
limit = limit ,
@@ -322,10 +329,10 @@ def create_keys_public_url(
322
329
prefix = prefix ,
323
330
)
324
331
325
- if store and 'urlSigningSecretKey' in store :
332
+ if metadata and 'urlSigningSecretKey' in metadata :
326
333
signature = create_storage_content_signature (
327
- resource_id = store ['id' ],
328
- url_signing_secret_key = store ['urlSigningSecretKey' ],
334
+ resource_id = metadata ['id' ],
335
+ url_signing_secret_key = metadata ['urlSigningSecretKey' ],
329
336
expires_in_millis = expires_in_secs * 1000 if expires_in_secs is not None else None ,
330
337
)
331
338
request_params ['signature' ] = signature
@@ -578,6 +585,36 @@ async def delete_record(self, key: str) -> None:
578
585
timeout_secs = _SMALL_TIMEOUT ,
579
586
)
580
587
588
+ async def get_record_public_url (self , key : str ) -> str :
589
+ """Generate a URL that can be used to access key-value store record.
590
+
591
+ If the client has permission to access the key-value store's URL signing key, the URL will include a signature
592
+ to verify its authenticity.
593
+
594
+ Args:
595
+ key: The key for which the URL should be generated.
596
+
597
+ Returns:
598
+ A public URL that can be used to access the value of the given key in the KVS.
599
+ """
600
+ if self .resource_id is None :
601
+ raise ValueError ('resource_id cannot be None when generating a public URL' )
602
+
603
+ metadata = await self .get ()
604
+
605
+ request_params = self ._params ()
606
+
607
+ if metadata and 'urlSigningSecretKey' in metadata :
608
+ request_params ['signature' ] = create_hmac_signature (metadata ['urlSigningSecretKey' ], key )
609
+
610
+ key_public_url = urlparse (self ._url (f'records/{ key } ' , public = True ))
611
+ filtered_params = {k : v for k , v in request_params .items () if v is not None }
612
+
613
+ if filtered_params :
614
+ key_public_url = key_public_url ._replace (query = urlencode (filtered_params ))
615
+
616
+ return urlunparse (key_public_url )
617
+
581
618
async def create_keys_public_url (
582
619
self ,
583
620
* ,
@@ -601,7 +638,7 @@ async def create_keys_public_url(
601
638
Returns:
602
639
The public key-value store keys URL.
603
640
"""
604
- store = await self .get ()
641
+ metadata = await self .get ()
605
642
606
643
keys_public_url = urlparse (self ._url ('keys' ))
607
644
@@ -612,10 +649,10 @@ async def create_keys_public_url(
612
649
prefix = prefix ,
613
650
)
614
651
615
- if store and 'urlSigningSecretKey' in store :
652
+ if metadata and 'urlSigningSecretKey' in metadata :
616
653
signature = create_storage_content_signature (
617
- resource_id = store ['id' ],
618
- url_signing_secret_key = store ['urlSigningSecretKey' ],
654
+ resource_id = metadata ['id' ],
655
+ url_signing_secret_key = metadata ['urlSigningSecretKey' ],
619
656
expires_in_millis = expires_in_secs * 1000 if expires_in_secs is not None else None ,
620
657
)
621
658
request_params ['signature' ] = signature
0 commit comments