| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5d5d7a8 commit 1f426ed
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | from google.auth import credentials | |
| 3 | 3 | from google.auth.exceptions import RefreshError | |
| 4 | 4 | from google.cloud import bigquery | |
| 5 | + from google.cloud.exceptions import Forbidden | ||
| 5 | 6 | from google.cloud.bigquery._http import Connection | |
| 6 | 7 | from kaggle_secrets import UserSecretsClient | |
| 7 | 8 | ||
@@ -39,6 +40,9 @@ def refresh(self, request): | |||
| 39 | 40 | client = UserSecretsClient() | |
| 40 | 41 | self.token, self.expiry = client.get_bigquery_access_token() | |
| 41 | 42 | except Exception as e: | |
| 43 | + if (not get_integrations().has_bigquery()): | ||
| 44 | + print( | ||
| 45 | + 'Please ensure you have selected a BigQuery account in the Kernels Settings sidebar.') | ||
| 42 | 46 | raise RefreshError('Unable to refresh access token.') from e | |
| 43 | 47 | ||
| 44 | 48 | ||
@@ -52,6 +56,16 @@ def __init__(self, client): | |||
| 52 | 56 | self._EXTRA_HEADERS["X-KAGGLE-PROXY-DATA"] = os.getenv( | |
| 53 | 57 | "KAGGLE_DATA_PROXY_TOKEN") | |
| 54 | 58 | ||
| 59 | + def api_request(self, *args, **kwargs): | ||
| 60 | + """Wrap Connection.api_request in order to handle errors gracefully. | ||
| 61 | + """ | ||
| 62 | + try: | ||
| 63 | + super().api_request(*args, **kwargs) | ||
| 64 | + except Forbidden as e: | ||
| 65 | + print("Permission denied using Kaggle's public BigQuery integration. " | ||
| 66 | + "Did you mean to select a BigQuery account in the Kernels Settings sidebar?") | ||
| 67 | + raise e | ||
| 68 | + | ||
| 55 | 69 | ||
| 56 | 70 | class PublicBigqueryClient(bigquery.client.Client): | |
| 57 | 71 | """A modified BigQuery client that routes requests using Kaggle's Data Proxy to provide free access to Public Datasets. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,14 +15,17 @@ | |||
| 15 | 15 | def monkeypatch_bq(bq_client, *args, **kwargs): | |
| 16 | 16 | specified_project = kwargs.get('project') | |
| 17 | 17 | specified_credentials = kwargs.get('credentials') | |
| 18 | - kernel_integrations = get_integrations() | ||
| 19 | - if specified_project is None and specified_credentials is None and not kernel_integrations.has_bigquery(): | ||
| 18 | + has_bigquery = get_integrations().has_bigquery() | ||
| 19 | + if specified_project is None and specified_credentials is None and not has_bigquery: | ||
| 20 | 20 | print("Using Kaggle's public dataset BigQuery integration.") | |
| 21 | 21 | return PublicBigqueryClient(*args, **kwargs) | |
| 22 | 22 | ||
| 23 | 23 | else: | |
| 24 | 24 | if specified_credentials is None: | |
| 25 | 25 | kwargs['credentials'] = KaggleKernelCredentials() | |
| 26 | + if (not has_bigquery): | ||
| 27 | + print('Please ensure you have selected a BigQuery ' | ||
| 28 | + 'account in the Kernels Settings sidebar.') | ||
| 26 | 29 | return bq_client(*args, **kwargs) | |
| 27 | 30 | ||
| 28 | 31 | # Monkey patches BigQuery client creation to use proxy or user-connected GCP account. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments