|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import os |
| 18 | +from typing import ( |
| 19 | + Optional, |
| 20 | + Sequence, |
| 21 | + Tuple, |
| 22 | + Union, |
| 23 | +) |
| 24 | + |
| 25 | +from google.cloud.bigquery_v2.services.centralized_service import _helpers |
| 26 | + |
| 27 | +# Import service client modules |
| 28 | +from google.cloud.bigquery_v2.services import ( |
| 29 | + dataset_service, |
| 30 | + job_service, |
| 31 | + model_service, |
| 32 | +) |
| 33 | + |
| 34 | +# Import types modules (to access *Requests classes) |
| 35 | +from google.cloud.bigquery_v2.types import ( |
| 36 | + dataset, |
| 37 | + job, |
| 38 | + model, |
| 39 | +) |
| 40 | + |
| 41 | +from google.api_core import client_options as client_options_lib |
| 42 | +from google.api_core import gapic_v1 |
| 43 | +from google.api_core import retry as retries |
| 44 | +from google.auth import credentials as auth_credentials |
| 45 | + |
| 46 | +# Create a type alias |
| 47 | +try: |
| 48 | + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None] |
| 49 | +except AttributeError: # pragma: NO COVER |
| 50 | + OptionalRetry = Union[retries.Retry, object, None] # type: ignore |
| 51 | + |
| 52 | +# TODO: This line is here to simplify prototyping, etc. |
| 53 | +PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT") |
| 54 | + |
| 55 | +DEFAULT_RETRY: OptionalRetry = gapic_v1.method.DEFAULT |
| 56 | +DEFAULT_TIMEOUT: Union[float, object] = gapic_v1.method.DEFAULT |
| 57 | +DEFAULT_METADATA: Sequence[Tuple[str, Union[str, bytes]]] = () |
| 58 | + |
| 59 | + |
| 60 | +# Create Centralized Client |
| 61 | +class BigQueryClient: |
| 62 | + def __init__( |
| 63 | + self, |
| 64 | + *, |
| 65 | + credentials: Optional[auth_credentials.Credentials] = None, |
| 66 | + client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None, |
| 67 | + ): |
| 68 | + self._clients = {} |
| 69 | + self._credentials = credentials |
| 70 | + self._client_options = client_options |
| 71 | + |
| 72 | + @property |
| 73 | + def dataset_service_client(self): |
| 74 | + if "dataset" not in self._clients: |
| 75 | + from google.cloud.bigquery_v2.services import dataset_service |
| 76 | + self._clients["dataset"] = dataset_service.DatasetServiceClient( |
| 77 | + credentials=self._credentials, client_options=self._client_options |
| 78 | + ) |
| 79 | + return self._clients["dataset"] |
| 80 | + |
| 81 | + @dataset_service_client.setter |
| 82 | + def dataset_service_client(self, value): |
| 83 | + # Check for the methods that this class most commonly uses (duck typing) |
| 84 | + required_methods = [ |
| 85 | + "get_dataset", |
| 86 | + "insert_dataset", |
| 87 | + "patch_dataset", |
| 88 | + "update_dataset", |
| 89 | + "delete_dataset", |
| 90 | + "list_datasets", |
| 91 | + "undelete_dataset", |
| 92 | + ] |
| 93 | + for method in required_methods: |
| 94 | + if not hasattr(value, method) or not callable(getattr(value, method)): |
| 95 | + raise AttributeError( |
| 96 | + f"Object assigned to dataset_service_client is missing a callable '{method}' method." |
| 97 | + ) |
| 98 | + self._clients["dataset"] = value |
| 99 | + |
| 100 | + @property |
| 101 | + def job_service_client(self): |
| 102 | + if "job" not in self._clients: |
| 103 | + from google.cloud.bigquery_v2.services import job_service |
| 104 | + self._clients["job"] = job_service.JobServiceClient( |
| 105 | + credentials=self._credentials, client_options=self._client_options |
| 106 | + ) |
| 107 | + return self._clients["job"] |
| 108 | + |
| 109 | + @job_service_client.setter |
| 110 | + def job_service_client(self, value): |
| 111 | + required_methods = [ |
| 112 | + "get_job", |
| 113 | + "insert_job", |
| 114 | + "cancel_job", |
| 115 | + "delete_job", |
| 116 | + "list_jobs", |
| 117 | + ] |
| 118 | + for method in required_methods: |
| 119 | + if not hasattr(value, method) or not callable(getattr(value, method)): |
| 120 | + raise AttributeError( |
| 121 | + f"Object assigned to job_service_client is missing a callable '{method}' method." |
| 122 | + ) |
| 123 | + self._clients["job"] = value |
| 124 | + |
| 125 | + @property |
| 126 | + def model_service_client(self): |
| 127 | + if "model" not in self._clients: |
| 128 | + from google.cloud.bigquery_v2.services import model_service |
| 129 | + self._clients["model"] = model_service.ModelServiceClient( |
| 130 | + credentials=self._credentials, client_options=self._client_options |
| 131 | + ) |
| 132 | + return self._clients["model"] |
| 133 | + |
| 134 | + @model_service_client.setter |
| 135 | + def model_service_client(self, value): |
| 136 | + required_methods = [ |
| 137 | + "get_model", |
| 138 | + "delete_model", |
| 139 | + "patch_model", |
| 140 | + "list_models", |
| 141 | + ] |
| 142 | + for method in required_methods: |
| 143 | + if not hasattr(value, method) or not callable(getattr(value, method)): |
| 144 | + raise AttributeError( |
| 145 | + f"Object assigned to model_service_client is missing a callable '{method}' method." |
| 146 | + ) |
| 147 | + self._clients["model"] = value |
| 148 | + |
| 149 | + def get_dataset( |
| 150 | + self, |
| 151 | + request: Optional[Union[dataset.GetDatasetRequest, dict]] = None, |
| 152 | + *, |
| 153 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 154 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 155 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 156 | + ): |
| 157 | + """ |
| 158 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 159 | + """ |
| 160 | + kwargs = _helpers._drop_self_key(locals()) |
| 161 | + return self.dataset_service_client.get_dataset(**kwargs) |
| 162 | + |
| 163 | + def list_datasets( |
| 164 | + self, |
| 165 | + request: Optional[Union[dataset.ListDatasetsRequest, dict]] = None, |
| 166 | + *, |
| 167 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 168 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 169 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 170 | + ): |
| 171 | + """ |
| 172 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 173 | + """ |
| 174 | + kwargs = _helpers._drop_self_key(locals()) |
| 175 | + return self.dataset_service_client.list_datasets(**kwargs) |
| 176 | + |
| 177 | + def list_jobs( |
| 178 | + self, |
| 179 | + request: Optional[Union[job.ListJobsRequest, dict]] = None, |
| 180 | + *, |
| 181 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 182 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 183 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 184 | + ): |
| 185 | + """ |
| 186 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 187 | + """ |
| 188 | + kwargs = _helpers._drop_self_key(locals()) |
| 189 | + return self.job_service_client.list_jobs(**kwargs) |
| 190 | + |
| 191 | + def get_model( |
| 192 | + self, |
| 193 | + request: Optional[Union[model.GetModelRequest, dict]] = None, |
| 194 | + *, |
| 195 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 196 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 197 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 198 | + ): |
| 199 | + """ |
| 200 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 201 | + """ |
| 202 | + kwargs = _helpers._drop_self_key(locals()) |
| 203 | + return self.model_service_client.get_model(**kwargs) |
| 204 | + |
| 205 | + def delete_model( |
| 206 | + self, |
| 207 | + request: Optional[Union[model.DeleteModelRequest, dict]] = None, |
| 208 | + *, |
| 209 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 210 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 211 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 212 | + ): |
| 213 | + """ |
| 214 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 215 | + """ |
| 216 | + kwargs = _helpers._drop_self_key(locals()) |
| 217 | + # The underlying GAPIC client returns None on success. |
| 218 | + return self.model_service_client.delete_model(**kwargs) |
| 219 | + |
| 220 | + def patch_model( |
| 221 | + self, |
| 222 | + request: Optional[Union[model.PatchModelRequest, dict]] = None, |
| 223 | + *, |
| 224 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 225 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 226 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 227 | + ): |
| 228 | + """ |
| 229 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 230 | + """ |
| 231 | + kwargs = _helpers._drop_self_key(locals()) |
| 232 | + return self.model_service_client.patch_model(**kwargs) |
| 233 | + |
| 234 | + def list_models( |
| 235 | + self, |
| 236 | + request: Optional[Union[model.ListModelsRequest, dict]] = None, |
| 237 | + *, |
| 238 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 239 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 240 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 241 | + ): |
| 242 | + """ |
| 243 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 244 | + """ |
| 245 | + kwargs = _helpers._drop_self_key(locals()) |
| 246 | + return self.model_service_client.list_models(**kwargs) |
0 commit comments