Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context manager support in client #415

Merged
merged 6 commits into from Oct 12, 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
Expand Up @@ -1929,6 +1929,12 @@ async def update_cmek_settings(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/logging_v2/services/config_service_v2/client.py
Expand Up @@ -397,10 +397,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def list_buckets(
Expand Down Expand Up @@ -2091,6 +2088,19 @@ def update_cmek_settings(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.

.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Expand Up @@ -322,6 +322,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.

.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def list_buckets(
self,
Expand Down
Expand Up @@ -883,5 +883,8 @@ def update_cmek_settings(
)
return self._stubs["update_cmek_settings"]

def close(self):
self.grpc_channel.close()


__all__ = ("ConfigServiceV2GrpcTransport",)
Expand Up @@ -915,5 +915,8 @@ def update_cmek_settings(
)
return self._stubs["update_cmek_settings"]

def close(self):
return self.grpc_channel.close()


__all__ = ("ConfigServiceV2GrpcAsyncIOTransport",)
Expand Up @@ -779,6 +779,12 @@ def tail_log_entries(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/logging_v2/services/logging_service_v2/client.py
Expand Up @@ -341,10 +341,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def delete_log(
Expand Down Expand Up @@ -886,6 +883,19 @@ def tail_log_entries(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.

.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Expand Up @@ -257,6 +257,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.

.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def delete_log(
self,
Expand Down
Expand Up @@ -404,5 +404,8 @@ def tail_log_entries(
)
return self._stubs["tail_log_entries"]

def close(self):
self.grpc_channel.close()


__all__ = ("LoggingServiceV2GrpcTransport",)
Expand Up @@ -415,5 +415,8 @@ def tail_log_entries(
)
return self._stubs["tail_log_entries"]

def close(self):
return self.grpc_channel.close()


__all__ = ("LoggingServiceV2GrpcAsyncIOTransport",)
Expand Up @@ -635,6 +635,12 @@ async def delete_log_metric(
request, retry=retry, timeout=timeout, metadata=metadata,
)

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/logging_v2/services/metrics_service_v2/client.py
Expand Up @@ -344,10 +344,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def list_log_metrics(
Expand Down Expand Up @@ -777,6 +774,19 @@ def delete_log_metric(
request, retry=retry, timeout=timeout, metadata=metadata,
)

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.

.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Expand Up @@ -228,6 +228,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.

.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def list_log_metrics(
self,
Expand Down
Expand Up @@ -358,5 +358,8 @@ def delete_log_metric(
)
return self._stubs["delete_log_metric"]

def close(self):
self.grpc_channel.close()


__all__ = ("MetricsServiceV2GrpcTransport",)
Expand Up @@ -368,5 +368,8 @@ def delete_log_metric(
)
return self._stubs["delete_log_metric"]

def close(self):
return self.grpc_channel.close()


__all__ = ("MetricsServiceV2GrpcAsyncIOTransport",)
1 change: 1 addition & 0 deletions google/cloud/logging_v2/types/log_entry.py
Expand Up @@ -31,6 +31,7 @@

class LogEntry(proto.Message):
r"""An individual entry in a log.

Attributes:
log_name (str):
Required. The resource name of the log to which this log
Expand Down
15 changes: 14 additions & 1 deletion google/cloud/logging_v2/types/logging.py
Expand Up @@ -42,6 +42,7 @@

class DeleteLogRequest(proto.Message):
r"""The parameters to DeleteLog.

Attributes:
log_name (str):
Required. The resource name of the log to delete:
Expand All @@ -65,6 +66,7 @@ class DeleteLogRequest(proto.Message):

class WriteLogEntriesRequest(proto.Message):
r"""The parameters to WriteLogEntries.

Attributes:
log_name (str):
Optional. A default log resource name that is assigned to
Expand Down Expand Up @@ -164,11 +166,13 @@ class WriteLogEntriesRequest(proto.Message):


class WriteLogEntriesResponse(proto.Message):
r"""Result returned from WriteLogEntries. """
r"""Result returned from WriteLogEntries.
"""


class WriteLogEntriesPartialErrors(proto.Message):
r"""Error details for WriteLogEntries with partial success.

Attributes:
log_entry_errors (Sequence[google.cloud.logging_v2.types.WriteLogEntriesPartialErrors.LogEntryErrorsEntry]):
When ``WriteLogEntriesRequest.partial_success`` is true,
Expand All @@ -187,6 +191,7 @@ class WriteLogEntriesPartialErrors(proto.Message):

class ListLogEntriesRequest(proto.Message):
r"""The parameters to ``ListLogEntries``.

Attributes:
resource_names (Sequence[str]):
Required. Names of one or more parent resources from which
Expand Down Expand Up @@ -249,6 +254,7 @@ class ListLogEntriesRequest(proto.Message):

class ListLogEntriesResponse(proto.Message):
r"""Result returned from ``ListLogEntries``.

Attributes:
entries (Sequence[google.cloud.logging_v2.types.LogEntry]):
A list of log entries. If ``entries`` is empty,
Expand Down Expand Up @@ -281,6 +287,7 @@ def raw_page(self):

class ListMonitoredResourceDescriptorsRequest(proto.Message):
r"""The parameters to ListMonitoredResourceDescriptors

Attributes:
page_size (int):
Optional. The maximum number of results to return from this
Expand All @@ -301,6 +308,7 @@ class ListMonitoredResourceDescriptorsRequest(proto.Message):

class ListMonitoredResourceDescriptorsResponse(proto.Message):
r"""Result returned from ListMonitoredResourceDescriptors.

Attributes:
resource_descriptors (Sequence[google.api.monitored_resource_pb2.MonitoredResourceDescriptor]):
A list of resource descriptors.
Expand All @@ -325,6 +333,7 @@ def raw_page(self):

class ListLogsRequest(proto.Message):
r"""The parameters to ListLogs.

Attributes:
parent (str):
Required. The resource name that owns the logs:
Expand Down Expand Up @@ -366,6 +375,7 @@ class ListLogsRequest(proto.Message):

class ListLogsResponse(proto.Message):
r"""Result returned from ListLogs.

Attributes:
log_names (Sequence[str]):
A list of log names. For example,
Expand All @@ -388,6 +398,7 @@ def raw_page(self):

class TailLogEntriesRequest(proto.Message):
r"""The parameters to ``TailLogEntries``.

Attributes:
resource_names (Sequence[str]):
Required. Name of a parent resource from which to retrieve
Expand Down Expand Up @@ -431,6 +442,7 @@ class TailLogEntriesRequest(proto.Message):

class TailLogEntriesResponse(proto.Message):
r"""Result returned from ``TailLogEntries``.

Attributes:
entries (Sequence[google.cloud.logging_v2.types.LogEntry]):
A list of log entries. Each response in the stream will
Expand All @@ -450,6 +462,7 @@ class TailLogEntriesResponse(proto.Message):

class SuppressionInfo(proto.Message):
r"""Information about entries that were omitted from the session.

Attributes:
reason (google.cloud.logging_v2.types.TailLogEntriesResponse.SuppressionInfo.Reason):
The reason that entries were omitted from the
Expand Down