| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | 6 | class KaggleDatasets: | |
| 7 | 7 | GET_GCS_PATH_ENDPOINT = '/requests/CopyDatasetVersionToKnownGcsBucketRequest' | |
| 8 | + TIMEOUT_SECS = 600 | ||
| 8 | 9 | ||
| 9 | 10 | # Integration types for GCS | |
| 10 | 11 | AUTO_ML = 1 | |
@@ -20,5 +21,5 @@ def get_gcs_path(self, dataset_dir: str = None) -> str: | |||
| 20 | 21 | 'MountSlug': dataset_dir, | |
| 21 | 22 | 'IntegrationType': integration_type, | |
| 22 | 23 | } | |
| 23 | - result = self.web_client.make_post_request(data, self.GET_GCS_PATH_ENDPOINT) | ||
| 24 | + result = self.web_client.make_post_request(data, self.GET_GCS_PATH_ENDPOINT, self.TIMEOUT_SECS) | ||
| 24 | 25 | return result['destinationBucket'] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,28 +4,12 @@ | |||
| 4 | 4 | (ie. BigQuery). | |
| 5 | 5 | """ | |
| 6 | 6 | ||
| 7 | - import json | ||
| 8 | 7 | import os | |
| 9 | - import socket | ||
| 10 | - import urllib.request | ||
| 11 | 8 | from datetime import datetime, timedelta | |
| 12 | 9 | from enum import Enum, unique | |
| 13 | 10 | from typing import Optional, Tuple | |
| 14 | - from urllib.error import HTTPError, URLError | ||
| 15 | - | ||
| 16 | - _KAGGLE_DEFAULT_URL_BASE = "https://www.kaggle.com" | ||
| 17 | - _KAGGLE_URL_BASE_ENV_VAR_NAME = "KAGGLE_URL_BASE" | ||
| 18 | - _KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME = "KAGGLE_USER_SECRETS_TOKEN" | ||
| 19 | - TIMEOUT_SECS = 40 | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - class CredentialError(Exception): | ||
| 23 | - pass | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - class BackendError(Exception): | ||
| 27 | - pass | ||
| 28 | - | ||
| 11 | + from kaggle_web_client import KaggleWebClient | ||
| 12 | + from kaggle_web_client import (CredentialError, BackendError) | ||
| 29 | 13 | ||
| 30 | 14 | class ValidationError(Exception): | |
| 31 | 15 | pass | |
@@ -56,48 +40,9 @@ def service(self): | |||
| 56 | 40 | class UserSecretsClient(): | |
| 57 | 41 | GET_USER_SECRET_ENDPOINT = '/requests/GetUserSecretRequest' | |
| 58 | 42 | GET_USER_SECRET_BY_LABEL_ENDPOINT = '/requests/GetUserSecretByLabelRequest' | |
| 59 | - BIGQUERY_TARGET_VALUE = 1 | ||
| 60 | 43 | ||
| 61 | 44 | def __init__(self): | |
| 62 | - url_base_override = os.getenv(_KAGGLE_URL_BASE_ENV_VAR_NAME) | ||
| 63 | - self.url_base = url_base_override or _KAGGLE_DEFAULT_URL_BASE | ||
| 64 | - # Follow the OAuth 2.0 Authorization standard (https://tools.ietf.org/html/rfc6750) | ||
| 65 | - self.jwt_token = os.getenv(_KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME) | ||
| 66 | - if self.jwt_token is None: | ||
| 67 | - raise CredentialError( | ||
| 68 | - 'A JWT Token is required to use the UserSecretsClient, ' | ||
| 69 | - f'but none found in environment variable {_KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME}') | ||
| 70 | - self.headers = {'Content-type': 'application/json'} | ||
| 71 | - | ||
| 72 | - def _make_post_request(self, data: dict, endpoint: str = GET_USER_SECRET_ENDPOINT) -> dict: | ||
| 73 | - # TODO(b/148309982) This code and the code in the constructor should be | ||
| 74 | - # removed and this class should use the new KaggleWebClient class instead. | ||
| 75 | - url = f'{self.url_base}{endpoint}' | ||
| 76 | - request_body = dict(data) | ||
| 77 | - request_body['JWE'] = self.jwt_token | ||
| 78 | - req = urllib.request.Request(url, headers=self.headers, data=bytes( | ||
| 79 | - json.dumps(request_body), encoding="utf-8")) | ||
| 80 | - try: | ||
| 81 | - with urllib.request.urlopen(req, timeout=TIMEOUT_SECS) as response: | ||
| 82 | - response_json = json.loads(response.read()) | ||
| 83 | - if not response_json.get('wasSuccessful') or 'result' not in response_json: | ||
| 84 | - raise BackendError( | ||
| 85 | - f'Unexpected response from the service. Response: {response_json}.') | ||
| 86 | - return response_json['result'] | ||
| 87 | - except (URLError, socket.timeout) as e: | ||
| 88 | - if isinstance( | ||
| 89 | - e, socket.timeout) or isinstance( | ||
| 90 | - e.reason, socket.timeout): | ||
| 91 | - raise ConnectionError( | ||
| 92 | - 'Timeout error trying to communicate with service. Please ensure internet is on.') from e | ||
| 93 | - raise ConnectionError( | ||
| 94 | - 'Connection error trying to communicate with service.') from e | ||
| 95 | - except HTTPError as e: | ||
| 96 | - if e.code == 401 or e.code == 403: | ||
| 97 | - raise CredentialError( | ||
| 98 | - f'Service responded with error code {e.code}.' | ||
| 99 | - ' Please ensure you have access to the resource.') from e | ||
| 100 | - raise BackendError('Unexpected response from the service.') from e | ||
| 45 | + self.web_client = KaggleWebClient() | ||
| 101 | 46 | ||
| 102 | 47 | def get_secret(self, label) -> str: | |
| 103 | 48 | """Retrieves a user secret value by its label. | |
@@ -113,7 +58,7 @@ def get_secret(self, label) -> str: | |||
| 113 | 58 | request_body = { | |
| 114 | 59 | 'Label': label, | |
| 115 | 60 | } | |
| 116 | - response_json = self._make_post_request(request_body, self.GET_USER_SECRET_BY_LABEL_ENDPOINT) | ||
| 61 | + response_json = self.web_client.make_post_request(request_body, self.GET_USER_SECRET_BY_LABEL_ENDPOINT) | ||
| 117 | 62 | if 'secret' not in response_json: | |
| 118 | 63 | raise BackendError( | |
| 119 | 64 | f'Unexpected response from the service. Response: {response_json}') | |
@@ -174,7 +119,7 @@ def _get_access_token(self, target: GcpTarget) -> Tuple[str, Optional[datetime]] | |||
| 174 | 119 | request_body = { | |
| 175 | 120 | 'Target': target.target | |
| 176 | 121 | } | |
| 177 | - response_json = self._make_post_request(request_body) | ||
| 122 | + response_json = self.web_client.make_post_request(request_body, self.GET_USER_SECRET_ENDPOINT) | ||
| 178 | 123 | if 'secret' not in response_json: | |
| 179 | 124 | raise BackendError( | |
| 180 | 125 | f'Unexpected response from the service. Response: {response_json}') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,17 +2,22 @@ | |||
| 2 | 2 | import os | |
| 3 | 3 | import socket | |
| 4 | 4 | import urllib.request | |
| 5 | - from datetime import datetime, timedelta | ||
| 6 | - from enum import Enum, unique | ||
| 7 | - from typing import Optional, Tuple | ||
| 8 | 5 | from urllib.error import HTTPError, URLError | |
| 9 | - from kaggle_secrets import (_KAGGLE_DEFAULT_URL_BASE, | ||
| 10 | - _KAGGLE_URL_BASE_ENV_VAR_NAME, | ||
| 11 | - _KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME, | ||
| 12 | - CredentialError, BackendError, ValidationError) | ||
| 6 | + | ||
| 7 | + _KAGGLE_DEFAULT_URL_BASE = "https://www.kaggle.com" | ||
| 8 | + _KAGGLE_URL_BASE_ENV_VAR_NAME = "KAGGLE_URL_BASE" | ||
| 9 | + _KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME = "KAGGLE_USER_SECRETS_TOKEN" | ||
| 10 | + TIMEOUT_SECS = 40 | ||
| 11 | + | ||
| 12 | + class CredentialError(Exception): | ||
| 13 | + pass | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + class BackendError(Exception): | ||
| 17 | + pass | ||
| 18 | + | ||
| 13 | 19 | ||
| 14 | 20 | class KaggleWebClient: | |
| 15 | - TIMEOUT_SECS = 600 | ||
| 16 | 21 | ||
| 17 | 22 | def __init__(self): | |
| 18 | 23 | url_base_override = os.getenv(_KAGGLE_URL_BASE_ENV_VAR_NAME) | |
@@ -29,14 +34,14 @@ def __init__(self): | |||
| 29 | 34 | 'X-Kaggle-Authorization': f'Bearer {self.jwt_token}', | |
| 30 | 35 | } | |
| 31 | 36 | ||
| 32 | - def make_post_request(self, data: dict, endpoint: str) -> dict: | ||
| 37 | + def make_post_request(self, data: dict, endpoint: str, timeout: int = TIMEOUT_SECS) -> dict: | ||
| 33 | 38 | url = f'{self.url_base}{endpoint}' | |
| 34 | 39 | request_body = dict(data) | |
| 35 | 40 | request_body['JWE'] = self.jwt_token | |
| 36 | 41 | req = urllib.request.Request(url, headers=self.headers, data=bytes( | |
| 37 | 42 | json.dumps(request_body), encoding="utf-8")) | |
| 38 | 43 | try: | |
| 39 | - with urllib.request.urlopen(req, timeout=self.TIMEOUT_SECS) as response: | ||
| 44 | + with urllib.request.urlopen(req, timeout=timeout) as response: | ||
| 40 | 45 | response_json = json.loads(response.read()) | |
| 41 | 46 | if not response_json.get('wasSuccessful') or 'result' not in response_json: | |
| 42 | 47 | raise BackendError( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,10 +6,10 @@ | |||
| 6 | 6 | from test.support import EnvironmentVarGuard | |
| 7 | 7 | from urllib.parse import urlparse | |
| 8 | 8 | ||
| 9 | - from kaggle_secrets import (_KAGGLE_URL_BASE_ENV_VAR_NAME, | ||
| 9 | + from kaggle_web_client import (KaggleWebClient, | ||
| 10 | + _KAGGLE_URL_BASE_ENV_VAR_NAME, | ||
| 10 | 11 | _KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME, | |
| 11 | - CredentialError, BackendError, ValidationError) | ||
| 12 | - from kaggle_web_client import KaggleWebClient | ||
| 12 | + CredentialError, BackendError) | ||
| 13 | 13 | from kaggle_datasets import KaggleDatasets, _KAGGLE_TPU_NAME_ENV_VAR_NAME | |
| 14 | 14 | ||
| 15 | 15 | _TEST_JWT = 'test-secrets-key' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,10 +10,11 @@ | |||
| 10 | 10 | ||
| 11 | 11 | from google.auth.exceptions import DefaultCredentialsError | |
| 12 | 12 | from google.cloud import bigquery | |
| 13 | - from kaggle_secrets import (_KAGGLE_URL_BASE_ENV_VAR_NAME, | ||
| 13 | + from kaggle_secrets import (GcpTarget, UserSecretsClient, | ||
| 14 | + NotFoundError, ValidationError) | ||
| 15 | + from kaggle_web_client import (_KAGGLE_URL_BASE_ENV_VAR_NAME, | ||
| 14 | 16 | _KAGGLE_USER_SECRETS_TOKEN_ENV_VAR_NAME, | |
| 15 | - CredentialError, GcpTarget, UserSecretsClient, | ||
| 16 | - BackendError, NotFoundError, ValidationError) | ||
| 17 | + CredentialError, BackendError) | ||
| 17 | 18 | ||
| 18 | 19 | _TEST_JWT = 'test-secrets-key' | |
| 19 | 20 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments