Skip to content

Commit 94d5f0c

Browse files
authored
feat: add 'from_service_account_info' factory to clients (#706)
Closes #705
1 parent f02a125 commit 94d5f0c

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
9797
DEFAULT_ENDPOINT
9898
)
9999

100+
@classmethod
101+
def from_service_account_info(cls, info: dict, *args, **kwargs):
102+
"""Creates an instance of this client using the provided credentials info.
103+
104+
Args:
105+
info (dict): The service account private key info.
106+
args: Additional arguments to pass to the constructor.
107+
kwargs: Additional arguments to pass to the constructor.
108+
109+
Returns:
110+
{@api.name}: The constructed client.
111+
"""
112+
credentials = service_account.Credentials.from_service_account_info(info)
113+
kwargs["credentials"] = credentials
114+
return cls(*args, **kwargs)
115+
100116
@classmethod
101117
def from_service_account_file(cls, filename: str, *args, **kwargs):
102118
"""Creates an instance of this client using the provided credentials

gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ def test__get_default_mtls_endpoint():
6161
assert {{ service.client_name }}._get_default_mtls_endpoint(non_googleapi) == non_googleapi
6262

6363

64+
def test_{{ service.client_name|snake_case }}_from_service_account_info():
65+
creds = credentials.AnonymousCredentials()
66+
with mock.patch.object(service_account.Credentials, 'from_service_account_info') as factory:
67+
factory.return_value = creds
68+
info = {"valid": True}
69+
client = {{ service.client_name }}.from_service_account_info(info)
70+
assert client.transport._credentials == creds
71+
72+
{% if service.host %}assert client.transport._host == '{{ service.host }}{% if ":" not in service.host %}:443{% endif %}'{% endif %}
73+
74+
6475
def test_{{ service.client_name|snake_case }}_from_service_account_file():
6576
creds = credentials.AnonymousCredentials()
6677
with mock.patch.object(service_account.Credentials, 'from_service_account_file') as factory:

gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class {{ service.async_client_name }}:
4848
parse_common_{{ resource_msg.message_type.resource_type|snake_case }}_path = staticmethod({{ service.client_name }}.parse_common_{{ resource_msg.message_type.resource_type|snake_case }}_path)
4949
{% endfor %}
5050

51+
from_service_account_info = {{ service.client_name }}.from_service_account_info
5152
from_service_account_file = {{ service.client_name }}.from_service_account_file
5253
from_service_account_json = from_service_account_file
5354

gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
113113
DEFAULT_ENDPOINT
114114
)
115115

116+
@classmethod
117+
def from_service_account_info(cls, info: dict, *args, **kwargs):
118+
"""Creates an instance of this client using the provided credentials info.
119+
120+
Args:
121+
info (dict): The service account private key info.
122+
args: Additional arguments to pass to the constructor.
123+
kwargs: Additional arguments to pass to the constructor.
124+
125+
Returns:
126+
{@api.name}: The constructed client.
127+
"""
128+
credentials = service_account.Credentials.from_service_account_info(info)
129+
kwargs["credentials"] = credentials
130+
return cls(*args, **kwargs)
131+
116132
@classmethod
117133
def from_service_account_file(cls, filename: str, *args, **kwargs):
118134
"""Creates an instance of this client using the provided credentials

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ def test__get_default_mtls_endpoint():
7070
assert {{ service.client_name }}._get_default_mtls_endpoint(non_googleapi) == non_googleapi
7171

7272

73+
def test_{{ service.client_name|snake_case }}_from_service_account_info():
74+
creds = credentials.AnonymousCredentials()
75+
with mock.patch.object(service_account.Credentials, 'from_service_account_info') as factory:
76+
factory.return_value = creds
77+
info = {"valid": True}
78+
client = {{ service.client_name }}.from_service_account_info(info)
79+
assert client.transport._credentials == creds
80+
81+
{% if service.host %}assert client.transport._host == '{{ service.host }}{% if ":" not in service.host %}:443{% endif %}'{% endif %}
82+
83+
7384
@pytest.mark.parametrize("client_class", [{{ service.client_name }}, {{ service.async_client_name }}])
7485
def test_{{ service.client_name|snake_case }}_from_service_account_file(client_class):
7586
creds = credentials.AnonymousCredentials()

0 commit comments

Comments
 (0)