| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,8 @@ | |||
| 1 | - import os | ||
| 2 | 1 | from google.auth import credentials | |
| 3 | 2 | from google.cloud import bigquery | |
| 4 | 3 | from google.cloud.bigquery._http import Connection | |
| 4 | + import os | ||
| 5 | + | ||
| 5 | 6 | ||
| 6 | 7 | class KaggleKernelCredentials(credentials.Credentials): | |
| 7 | 8 | def refresh(self, request): | |
@@ -11,16 +12,18 @@ def refresh(self, request): | |||
| 11 | 12 | ||
| 12 | 13 | kaggle_proxy_token = os.getenv("KAGGLE_DATA_PROXY_TOKEN") | |
| 13 | 14 | CONNECTION_BASE_URL = Connection.API_BASE_URL | |
| 15 | + | ||
| 16 | + | ||
| 14 | 17 | def monkeypatch_bq(bq_client, *args, **kwargs): | |
| 15 | 18 | data_proxy_project = os.getenv("KAGGLE_DATA_PROXY_PROJECT") | |
| 16 | 19 | bq_user_jwt = os.getenv("KAGGLE_BQ_USER_JWT") | |
| 17 | - specified_project = kwargs.get('project').lower() if 'project' in kwargs else None | ||
| 20 | + specified_project = kwargs['project'] if 'project' in kwargs else None | ||
| 18 | 21 | # Use Data Proxy if user has specified to use the Kaggle project, or if | |
| 19 | 22 | # there are no connected GCP accounts (to maintain backwards compatibility). | |
| 20 | - if bq_user_jwt is None and specified_project and specified_project != 'kaggle': | ||
| 23 | + if bq_user_jwt is None and specified_project and specified_project.lower() != 'kaggle': | ||
| 21 | 24 | raise Exception("In order to query a private BigQuery project, please connect a GCP account. " | |
| 22 | 25 | "Otherwise specify 'kaggle' as the project to use Kaggle's public dataset BigQuery integration.") | |
| 23 | - use_data_proxy = specified_project == 'kaggle' or bq_user_jwt is None | ||
| 26 | + use_data_proxy = (specified_project and specified_project.lower() == 'kaggle') or bq_user_jwt is None | ||
| 24 | 27 | if use_data_proxy: | |
| 25 | 28 | if data_proxy_project is None or kaggle_proxy_token is None: | |
| 26 | 29 | # We don't have the data proxy info so leave the bq client unmodified. | |
@@ -43,7 +46,6 @@ def monkeypatch_bq(bq_client, *args, **kwargs): | |||
| 43 | 46 | return bq_client(*args, **kwargs) | |
| 44 | 47 | print("Using enabled BigQuery integration.") | |
| 45 | 48 | kwargs['credentials'] = KaggleKernelCredentials() | |
| 46 | - kwargs['project'] = kwargs.get('project') | ||
| 47 | 49 | return bq_client( | |
| 48 | 50 | *args, | |
| 49 | 51 | **kwargs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,18 +36,24 @@ def do_GET(s): | |||
| 36 | 36 | ||
| 37 | 37 | httpd.shutdown() | |
| 38 | 38 | if should_use_proxy: | |
| 39 | - self.assertTrue(HTTPHandler.called, msg="Fake server did not recieve a request from the BQ client.") | ||
| 39 | + self.assertTrue(HTTPHandler.called, msg="Fake server did not receive a request from the BQ client.") | ||
| 40 | 40 | self.assertTrue(HTTPHandler.header_found, msg="X-KAGGLE-PROXY-DATA header was missing from the BQ request.") | |
| 41 | 41 | else: | |
| 42 | 42 | self.assertFalse(HTTPHandler.called, msg="Fake server was called from the BQ client, but should not have been.") | |
| 43 | 43 | ||
| 44 | 44 | def test_proxy_kaggle_project(self): | |
| 45 | - client = bigquery.Client(project='KAGGLE') | ||
| 46 | - self._test_proxy(client, should_use_proxy=True) | ||
| 45 | + env = EnvironmentVarGuard() | ||
| 46 | + env.unset('KAGGLE_BQ_USER_JWT') | ||
| 47 | + with env: | ||
| 48 | + client = bigquery.Client(project='KAGGLE') | ||
| 49 | + self._test_proxy(client, should_use_proxy=True) | ||
| 47 | 50 | ||
| 48 | 51 | def test_proxy_no_project(self): | |
| 49 | - client = bigquery.Client() | ||
| 50 | - self._test_proxy(client, should_use_proxy=True) | ||
| 52 | + env = EnvironmentVarGuard() | ||
| 53 | + env.unset('KAGGLE_BQ_USER_JWT') | ||
| 54 | + with env: | ||
| 55 | + client = bigquery.Client() | ||
| 56 | + self._test_proxy(client, should_use_proxy=True) | ||
| 51 | 57 | ||
| 52 | 58 | def test_project_with_connected_account(self): | |
| 53 | 59 | env = EnvironmentVarGuard() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments