33#
44# Licensed under the Apache License, Version 2.0 (the "License");
55# you may not use this file except in compliance with the License.
6- # You may obtain a copy of the License at
6+ # You may obtain a copy of the License atp
77#
88# http://www.apache.org/licenses/LICENSE-2.0
99#
1414# limitations under the License.
1515#
1616
17+ from typing import Any , Callable , Optional , Type , TypeVar
18+
19+
20+ T = TypeVar ("T" )
21+
1722
1823def _drop_self_key (kwargs ):
19- "Drops 'self' key from a given kwargs dict."
24+ """ Drops 'self' key from a given kwargs dict."" "
2025
2126 if not isinstance (kwargs , dict ):
2227 raise TypeError ("kwargs must be a dict." )
@@ -25,13 +30,56 @@ def _drop_self_key(kwargs):
2530
2631
2732def _make_request (
28- request_class ,
29- user_request ,
30- identifier_value ,
33+ request_class : Type [ T ] ,
34+ user_request : Optional [ T ] ,
35+ identifier_value : Optional [ Any ] ,
3136 identifier_name : str ,
32- parser ,
37+ parser : Callable [..., Any ] ,
3338 identifier_required : bool = True ,
34- ):
39+ ) -> T :
40+ """A factory for creating *Request objects.
41+
42+ This function simplifies the creation of request objects by extracting identifier
43+ values from strings (e.g., 'project_id.dataset_id') and using them to instantiate
44+ the appropriate request object. It allows users to continue using identifier
45+ strings with BigQueryClient methods, even though the underlying *ServiceClient
46+ methods do not directly support this convenience.
47+
48+ For example, this helper is used in methods like:
49+ - BigQueryClient.get_dataset()
50+ - BigQueryClient.delete_dataset()
51+
52+ Args:
53+ request_class: The class of the request object to create (e.g.,
54+ GetDatasetRequest, ListModelsRequest).
55+ user_request: A user-provided *Request object. If not None, this
56+ object is returned directly.
57+ identifier_value: The value to be parsed to create the request object
58+ (e.g., a dataset_id for GetDatasetRequest).
59+ identifier_name: The name of the identifier field (e.g., 'dataset_id',
60+ 'job_id').
61+ parser: A callable that takes the identifier_value and returns a dict
62+ of fields for the request object. For example, a parser could
63+ separate a 'project_id.dataset_id' string into its components.
64+ identifier_required: Whether the identifier is required. Defaults to True.
65+
66+ Returns:
67+ A *Request object.
68+
69+ Raises:
70+ ValueError: If both user_request and identifier_value are provided, or
71+ if identifier_required is True and both are None.
72+
73+ Example:
74+ request = _make_request(
75+ request_class=dataset.GetDatasetRequest,
76+ user_request=request,
77+ identifier_value=dataset_id,
78+ identifier_name="dataset_id",
79+ parser=self._parse_dataset_id_to_dict,
80+ identifier_required=True,
81+ )
82+ """
3583 if user_request is not None and identifier_value is not None :
3684 raise ValueError (
3785 f"Provide either a request object or '{ identifier_name } ', not both."
0 commit comments