-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
23 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,34 @@ | ||
"""API to access user secrets.""" | ||
|
||
from google.colab._message import blocking_request as BlockingRequest | ||
from google.colab import _message | ||
|
||
|
||
def Get(*args): | ||
class NotebookAccessError(Exception): | ||
"""Exception thrown then the current notebook doesn't have access to the requested secret.""" | ||
|
||
def __init__(self, key): | ||
super().__init__(f'Notebook does not have access to secret {key}') | ||
|
||
|
||
def Get(key): | ||
"""Fetchets the value for specified secret keys. | ||
Args: | ||
*args: identifiers for the secret to fetch. | ||
key: Identifier of the secret to fetch. | ||
Returns: | ||
Stored secret | ||
Raises: | ||
NotebookAccessError: If the notebook does not have access to the requested | ||
secret. | ||
""" | ||
return BlockingRequest('GetSecret', request={'keys': args}, timeout_sec=None) | ||
resp = _message.blocking_request( | ||
'GetSecret', request={'key': key}, timeout_sec=None | ||
) | ||
access = resp.get('access', False) | ||
if not access: | ||
# TODO(b/294619193): Open the user secrets pane so that they can grant | ||
# access. | ||
raise NotebookAccessError(key) | ||
return resp.get('payload', '') |