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

feat: add from_service_account_info #80

Merged
merged 4 commits into from Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 38 additions & 2 deletions google/cloud/tasks_v2/services/cloud_tasks/async_client.py
Expand Up @@ -84,8 +84,36 @@ class CloudTasksAsyncClient:
CloudTasksClient.parse_common_location_path
)

from_service_account_info = CloudTasksClient.from_service_account_info
from_service_account_file = CloudTasksClient.from_service_account_file
@classmethod
def from_service_account_info(cls, info: dict, *args, **kwargs):
"""Creates an instance of this client using the provided credentials info.

Args:
info (dict): The service account private key info.
args: Additional arguments to pass to the constructor.
kwargs: Additional arguments to pass to the constructor.

Returns:
CloudTasksAsyncClient: The constructed client.
"""
return CloudTasksClient.from_service_account_info.__func__(CloudTasksAsyncClient, info, *args, **kwargs) # type: ignore

@classmethod
def from_service_account_file(cls, filename: str, *args, **kwargs):
"""Creates an instance of this client using the provided credentials
file.

Args:
filename (str): The path to the service account private key json
file.
args: Additional arguments to pass to the constructor.
kwargs: Additional arguments to pass to the constructor.

Returns:
CloudTasksAsyncClient: The constructed client.
"""
return CloudTasksClient.from_service_account_file.__func__(CloudTasksAsyncClient, filename, *args, **kwargs) # type: ignore

from_service_account_json = from_service_account_file

@property
Expand Down Expand Up @@ -217,6 +245,7 @@ async def list_queues(
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=DEFAULT_CLIENT_INFO,
Expand Down Expand Up @@ -308,6 +337,7 @@ async def get_queue(
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=DEFAULT_CLIENT_INFO,
Expand Down Expand Up @@ -610,6 +640,7 @@ async def delete_queue(
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=DEFAULT_CLIENT_INFO,
Expand Down Expand Up @@ -1010,6 +1041,7 @@ async def get_iam_policy(
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=DEFAULT_CLIENT_INFO,
Expand Down Expand Up @@ -1251,6 +1283,7 @@ async def test_iam_permissions(
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=DEFAULT_CLIENT_INFO,
Expand Down Expand Up @@ -1345,6 +1378,7 @@ async def list_tasks(
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=DEFAULT_CLIENT_INFO,
Expand Down Expand Up @@ -1431,6 +1465,7 @@ async def get_task(
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=DEFAULT_CLIENT_INFO,
Expand Down Expand Up @@ -1637,6 +1672,7 @@ async def delete_task(
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=DEFAULT_CLIENT_INFO,
Expand Down
29 changes: 15 additions & 14 deletions google/cloud/tasks_v2/services/cloud_tasks/client.py
Expand Up @@ -1195,13 +1195,13 @@ def get_iam_policy(
"the individual field arguments should be set."
)

# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
request = iam_policy.GetIamPolicyRequest(**request)

elif not request:
request = iam_policy.GetIamPolicyRequest(resource=resource,)
# Null request, just make one.
request = iam_policy.GetIamPolicyRequest()

if resource is not None:
request.resource = resource
Expand Down Expand Up @@ -1334,13 +1334,13 @@ def set_iam_policy(
"the individual field arguments should be set."
)

# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
request = iam_policy.SetIamPolicyRequest(**request)

elif not request:
request = iam_policy.SetIamPolicyRequest(resource=resource,)
# Null request, just make one.
request = iam_policy.SetIamPolicyRequest()

if resource is not None:
request.resource = resource
Expand Down Expand Up @@ -1424,19 +1424,20 @@ def test_iam_permissions(
"the individual field arguments should be set."
)

# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
request = iam_policy.TestIamPermissionsRequest(**request)

elif not request:
request = iam_policy.TestIamPermissionsRequest(
resource=resource, permissions=permissions,
)
# Null request, just make one.
request = iam_policy.TestIamPermissionsRequest()

if resource is not None:
request.resource = resource

if permissions:
request.permissions.extend(permissions)

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = self._transport._wrapped_methods[self._transport.test_iam_permissions]
Expand Down
26 changes: 17 additions & 9 deletions google/cloud/tasks_v2/services/cloud_tasks/transports/base.py
Expand Up @@ -74,17 +74,20 @@ def __init__(
scope (Optional[Sequence[str]]): A list of scopes.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
"""
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
if ":" not in host:
host += ":443"
self._host = host

# Save the scopes.
self._scopes = scopes or self.AUTH_SCOPES

# If no credentials are provided, then determine the appropriate
# defaults.
if credentials and credentials_file:
Expand All @@ -94,20 +97,17 @@ def __init__(

if credentials_file is not None:
credentials, _ = auth.load_credentials_from_file(
credentials_file, scopes=scopes, quota_project_id=quota_project_id
credentials_file, scopes=self._scopes, quota_project_id=quota_project_id
)

elif credentials is None:
credentials, _ = auth.default(
scopes=scopes, quota_project_id=quota_project_id
scopes=self._scopes, quota_project_id=quota_project_id
)

# Save the credentials.
self._credentials = credentials

# Lifted into its own function so it can be stubbed out during tests.
self._prep_wrapped_messages(client_info)

def _prep_wrapped_messages(self, client_info):
# Precompute the wrapped methods.
self._wrapped_methods = {
Expand All @@ -120,6 +120,7 @@ def _prep_wrapped_messages(self, client_info):
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=client_info,
Expand All @@ -133,6 +134,7 @@ def _prep_wrapped_messages(self, client_info):
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=client_info,
Expand All @@ -152,6 +154,7 @@ def _prep_wrapped_messages(self, client_info):
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=client_info,
Expand All @@ -174,6 +177,7 @@ def _prep_wrapped_messages(self, client_info):
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=client_info,
Expand All @@ -190,6 +194,7 @@ def _prep_wrapped_messages(self, client_info):
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=client_info,
Expand All @@ -203,6 +208,7 @@ def _prep_wrapped_messages(self, client_info):
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=client_info,
Expand All @@ -216,6 +222,7 @@ def _prep_wrapped_messages(self, client_info):
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=client_info,
Expand All @@ -232,6 +239,7 @@ def _prep_wrapped_messages(self, client_info):
predicate=retries.if_exception_type(
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
deadline=10.0,
),
default_timeout=10.0,
client_info=client_info,
Expand Down