|
| 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 | +# Import service client modules |
| 26 | +from google.cloud.bigquery_v2.services import ( |
| 27 | + dataset_service, |
| 28 | + job_service, |
| 29 | + model_service, |
| 30 | +) |
| 31 | + |
| 32 | +# Import types modules (to access *Requests classes) |
| 33 | +from google.cloud.bigquery_v2.types import ( |
| 34 | + dataset, |
| 35 | + job, |
| 36 | + model, |
| 37 | +) |
| 38 | + |
| 39 | +from google.api_core import client_options as client_options_lib |
| 40 | +from google.api_core import gapic_v1 |
| 41 | +from google.api_core import retry as retries |
| 42 | +from google.auth import credentials as auth_credentials |
| 43 | + |
| 44 | +# Create a type alias |
| 45 | +try: |
| 46 | + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None] |
| 47 | +except AttributeError: # pragma: NO COVER |
| 48 | + OptionalRetry = Union[retries.Retry, object, None] # type: ignore |
| 49 | + |
| 50 | +# TODO: This line is here to simplify prototyping, etc. |
| 51 | +PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT") |
| 52 | + |
| 53 | +DEFAULT_RETRY: OptionalRetry = gapic_v1.method.DEFAULT |
| 54 | +DEFAULT_TIMEOUT: Union[float, object] = gapic_v1.method.DEFAULT |
| 55 | +DEFAULT_METADATA: Sequence[Tuple[str, Union[str, bytes]]] = () |
| 56 | + |
| 57 | + |
| 58 | +def _drop_self_key(kwargs): |
| 59 | + "Drops 'self' key from a given kwargs dict." |
| 60 | + |
| 61 | + if not isinstance(kwargs, dict): |
| 62 | + raise TypeError("kwargs must be a dict.") |
| 63 | + kwargs.pop("self", None) # Essentially a no-op if 'self' key does not exist |
| 64 | + return kwargs |
| 65 | + |
| 66 | + |
| 67 | +# Create Centralized Client |
| 68 | +class BigQueryClient: |
| 69 | + def __init__( |
| 70 | + self, |
| 71 | + *, |
| 72 | + credentials: Optional[auth_credentials.Credentials] = None, |
| 73 | + client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None, |
| 74 | + ): |
| 75 | + # *ServiceClient related initializations |
| 76 | + self.dataset_service_client = dataset_service.DatasetServiceClient( |
| 77 | + credentials=credentials, client_options=client_options |
| 78 | + ) |
| 79 | + self.job_service_client = job_service.JobServiceClient( |
| 80 | + credentials=credentials, client_options=client_options |
| 81 | + ) |
| 82 | + self.model_service_client = model_service.ModelServiceClient( |
| 83 | + credentials=credentials, client_options=client_options |
| 84 | + ) |
| 85 | + |
| 86 | + def get_dataset( |
| 87 | + self, |
| 88 | + request: Optional[Union[dataset.GetDatasetRequest, dict]] = None, |
| 89 | + *, |
| 90 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 91 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 92 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 93 | + ): |
| 94 | + """ |
| 95 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 96 | + """ |
| 97 | + kwargs = _drop_self_key(locals()) |
| 98 | + return self.dataset_service_client.get_dataset(**kwargs) |
| 99 | + |
| 100 | + def list_jobs( |
| 101 | + self, |
| 102 | + request: Optional[Union[job.ListJobsRequest, dict]] = None, |
| 103 | + *, |
| 104 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 105 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 106 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 107 | + ): |
| 108 | + """ |
| 109 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 110 | + """ |
| 111 | + kwargs = _drop_self_key(locals()) |
| 112 | + return self.job_service_client.list_jobs(**kwargs) |
| 113 | + |
| 114 | + def get_model( |
| 115 | + self, |
| 116 | + request: Optional[Union[model.GetModelRequest, dict]] = None, |
| 117 | + *, |
| 118 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 119 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 120 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 121 | + ): |
| 122 | + """ |
| 123 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 124 | + """ |
| 125 | + kwargs = _drop_self_key(locals()) |
| 126 | + return self.model_service_client.get_model(**kwargs) |
| 127 | + |
| 128 | + def delete_model( |
| 129 | + self, |
| 130 | + request: Optional[Union[model.DeleteModelRequest, dict]] = None, |
| 131 | + *, |
| 132 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 133 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 134 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 135 | + ): |
| 136 | + """ |
| 137 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 138 | + """ |
| 139 | + kwargs = _drop_self_key(locals()) |
| 140 | + # The underlying GAPIC client returns None on success. |
| 141 | + return self.model_service_client.delete_model(**kwargs) |
| 142 | + |
| 143 | + def patch_model( |
| 144 | + self, |
| 145 | + request: Optional[Union[model.PatchModelRequest, dict]] = None, |
| 146 | + *, |
| 147 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 148 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 149 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 150 | + ): |
| 151 | + """ |
| 152 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 153 | + """ |
| 154 | + kwargs = _drop_self_key(locals()) |
| 155 | + return self.model_service_client.patch_model(**kwargs) |
| 156 | + |
| 157 | + def list_models( |
| 158 | + self, |
| 159 | + request: Optional[Union[model.ListModelsRequest, dict]] = None, |
| 160 | + *, |
| 161 | + retry: OptionalRetry = DEFAULT_RETRY, |
| 162 | + timeout: Union[float, object] = DEFAULT_TIMEOUT, |
| 163 | + metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
| 164 | + ): |
| 165 | + """ |
| 166 | + TODO: Docstring is purposefully blank. microgenerator will add automatically. |
| 167 | + """ |
| 168 | + kwargs = _drop_self_key(locals()) |
| 169 | + return self.model_service_client.list_models(**kwargs) |
| 170 | + |
| 171 | + |
| 172 | +# =============================================== |
| 173 | +# Sample TODO: Relocate this to a samples file |
| 174 | +# =============================================== |
| 175 | + |
| 176 | +# Instantiate BQClient class |
| 177 | +bqclient = BigQueryClient() |
| 178 | + |
| 179 | +# Instantiate Request class |
| 180 | +get_dataset_request = dataset.GetDatasetRequest( |
| 181 | + project_id=PROJECT_ID, |
| 182 | + dataset_id="experimental", |
| 183 | +) |
| 184 | + |
| 185 | +# Generate response |
| 186 | +dataset = bqclient.get_dataset(request=get_dataset_request) |
| 187 | + |
| 188 | +# Display response |
| 189 | +print(f"GET DATASET:\n\t{dataset.id=}\n") |
0 commit comments