Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit 2498225

Browse files
feat: add from_service_account_info (#80)
fix: use correct retry deadlines
1 parent c653f48 commit 2498225

File tree

25 files changed

+1501
-633
lines changed

25 files changed

+1501
-633
lines changed

google/cloud/tasks_v2/services/cloud_tasks/async_client.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,36 @@ class CloudTasksAsyncClient:
8484
CloudTasksClient.parse_common_location_path
8585
)
8686

87-
from_service_account_info = CloudTasksClient.from_service_account_info
88-
from_service_account_file = CloudTasksClient.from_service_account_file
87+
@classmethod
88+
def from_service_account_info(cls, info: dict, *args, **kwargs):
89+
"""Creates an instance of this client using the provided credentials info.
90+
91+
Args:
92+
info (dict): The service account private key info.
93+
args: Additional arguments to pass to the constructor.
94+
kwargs: Additional arguments to pass to the constructor.
95+
96+
Returns:
97+
CloudTasksAsyncClient: The constructed client.
98+
"""
99+
return CloudTasksClient.from_service_account_info.__func__(CloudTasksAsyncClient, info, *args, **kwargs) # type: ignore
100+
101+
@classmethod
102+
def from_service_account_file(cls, filename: str, *args, **kwargs):
103+
"""Creates an instance of this client using the provided credentials
104+
file.
105+
106+
Args:
107+
filename (str): The path to the service account private key json
108+
file.
109+
args: Additional arguments to pass to the constructor.
110+
kwargs: Additional arguments to pass to the constructor.
111+
112+
Returns:
113+
CloudTasksAsyncClient: The constructed client.
114+
"""
115+
return CloudTasksClient.from_service_account_file.__func__(CloudTasksAsyncClient, filename, *args, **kwargs) # type: ignore
116+
89117
from_service_account_json = from_service_account_file
90118

91119
@property
@@ -217,6 +245,7 @@ async def list_queues(
217245
predicate=retries.if_exception_type(
218246
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
219247
),
248+
deadline=10.0,
220249
),
221250
default_timeout=10.0,
222251
client_info=DEFAULT_CLIENT_INFO,
@@ -308,6 +337,7 @@ async def get_queue(
308337
predicate=retries.if_exception_type(
309338
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
310339
),
340+
deadline=10.0,
311341
),
312342
default_timeout=10.0,
313343
client_info=DEFAULT_CLIENT_INFO,
@@ -610,6 +640,7 @@ async def delete_queue(
610640
predicate=retries.if_exception_type(
611641
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
612642
),
643+
deadline=10.0,
613644
),
614645
default_timeout=10.0,
615646
client_info=DEFAULT_CLIENT_INFO,
@@ -1010,6 +1041,7 @@ async def get_iam_policy(
10101041
predicate=retries.if_exception_type(
10111042
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
10121043
),
1044+
deadline=10.0,
10131045
),
10141046
default_timeout=10.0,
10151047
client_info=DEFAULT_CLIENT_INFO,
@@ -1251,6 +1283,7 @@ async def test_iam_permissions(
12511283
predicate=retries.if_exception_type(
12521284
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
12531285
),
1286+
deadline=10.0,
12541287
),
12551288
default_timeout=10.0,
12561289
client_info=DEFAULT_CLIENT_INFO,
@@ -1345,6 +1378,7 @@ async def list_tasks(
13451378
predicate=retries.if_exception_type(
13461379
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
13471380
),
1381+
deadline=10.0,
13481382
),
13491383
default_timeout=10.0,
13501384
client_info=DEFAULT_CLIENT_INFO,
@@ -1431,6 +1465,7 @@ async def get_task(
14311465
predicate=retries.if_exception_type(
14321466
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
14331467
),
1468+
deadline=10.0,
14341469
),
14351470
default_timeout=10.0,
14361471
client_info=DEFAULT_CLIENT_INFO,
@@ -1637,6 +1672,7 @@ async def delete_task(
16371672
predicate=retries.if_exception_type(
16381673
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
16391674
),
1675+
deadline=10.0,
16401676
),
16411677
default_timeout=10.0,
16421678
client_info=DEFAULT_CLIENT_INFO,

google/cloud/tasks_v2/services/cloud_tasks/client.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,13 +1195,13 @@ def get_iam_policy(
11951195
"the individual field arguments should be set."
11961196
)
11971197

1198-
# The request isn't a proto-plus wrapped type,
1199-
# so it must be constructed via keyword expansion.
12001198
if isinstance(request, dict):
1199+
# The request isn't a proto-plus wrapped type,
1200+
# so it must be constructed via keyword expansion.
12011201
request = iam_policy.GetIamPolicyRequest(**request)
1202-
12031202
elif not request:
1204-
request = iam_policy.GetIamPolicyRequest(resource=resource,)
1203+
# Null request, just make one.
1204+
request = iam_policy.GetIamPolicyRequest()
12051205

12061206
if resource is not None:
12071207
request.resource = resource
@@ -1334,13 +1334,13 @@ def set_iam_policy(
13341334
"the individual field arguments should be set."
13351335
)
13361336

1337-
# The request isn't a proto-plus wrapped type,
1338-
# so it must be constructed via keyword expansion.
13391337
if isinstance(request, dict):
1338+
# The request isn't a proto-plus wrapped type,
1339+
# so it must be constructed via keyword expansion.
13401340
request = iam_policy.SetIamPolicyRequest(**request)
1341-
13421341
elif not request:
1343-
request = iam_policy.SetIamPolicyRequest(resource=resource,)
1342+
# Null request, just make one.
1343+
request = iam_policy.SetIamPolicyRequest()
13441344

13451345
if resource is not None:
13461346
request.resource = resource
@@ -1424,19 +1424,20 @@ def test_iam_permissions(
14241424
"the individual field arguments should be set."
14251425
)
14261426

1427-
# The request isn't a proto-plus wrapped type,
1428-
# so it must be constructed via keyword expansion.
14291427
if isinstance(request, dict):
1428+
# The request isn't a proto-plus wrapped type,
1429+
# so it must be constructed via keyword expansion.
14301430
request = iam_policy.TestIamPermissionsRequest(**request)
1431-
14321431
elif not request:
1433-
request = iam_policy.TestIamPermissionsRequest(
1434-
resource=resource, permissions=permissions,
1435-
)
1432+
# Null request, just make one.
1433+
request = iam_policy.TestIamPermissionsRequest()
14361434

14371435
if resource is not None:
14381436
request.resource = resource
14391437

1438+
if permissions:
1439+
request.permissions.extend(permissions)
1440+
14401441
# Wrap the RPC method; this adds retry and timeout information,
14411442
# and friendly error handling.
14421443
rpc = self._transport._wrapped_methods[self._transport.test_iam_permissions]

google/cloud/tasks_v2/services/cloud_tasks/transports/base.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,20 @@ def __init__(
7474
scope (Optional[Sequence[str]]): A list of scopes.
7575
quota_project_id (Optional[str]): An optional project to use for billing
7676
and quota.
77-
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
78-
The client info used to send a user-agent string along with
79-
API requests. If ``None``, then default info will be used.
80-
Generally, you only need to set this if you're developing
77+
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
78+
The client info used to send a user-agent string along with
79+
API requests. If ``None``, then default info will be used.
80+
Generally, you only need to set this if you're developing
8181
your own client library.
8282
"""
8383
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
8484
if ":" not in host:
8585
host += ":443"
8686
self._host = host
8787

88+
# Save the scopes.
89+
self._scopes = scopes or self.AUTH_SCOPES
90+
8891
# If no credentials are provided, then determine the appropriate
8992
# defaults.
9093
if credentials and credentials_file:
@@ -94,20 +97,17 @@ def __init__(
9497

9598
if credentials_file is not None:
9699
credentials, _ = auth.load_credentials_from_file(
97-
credentials_file, scopes=scopes, quota_project_id=quota_project_id
100+
credentials_file, scopes=self._scopes, quota_project_id=quota_project_id
98101
)
99102

100103
elif credentials is None:
101104
credentials, _ = auth.default(
102-
scopes=scopes, quota_project_id=quota_project_id
105+
scopes=self._scopes, quota_project_id=quota_project_id
103106
)
104107

105108
# Save the credentials.
106109
self._credentials = credentials
107110

108-
# Lifted into its own function so it can be stubbed out during tests.
109-
self._prep_wrapped_messages(client_info)
110-
111111
def _prep_wrapped_messages(self, client_info):
112112
# Precompute the wrapped methods.
113113
self._wrapped_methods = {
@@ -120,6 +120,7 @@ def _prep_wrapped_messages(self, client_info):
120120
predicate=retries.if_exception_type(
121121
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
122122
),
123+
deadline=10.0,
123124
),
124125
default_timeout=10.0,
125126
client_info=client_info,
@@ -133,6 +134,7 @@ def _prep_wrapped_messages(self, client_info):
133134
predicate=retries.if_exception_type(
134135
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
135136
),
137+
deadline=10.0,
136138
),
137139
default_timeout=10.0,
138140
client_info=client_info,
@@ -152,6 +154,7 @@ def _prep_wrapped_messages(self, client_info):
152154
predicate=retries.if_exception_type(
153155
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
154156
),
157+
deadline=10.0,
155158
),
156159
default_timeout=10.0,
157160
client_info=client_info,
@@ -174,6 +177,7 @@ def _prep_wrapped_messages(self, client_info):
174177
predicate=retries.if_exception_type(
175178
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
176179
),
180+
deadline=10.0,
177181
),
178182
default_timeout=10.0,
179183
client_info=client_info,
@@ -190,6 +194,7 @@ def _prep_wrapped_messages(self, client_info):
190194
predicate=retries.if_exception_type(
191195
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
192196
),
197+
deadline=10.0,
193198
),
194199
default_timeout=10.0,
195200
client_info=client_info,
@@ -203,6 +208,7 @@ def _prep_wrapped_messages(self, client_info):
203208
predicate=retries.if_exception_type(
204209
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
205210
),
211+
deadline=10.0,
206212
),
207213
default_timeout=10.0,
208214
client_info=client_info,
@@ -216,6 +222,7 @@ def _prep_wrapped_messages(self, client_info):
216222
predicate=retries.if_exception_type(
217223
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
218224
),
225+
deadline=10.0,
219226
),
220227
default_timeout=10.0,
221228
client_info=client_info,
@@ -232,6 +239,7 @@ def _prep_wrapped_messages(self, client_info):
232239
predicate=retries.if_exception_type(
233240
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
234241
),
242+
deadline=10.0,
235243
),
236244
default_timeout=10.0,
237245
client_info=client_info,

0 commit comments

Comments
 (0)