Skip to content

Commit a3117d8

Browse files
committed
feat: Add client.py.j2 template\n\nAdds the main Jinja2 template for generating the BigQueryClient class.
1 parent 510a87b commit a3117d8

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# TODO: Add a header if needed.
2+
3+
# ======== 🦕 HERE THERE BE DINOSAURS 🦖 =========
4+
# This content is subject to significant change. Not for review yet.
5+
# Included as a proof of concept for context or testing ONLY.
6+
# ================================================
7+
8+
# Imports
9+
import os
10+
11+
from typing import (
12+
Any,
13+
Dict,
14+
List,
15+
Optional,
16+
Sequence,
17+
Tuple,
18+
Union,
19+
)
20+
21+
# Service Client Imports
22+
{% for imp in service_imports %}{{ imp }}{% endfor %}
23+
24+
# Helper Imports
25+
from . import _helpers
26+
27+
# Type Imports
28+
{% for imp in type_imports %}{{ imp }}{% endfor %}
29+
30+
from google.api_core import client_options as client_options_lib
31+
from google.api_core import gapic_v1
32+
from google.api_core import retry as retries
33+
from google.auth import credentials as auth_credentials
34+
import google.auth
35+
36+
# Create type aliases
37+
try:
38+
OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None]
39+
except AttributeError: # pragma: NO COVER
40+
OptionalRetry = Union[retries.Retry, object, None] # type: ignore
41+
42+
DEFAULT_RETRY: OptionalRetry = gapic_v1.method.DEFAULT
43+
DEFAULT_TIMEOUT: Union[float, object] = gapic_v1.method.DEFAULT
44+
DEFAULT_METADATA: Sequence[Tuple[str, Union[str, bytes]]] = ()
45+
46+
{#- Create a mapping from the ServiceClient class name to its property name on the main client.
47+
e.g., {'DatasetServiceClient': 'dataset_service_client'}
48+
-#}
49+
{% set class_to_instance_map = {} %}
50+
{% for service in services %}
51+
{% set _ = class_to_instance_map.update({service.service_client_class: service.property_name}) %}
52+
{% endfor %}
53+
54+
55+
class BigQueryClient:
56+
def __init__(
57+
self,
58+
project: Optional[str] = None,
59+
credentials: Optional[auth_credentials.Credentials] = None,
60+
client_options: Optional[client_options_lib.ClientOptions] = None,
61+
):
62+
if credentials is None:
63+
credentials, project_id = google.auth.default()
64+
else:
65+
project_id = None # project_id is not available from non-default credentials
66+
67+
if project is None:
68+
project = project_id
69+
70+
self.project = project
71+
self._credentials = credentials
72+
self._client_options = client_options
73+
self._clients: Dict[str, Any] = {}
74+
75+
# --- *METHOD SECTION ---
76+
77+
{% for method in methods %}
78+
{% if method.request_id_args is not none and method.request_id_args|length > 0 %}
79+
{% filter indent(4, True) %}
80+
{% include 'partials/_method_with_request_builder.j2' %}
81+
{% endfilter %}
82+
{% else %}
83+
{% filter indent(4, True) %}
84+
{% include 'partials/_simple_passthrough_method.j2' %}
85+
{% endfilter %}
86+
{% endif %}
87+
88+
89+
{% endfor %}
90+
91+
{#- *ServiceClient Properties Section: methods to get/set service clients -#}
92+
# --- *SERVICECLIENT PROPERTIES ---
93+
{% for service in services %}
94+
@property
95+
def {{ service.property_name }}(self):
96+
if "{{ service.service_name }}" not in self._clients:
97+
self._clients["{{ service.service_name }}"] = {{ service.service_module_name }}.{{ service.service_client_class }}(
98+
credentials=self._credentials, client_options=self._client_options
99+
)
100+
return self._clients["{{ service.service_name }}"]
101+
102+
@{{ service.property_name }}.setter
103+
def {{ service.property_name }}(self, value):
104+
if not isinstance(value, {{ service.service_module_name }}.{{ service.service_client_class }}):
105+
raise TypeError(
106+
"Expected an instance of {{ service.service_client_class }}."
107+
)
108+
self._clients["{{ service.service_name }}"] = value
109+
110+
{% endfor %}
111+
112+
{#- Helper Section: methods included from partial template -#}
113+
{#- include "partials/_client_helpers.j2" #}
114+
115+
116+
# ======== 🦕 HERE THERE WERE DINOSAURS 🦖 =========
117+
# The above content is subject to significant change. Not for review yet.
118+
# Included as a proof of concept for context or testing ONLY.
119+
# ================================================

0 commit comments

Comments
 (0)