Skip to content

Commit 07910c5

Browse files
committed
feat: adds _helpers.py.js template
1 parent 7318f0b commit 07910c5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Any, Dict, List, Optional, Type
2+
3+
4+
def _create_request(
5+
request_class: Type,
6+
path_identifier: str,
7+
expected_args: List[str],
8+
default_project_id: Optional[str] = None,
9+
) -> Any:
10+
"""
11+
Constructs a *Request object from a class, path_identifier, and expected args.
12+
13+
Args:
14+
request_class: The class of the request object to create (e.g., GetDatasetRequest).
15+
path_identifier: The dot-separated string of resource IDs.
16+
expected_args: An ordered list of the argument names the request object
17+
expects (e.g., ['project_id', 'dataset_id', 'table_id']).
18+
default_project_id: The default project ID to use if needed.
19+
20+
Returns:
21+
An instantiated request object.
22+
"""
23+
# Start of inlined parse_path_to_request_inputs
24+
segments = path_identifier.split(".")
25+
num_segments = len(segments)
26+
num_expected = len(expected_args)
27+
project_id_is_expected = "project_id" in expected_args
28+
29+
# Validate the number of segments.
30+
if not (
31+
num_segments == num_expected
32+
or (project_id_is_expected and num_segments == num_expected - 1)
33+
):
34+
raise ValueError(
35+
f"Invalid path identifier '{path_identifier}'. Expected "
36+
f"{num_expected} parts (or {num_expected - 1} if project_id is "
37+
f"omitted), but got {num_segments}."
38+
)
39+
40+
# If project_id is implicitly expected, use the default.
41+
if project_id_is_expected and num_segments == num_expected - 1:
42+
if not default_project_id:
43+
raise ValueError(
44+
f"Missing project_id in path '{path_identifier}' and no "
45+
"default_project_id was provided."
46+
)
47+
# Prepend the default project_id to the segments.
48+
segments.insert(0, default_project_id)
49+
50+
request_inputs = dict(zip(expected_args, segments))
51+
# End of inlined parse_path_to_request_inputs
52+
53+
# Instantiate the request object.
54+
return request_class(**request_inputs)

0 commit comments

Comments
 (0)