Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
feat: add common resource helper methods; expose client transport rem…
Browse files Browse the repository at this point in the history
…ove gRPC send/recv limits (#57)
  • Loading branch information
yoshi-automation committed Dec 23, 2020
1 parent 3665b08 commit 6f17871
Show file tree
Hide file tree
Showing 90 changed files with 5,078 additions and 1,190 deletions.
1 change: 1 addition & 0 deletions docs/talent_v4/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Types for Google Cloud Talent v4 API

.. automodule:: google.cloud.talent_v4.types
:members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/talent_v4beta1/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Types for Google Cloud Talent v4beta1 API

.. automodule:: google.cloud.talent_v4beta1.types
:members:
:show-inheritance:
4 changes: 2 additions & 2 deletions google/cloud/talent_v4/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
"DeleteTenantRequest",
"DeviceInfo",
"EmploymentType",
"EventServiceClient",
"GetCompanyRequest",
"GetJobRequest",
"GetTenantRequest",
Expand Down Expand Up @@ -144,10 +143,11 @@
"SearchJobsResponse",
"SpellingCorrection",
"Tenant",
"TenantServiceClient",
"TimestampRange",
"UpdateCompanyRequest",
"UpdateJobRequest",
"UpdateTenantRequest",
"Visibility",
"TenantServiceClient",
"EventServiceClient",
)
61 changes: 53 additions & 8 deletions google/cloud/talent_v4/services/company_service/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,50 @@ class CompanyServiceAsyncClient:

company_path = staticmethod(CompanyServiceClient.company_path)
parse_company_path = staticmethod(CompanyServiceClient.parse_company_path)
tenant_path = staticmethod(CompanyServiceClient.tenant_path)
parse_tenant_path = staticmethod(CompanyServiceClient.parse_tenant_path)

common_billing_account_path = staticmethod(
CompanyServiceClient.common_billing_account_path
)
parse_common_billing_account_path = staticmethod(
CompanyServiceClient.parse_common_billing_account_path
)

common_folder_path = staticmethod(CompanyServiceClient.common_folder_path)
parse_common_folder_path = staticmethod(
CompanyServiceClient.parse_common_folder_path
)

common_organization_path = staticmethod(
CompanyServiceClient.common_organization_path
)
parse_common_organization_path = staticmethod(
CompanyServiceClient.parse_common_organization_path
)

common_project_path = staticmethod(CompanyServiceClient.common_project_path)
parse_common_project_path = staticmethod(
CompanyServiceClient.parse_common_project_path
)

common_location_path = staticmethod(CompanyServiceClient.common_location_path)
parse_common_location_path = staticmethod(
CompanyServiceClient.parse_common_location_path
)

from_service_account_file = CompanyServiceClient.from_service_account_file
from_service_account_json = from_service_account_file

@property
def transport(self) -> CompanyServiceTransport:
"""Return the transport used by the client instance.
Returns:
CompanyServiceTransport: The transport used by the client instance.
"""
return self._client.transport

get_transport_class = functools.partial(
type(CompanyServiceClient).get_transport_class, type(CompanyServiceClient)
)
Expand Down Expand Up @@ -159,7 +199,8 @@ async def create_company(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and any([parent, company]):
has_flattened_params = any([parent, company])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -241,7 +282,8 @@ async def get_company(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and any([name]):
has_flattened_params = any([name])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand All @@ -264,7 +306,7 @@ async def get_company(
maximum=60.0,
multiplier=1.3,
predicate=retries.if_exception_type(
exceptions.ServiceUnavailable, exceptions.DeadlineExceeded,
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
),
default_timeout=30.0,
Expand Down Expand Up @@ -341,7 +383,8 @@ async def update_company(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and any([company, update_mask]):
has_flattened_params = any([company, update_mask])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -415,7 +458,8 @@ async def delete_company(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and any([name]):
has_flattened_params = any([name])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand All @@ -438,7 +482,7 @@ async def delete_company(
maximum=60.0,
multiplier=1.3,
predicate=retries.if_exception_type(
exceptions.ServiceUnavailable, exceptions.DeadlineExceeded,
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
),
default_timeout=30.0,
Expand Down Expand Up @@ -499,7 +543,8 @@ async def list_companies(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and any([parent]):
has_flattened_params = any([parent])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand All @@ -522,7 +567,7 @@ async def list_companies(
maximum=60.0,
multiplier=1.3,
predicate=retries.if_exception_type(
exceptions.ServiceUnavailable, exceptions.DeadlineExceeded,
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
),
default_timeout=30.0,
Expand Down
89 changes: 85 additions & 4 deletions google/cloud/talent_v4/services/company_service/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):

from_service_account_json = from_service_account_file

@property
def transport(self) -> CompanyServiceTransport:
"""Return the transport used by the client instance.
Returns:
CompanyServiceTransport: The transport used by the client instance.
"""
return self._transport

@staticmethod
def company_path(project: str, tenant: str, company: str,) -> str:
"""Return a fully-qualified company string."""
Expand All @@ -152,6 +161,78 @@ def parse_company_path(path: str) -> Dict[str, str]:
)
return m.groupdict() if m else {}

@staticmethod
def tenant_path(project: str, tenant: str,) -> str:
"""Return a fully-qualified tenant string."""
return "projects/{project}/tenants/{tenant}".format(
project=project, tenant=tenant,
)

@staticmethod
def parse_tenant_path(path: str) -> Dict[str, str]:
"""Parse a tenant path into its component segments."""
m = re.match(r"^projects/(?P<project>.+?)/tenants/(?P<tenant>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_billing_account_path(billing_account: str,) -> str:
"""Return a fully-qualified billing_account string."""
return "billingAccounts/{billing_account}".format(
billing_account=billing_account,
)

@staticmethod
def parse_common_billing_account_path(path: str) -> Dict[str, str]:
"""Parse a billing_account path into its component segments."""
m = re.match(r"^billingAccounts/(?P<billing_account>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_folder_path(folder: str,) -> str:
"""Return a fully-qualified folder string."""
return "folders/{folder}".format(folder=folder,)

@staticmethod
def parse_common_folder_path(path: str) -> Dict[str, str]:
"""Parse a folder path into its component segments."""
m = re.match(r"^folders/(?P<folder>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_organization_path(organization: str,) -> str:
"""Return a fully-qualified organization string."""
return "organizations/{organization}".format(organization=organization,)

@staticmethod
def parse_common_organization_path(path: str) -> Dict[str, str]:
"""Parse a organization path into its component segments."""
m = re.match(r"^organizations/(?P<organization>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_project_path(project: str,) -> str:
"""Return a fully-qualified project string."""
return "projects/{project}".format(project=project,)

@staticmethod
def parse_common_project_path(path: str) -> Dict[str, str]:
"""Parse a project path into its component segments."""
m = re.match(r"^projects/(?P<project>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_location_path(project: str, location: str,) -> str:
"""Return a fully-qualified location string."""
return "projects/{project}/locations/{location}".format(
project=project, location=location,
)

@staticmethod
def parse_common_location_path(path: str) -> Dict[str, str]:
"""Parse a location path into its component segments."""
m = re.match(r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$", path)
return m.groupdict() if m else {}

def __init__(
self,
*,
Expand Down Expand Up @@ -187,10 +268,10 @@ def __init__(
not provided, the default SSL client certificate will be used if
present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not
set, no client certificate will be used.
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.
Raises:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
_transport_registry["grpc"] = CompanyServiceGrpcTransport
_transport_registry["grpc_asyncio"] = CompanyServiceGrpcAsyncIOTransport


__all__ = (
"CompanyServiceTransport",
"CompanyServiceGrpcTransport",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _prep_wrapped_messages(self, client_info):
maximum=60.0,
multiplier=1.3,
predicate=retries.if_exception_type(
exceptions.ServiceUnavailable, exceptions.DeadlineExceeded,
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
),
default_timeout=30.0,
Expand All @@ -136,7 +136,7 @@ def _prep_wrapped_messages(self, client_info):
maximum=60.0,
multiplier=1.3,
predicate=retries.if_exception_type(
exceptions.ServiceUnavailable, exceptions.DeadlineExceeded,
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
),
default_timeout=30.0,
Expand All @@ -149,7 +149,7 @@ def _prep_wrapped_messages(self, client_info):
maximum=60.0,
multiplier=1.3,
predicate=retries.if_exception_type(
exceptions.ServiceUnavailable, exceptions.DeadlineExceeded,
exceptions.DeadlineExceeded, exceptions.ServiceUnavailable,
),
),
default_timeout=30.0,
Expand Down
28 changes: 18 additions & 10 deletions google/cloud/talent_v4/services/company_service/transports/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def __init__(
for grpc channel. It is ignored if ``channel`` is provided.
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.
Raises:
Expand All @@ -105,13 +105,16 @@ def __init__(
google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
and ``credentials_file`` are passed.
"""
self._ssl_channel_credentials = ssl_channel_credentials

if channel:
# Sanity check: Ensure that channel and credentials are not both
# provided.
credentials = False

# If a channel was explicitly provided, set it.
self._grpc_channel = channel
self._ssl_channel_credentials = None
elif api_mtls_endpoint:
warnings.warn(
"api_mtls_endpoint and client_cert_source are deprecated",
Expand Down Expand Up @@ -147,7 +150,12 @@ def __init__(
ssl_credentials=ssl_credentials,
scopes=scopes or self.AUTH_SCOPES,
quota_project_id=quota_project_id,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)
self._ssl_channel_credentials = ssl_credentials
else:
host = host if ":" in host else host + ":443"

Expand All @@ -164,6 +172,10 @@ def __init__(
ssl_credentials=ssl_channel_credentials,
scopes=scopes or self.AUTH_SCOPES,
quota_project_id=quota_project_id,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)

self._stubs = {} # type: Dict[str, Callable]
Expand All @@ -190,7 +202,7 @@ def create_channel(
) -> grpc.Channel:
"""Create and return a gRPC channel object.
Args:
address (Optionsl[str]): The host for the channel to use.
address (Optional[str]): The host for the channel to use.
credentials (Optional[~.Credentials]): The
authorization credentials to attach to requests. These
credentials identify this application to the service. If
Expand Down Expand Up @@ -225,12 +237,8 @@ def create_channel(

@property
def grpc_channel(self) -> grpc.Channel:
"""Create the channel designed to connect to this service.
This property caches on the instance; repeated calls return
the same channel.
"""Return the channel designed to connect to this service.
"""
# Return the channel from cache.
return self._grpc_channel

@property
Expand Down
Loading

0 comments on commit 6f17871

Please sign in to comment.