|
16 | 16 |
|
17 | 17 | import os |
18 | 18 | from typing import ( |
| 19 | + Dict, |
19 | 20 | Optional, |
20 | 21 | Sequence, |
21 | 22 | Tuple, |
|
34 | 35 | # Import types modules (to access *Requests classes) |
35 | 36 | from google.cloud.bigquery_v2.types import ( |
36 | 37 | dataset, |
| 38 | + dataset_reference, |
37 | 39 | job, |
38 | 40 | model, |
39 | 41 | ) |
|
43 | 45 | from google.api_core import retry as retries |
44 | 46 | from google.auth import credentials as auth_credentials |
45 | 47 |
|
46 | | -# Create a type alias |
| 48 | +# Create type aliases |
47 | 49 | try: |
48 | 50 | OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None] |
49 | 51 | except AttributeError: # pragma: NO COVER |
50 | 52 | OptionalRetry = Union[retries.Retry, object, None] # type: ignore |
51 | 53 |
|
52 | | -# TODO: This line is here to simplify prototyping, etc. |
53 | | -PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT") |
| 54 | +DatasetIdentifier = Union[str, dataset_reference.DatasetReference] |
54 | 55 |
|
| 56 | +# TODO: This variable is here to simplify prototyping, etc. |
| 57 | +PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT") |
55 | 58 | DEFAULT_RETRY: OptionalRetry = gapic_v1.method.DEFAULT |
56 | 59 | DEFAULT_TIMEOUT: Union[float, object] = gapic_v1.method.DEFAULT |
57 | 60 | DEFAULT_METADATA: Sequence[Tuple[str, Union[str, bytes]]] = () |
58 | 61 |
|
59 | | - |
60 | 62 | # Create Centralized Client |
61 | 63 | class BigQueryClient: |
| 64 | + """A centralized client for BigQuery API.""" |
| 65 | + |
62 | 66 | def __init__( |
63 | 67 | self, |
64 | 68 | *, |
65 | 69 | credentials: Optional[auth_credentials.Credentials] = None, |
66 | 70 | client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None, |
67 | 71 | ): |
68 | | - self._clients = {} |
| 72 | + """ |
| 73 | + Initializes the BigQueryClient. |
| 74 | +
|
| 75 | + Args: |
| 76 | + credentials: |
| 77 | + The credentials to use for authentication. If not provided, the |
| 78 | + client will attempt to use the default credentials. |
| 79 | + client_options: |
| 80 | + A dictionary of client options to pass to the underlying |
| 81 | + service clients. |
| 82 | + """ |
| 83 | + |
| 84 | + self._clients: Dict[str, object] = {} |
69 | 85 | self._credentials = credentials |
70 | 86 | self._client_options = client_options |
| 87 | + self.project = PROJECT_ID |
| 88 | + |
| 89 | + # --- HELPER METHODS --- |
| 90 | + def _parse_dataset_path(self, dataset_path: str) -> Tuple[Optional[str], str]: |
| 91 | + """ |
| 92 | + Helper to parse project and dataset from a string identifier. |
| 93 | +
|
| 94 | + Args: |
| 95 | + dataset_path: A string in the format 'project_id.dataset_id' or |
| 96 | + 'dataset_id'. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + A tuple of (project_id, dataset_id). |
| 100 | + """ |
| 101 | + if "." in dataset_path: |
| 102 | + project_id, dataset_id = dataset_path.split(".", 1) |
| 103 | + return project_id, dataset_id |
| 104 | + return self.project, dataset_path |
| 105 | + |
| 106 | + def _parse_dataset_id_to_dict(self, dataset_id: DatasetIdentifier) -> dict: |
| 107 | + if isinstance(dataset_id, str): |
| 108 | + project_id, dataset_id_str = self._parse_dataset_path(dataset_id) |
| 109 | + return {"project_id": project_id, "dataset_id": dataset_id_str} |
| 110 | + elif isinstance(dataset_id, dataset_reference.DatasetReference): |
| 111 | + return { |
| 112 | + "project_id": dataset_id.project_id, |
| 113 | + "dataset_id": dataset_id.dataset_id, |
| 114 | + } |
| 115 | + else: |
| 116 | + raise TypeError(f"Invalid type for dataset_id: {type(dataset_id)}") |
71 | 117 |
|
72 | | -# --- SERVICE CLIENT ATTRIBUTES --- |
| 118 | + def _parse_project_id_to_dict(self, project_id: Optional[str] = None) -> dict: |
| 119 | + """Helper to create a request dictionary from a project_id.""" |
| 120 | + final_project_id = project_id or self.project |
| 121 | + return {"project_id": final_project_id} |
73 | 122 |
|
| 123 | + # --- *SERVICECLIENT ATTRIBUTES --- |
74 | 124 | @property |
75 | 125 | def dataset_service_client(self): |
76 | 126 | if "dataset" not in self._clients: |
77 | | - from google.cloud.bigquery_v2.services import dataset_service |
78 | | - |
79 | 127 | self._clients["dataset"] = dataset_service.DatasetServiceClient( |
80 | 128 | credentials=self._credentials, client_options=self._client_options |
81 | 129 | ) |
82 | 130 | return self._clients["dataset"] |
83 | 131 |
|
84 | 132 | @dataset_service_client.setter |
85 | 133 | def dataset_service_client(self, value): |
86 | | - # Check for the methods the centralized client exposes (to allow duck-typing) |
87 | | - required_methods = [ |
88 | | - "get_dataset", |
89 | | - "insert_dataset", |
90 | | - "patch_dataset", |
91 | | - "update_dataset", |
92 | | - "delete_dataset", |
93 | | - "list_datasets", |
94 | | - "undelete_dataset", |
95 | | - ] |
96 | | - for method in required_methods: |
97 | | - if not hasattr(value, method) or not callable(getattr(value, method)): |
98 | | - raise AttributeError( |
99 | | - f"Object assigned to dataset_service_client is missing a callable '{method}' method." |
100 | | - ) |
| 134 | + if not isinstance(value, dataset_service.DatasetServiceClient): |
| 135 | + raise TypeError( |
| 136 | + "Expected an instance of dataset_service.DatasetServiceClient." |
| 137 | + ) |
101 | 138 | self._clients["dataset"] = value |
102 | 139 |
|
103 | 140 | @property |
104 | 141 | def job_service_client(self): |
105 | 142 | if "job" not in self._clients: |
106 | | - from google.cloud.bigquery_v2.services import job_service |
107 | | - |
108 | 143 | self._clients["job"] = job_service.JobServiceClient( |
109 | 144 | credentials=self._credentials, client_options=self._client_options |
110 | 145 | ) |
111 | 146 | return self._clients["job"] |
112 | 147 |
|
113 | 148 | @job_service_client.setter |
114 | 149 | def job_service_client(self, value): |
115 | | - required_methods = [ |
116 | | - "get_job", |
117 | | - "insert_job", |
118 | | - "cancel_job", |
119 | | - "delete_job", |
120 | | - "list_jobs", |
121 | | - ] |
122 | | - for method in required_methods: |
123 | | - if not hasattr(value, method) or not callable(getattr(value, method)): |
124 | | - raise AttributeError( |
125 | | - f"Object assigned to job_service_client is missing a callable '{method}' method." |
126 | | - ) |
| 150 | + if not isinstance(value, job_service.JobServiceClient): |
| 151 | + raise TypeError("Expected an instance of job_service.JobServiceClient.") |
127 | 152 | self._clients["job"] = value |
128 | 153 |
|
129 | 154 | @property |
130 | 155 | def model_service_client(self): |
131 | 156 | if "model" not in self._clients: |
132 | | - from google.cloud.bigquery_v2.services import model_service |
133 | | - |
134 | 157 | self._clients["model"] = model_service.ModelServiceClient( |
135 | 158 | credentials=self._credentials, client_options=self._client_options |
136 | 159 | ) |
137 | 160 | return self._clients["model"] |
138 | 161 |
|
139 | 162 | @model_service_client.setter |
140 | 163 | def model_service_client(self, value): |
141 | | - required_methods = [ |
142 | | - "get_model", |
143 | | - "delete_model", |
144 | | - "patch_model", |
145 | | - "list_models", |
146 | | - ] |
147 | | - for method in required_methods: |
148 | | - if not hasattr(value, method) or not callable(getattr(value, method)): |
149 | | - raise AttributeError( |
150 | | - f"Object assigned to model_service_client is missing a callable '{method}' method." |
151 | | - ) |
| 164 | + if not isinstance(value, model_service.ModelServiceClient): |
| 165 | + raise TypeError("Expected an instance of model_service.ModelServiceClient.") |
152 | 166 | self._clients["model"] = value |
153 | 167 |
|
154 | | -# --- SERVICE CLIENT METHODS --- |
155 | | -# TODO: refactor the microgenerator template so that everything related to |
156 | | -# a single ServiceClient is kept close togetehr in the same section of this class: |
157 | | -# @property |
158 | | -# @setter |
159 | | -# _method_A() |
160 | | -# _method_B() |
161 | | -# _method_C() |
162 | | -# etc |
163 | | - |
| 168 | + # --- *SERVICECLIENT METHODS --- |
164 | 169 | def get_dataset( |
165 | 170 | self, |
166 | | - request: Optional[Union[dataset.GetDatasetRequest, dict]] = None, |
| 171 | + dataset_id: Optional[DatasetIdentifier] = None, |
167 | 172 | *, |
| 173 | + request: Optional["dataset.GetDatasetRequest"] = None, |
168 | 174 | retry: OptionalRetry = DEFAULT_RETRY, |
169 | 175 | timeout: Union[float, object] = DEFAULT_TIMEOUT, |
170 | 176 | metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
171 | | - ): |
| 177 | + ) -> "dataset.Dataset": |
172 | 178 | """ |
173 | 179 | TODO: Docstring is purposefully blank. microgenerator will add automatically. |
174 | 180 | """ |
175 | | - kwargs = _helpers._drop_self_key(locals()) |
176 | | - return self.dataset_service_client.get_dataset(**kwargs) |
| 181 | + final_request = _helpers._make_request( |
| 182 | + request_class=dataset.GetDatasetRequest, |
| 183 | + user_request=request, |
| 184 | + identifier_value=dataset_id, |
| 185 | + identifier_name="dataset_id", |
| 186 | + parser=self._parse_dataset_id_to_dict, |
| 187 | + identifier_required=True, |
| 188 | + ) |
| 189 | + |
| 190 | + return self.dataset_service_client.get_dataset( |
| 191 | + request=final_request, |
| 192 | + retry=retry, |
| 193 | + timeout=timeout, |
| 194 | + metadata=metadata, |
| 195 | + ) |
177 | 196 |
|
178 | 197 | def list_datasets( |
179 | 198 | self, |
180 | | - request: Optional[Union[dataset.ListDatasetsRequest, dict]] = None, |
| 199 | + project_id: Optional[str] = None, |
181 | 200 | *, |
| 201 | + request: Optional["dataset.ListDatasetsRequest"] = None, |
182 | 202 | retry: OptionalRetry = DEFAULT_RETRY, |
183 | 203 | timeout: Union[float, object] = DEFAULT_TIMEOUT, |
184 | 204 | metadata: Sequence[Tuple[str, Union[str, bytes]]] = DEFAULT_METADATA, |
185 | 205 | ): |
186 | 206 | """ |
187 | 207 | TODO: Docstring is purposefully blank. microgenerator will add automatically. |
188 | 208 | """ |
189 | | - kwargs = _helpers._drop_self_key(locals()) |
190 | | - return self.dataset_service_client.list_datasets(**kwargs) |
| 209 | + final_request = _helpers._make_request( |
| 210 | + request_class=dataset.ListDatasetsRequest, |
| 211 | + user_request=request, |
| 212 | + identifier_value=project_id, |
| 213 | + identifier_name="project_id", |
| 214 | + parser=self._parse_project_id_to_dict, |
| 215 | + identifier_required=False, |
| 216 | + ) |
| 217 | + |
| 218 | + return self.dataset_service_client.list_datasets( |
| 219 | + request=final_request, |
| 220 | + retry=retry, |
| 221 | + timeout=timeout, |
| 222 | + metadata=metadata, |
| 223 | + ) |
191 | 224 |
|
192 | 225 | def list_jobs( |
193 | 226 | self, |
|
0 commit comments