Skip to content

Commit d0b0d91

Browse files
feat: add context manager support in client (#96)
- [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: googleapis/googleapis@787f8c9 Source-Link: googleapis/googleapis-gen@81decff Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent fe846dd commit d0b0d91

File tree

7 files changed

+108
-4
lines changed

7 files changed

+108
-4
lines changed

packages/google-area120-tables/google/area120/tables_v1alpha1/services/tables_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,12 @@ async def batch_delete_rows(
934934
request, retry=retry, timeout=timeout, metadata=metadata,
935935
)
936936

937+
async def __aenter__(self):
938+
return self
939+
940+
async def __aexit__(self, exc_type, exc, tb):
941+
await self.transport.close()
942+
937943

938944
try:
939945
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-area120-tables/google/area120/tables_v1alpha1/services/tables_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,7 @@ def __init__(
374374
client_cert_source_for_mtls=client_cert_source_func,
375375
quota_project_id=client_options.quota_project_id,
376376
client_info=client_info,
377-
always_use_jwt_access=(
378-
Transport == type(self).get_transport_class("grpc")
379-
or Transport == type(self).get_transport_class("grpc_asyncio")
380-
),
377+
always_use_jwt_access=True,
381378
)
382379

383380
def get_table(
@@ -1139,6 +1136,19 @@ def batch_delete_rows(
11391136
request, retry=retry, timeout=timeout, metadata=metadata,
11401137
)
11411138

1139+
def __enter__(self):
1140+
return self
1141+
1142+
def __exit__(self, type, value, traceback):
1143+
"""Releases underlying transport's resources.
1144+
1145+
.. warning::
1146+
ONLY use as a context manager if the transport is NOT shared
1147+
with other clients! Exiting the with block will CLOSE the transport
1148+
and may cause errors in other clients!
1149+
"""
1150+
self.transport.close()
1151+
11421152

11431153
try:
11441154
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-area120-tables/google/area120/tables_v1alpha1/services/tables_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ def _prep_wrapped_messages(self, client_info):
198198
),
199199
}
200200

201+
def close(self):
202+
"""Closes resources associated with the transport.
203+
204+
.. warning::
205+
Only call this method if the transport is NOT shared
206+
with other clients - this may cause errors in other clients!
207+
"""
208+
raise NotImplementedError()
209+
201210
@property
202211
def get_table(
203212
self,

packages/google-area120-tables/google/area120/tables_v1alpha1/services/tables_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,5 +540,8 @@ def batch_delete_rows(
540540
)
541541
return self._stubs["batch_delete_rows"]
542542

543+
def close(self):
544+
self.grpc_channel.close()
545+
543546

544547
__all__ = ("TablesServiceGrpcTransport",)

packages/google-area120-tables/google/area120/tables_v1alpha1/services/tables_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,5 +555,8 @@ def batch_delete_rows(
555555
)
556556
return self._stubs["batch_delete_rows"]
557557

558+
def close(self):
559+
return self.grpc_channel.close()
560+
558561

559562
__all__ = ("TablesServiceGrpcAsyncIOTransport",)

packages/google-area120-tables/google/area120/tables_v1alpha1/types/tables.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class View(proto.Enum):
5959

6060
class GetTableRequest(proto.Message):
6161
r"""Request message for TablesService.GetTable.
62+
6263
Attributes:
6364
name (str):
6465
Required. The name of the table to retrieve.
@@ -70,6 +71,7 @@ class GetTableRequest(proto.Message):
7071

7172
class ListTablesRequest(proto.Message):
7273
r"""Request message for TablesService.ListTables.
74+
7375
Attributes:
7476
page_size (int):
7577
The maximum number of tables to return. The
@@ -93,6 +95,7 @@ class ListTablesRequest(proto.Message):
9395

9496
class ListTablesResponse(proto.Message):
9597
r"""Response message for TablesService.ListTables.
98+
9699
Attributes:
97100
tables (Sequence[google.area120.tables_v1alpha1.types.Table]):
98101
The list of tables.
@@ -112,6 +115,7 @@ def raw_page(self):
112115

113116
class GetWorkspaceRequest(proto.Message):
114117
r"""Request message for TablesService.GetWorkspace.
118+
115119
Attributes:
116120
name (str):
117121
Required. The name of the workspace to
@@ -123,6 +127,7 @@ class GetWorkspaceRequest(proto.Message):
123127

124128
class ListWorkspacesRequest(proto.Message):
125129
r"""Request message for TablesService.ListWorkspaces.
130+
126131
Attributes:
127132
page_size (int):
128133
The maximum number of workspaces to return.
@@ -145,6 +150,7 @@ class ListWorkspacesRequest(proto.Message):
145150

146151
class ListWorkspacesResponse(proto.Message):
147152
r"""Response message for TablesService.ListWorkspaces.
153+
148154
Attributes:
149155
workspaces (Sequence[google.area120.tables_v1alpha1.types.Workspace]):
150156
The list of workspaces.
@@ -164,6 +170,7 @@ def raw_page(self):
164170

165171
class GetRowRequest(proto.Message):
166172
r"""Request message for TablesService.GetRow.
173+
167174
Attributes:
168175
name (str):
169176
Required. The name of the row to retrieve.
@@ -179,6 +186,7 @@ class GetRowRequest(proto.Message):
179186

180187
class ListRowsRequest(proto.Message):
181188
r"""Request message for TablesService.ListRows.
189+
182190
Attributes:
183191
parent (str):
184192
Required. The parent table.
@@ -216,6 +224,7 @@ class ListRowsRequest(proto.Message):
216224

217225
class ListRowsResponse(proto.Message):
218226
r"""Response message for TablesService.ListRows.
227+
219228
Attributes:
220229
rows (Sequence[google.area120.tables_v1alpha1.types.Row]):
221230
The rows from the specified table.
@@ -235,6 +244,7 @@ def raw_page(self):
235244

236245
class CreateRowRequest(proto.Message):
237246
r"""Request message for TablesService.CreateRow.
247+
238248
Attributes:
239249
parent (str):
240250
Required. The parent table where this row
@@ -253,6 +263,7 @@ class CreateRowRequest(proto.Message):
253263

254264
class BatchCreateRowsRequest(proto.Message):
255265
r"""Request message for TablesService.BatchCreateRows.
266+
256267
Attributes:
257268
parent (str):
258269
Required. The parent table where the rows
@@ -270,6 +281,7 @@ class BatchCreateRowsRequest(proto.Message):
270281

271282
class BatchCreateRowsResponse(proto.Message):
272283
r"""Response message for TablesService.BatchCreateRows.
284+
273285
Attributes:
274286
rows (Sequence[google.area120.tables_v1alpha1.types.Row]):
275287
The created rows.
@@ -280,6 +292,7 @@ class BatchCreateRowsResponse(proto.Message):
280292

281293
class UpdateRowRequest(proto.Message):
282294
r"""Request message for TablesService.UpdateRow.
295+
283296
Attributes:
284297
row (google.area120.tables_v1alpha1.types.Row):
285298
Required. The row to update.
@@ -299,6 +312,7 @@ class UpdateRowRequest(proto.Message):
299312

300313
class BatchUpdateRowsRequest(proto.Message):
301314
r"""Request message for TablesService.BatchUpdateRows.
315+
302316
Attributes:
303317
parent (str):
304318
Required. The parent table shared by all rows
@@ -316,6 +330,7 @@ class BatchUpdateRowsRequest(proto.Message):
316330

317331
class BatchUpdateRowsResponse(proto.Message):
318332
r"""Response message for TablesService.BatchUpdateRows.
333+
319334
Attributes:
320335
rows (Sequence[google.area120.tables_v1alpha1.types.Row]):
321336
The updated rows.
@@ -326,6 +341,7 @@ class BatchUpdateRowsResponse(proto.Message):
326341

327342
class DeleteRowRequest(proto.Message):
328343
r"""Request message for TablesService.DeleteRow
344+
329345
Attributes:
330346
name (str):
331347
Required. The name of the row to delete.
@@ -337,6 +353,7 @@ class DeleteRowRequest(proto.Message):
337353

338354
class BatchDeleteRowsRequest(proto.Message):
339355
r"""Request message for TablesService.BatchDeleteRows
356+
340357
Attributes:
341358
parent (str):
342359
Required. The parent table shared by all rows
@@ -355,6 +372,7 @@ class BatchDeleteRowsRequest(proto.Message):
355372

356373
class Table(proto.Message):
357374
r"""A single table.
375+
358376
Attributes:
359377
name (str):
360378
The resource name of the table. Table names have the form
@@ -373,6 +391,7 @@ class Table(proto.Message):
373391

374392
class ColumnDescription(proto.Message):
375393
r"""Details on a column in the table.
394+
376395
Attributes:
377396
name (str):
378397
column name
@@ -416,6 +435,7 @@ class ColumnDescription(proto.Message):
416435

417436
class LabeledItem(proto.Message):
418437
r"""A single item in a labeled column.
438+
419439
Attributes:
420440
name (str):
421441
Display string as entered by user.
@@ -429,6 +449,7 @@ class LabeledItem(proto.Message):
429449

430450
class RelationshipDetails(proto.Message):
431451
r"""Details about a relationship column.
452+
432453
Attributes:
433454
linked_table (str):
434455
The name of the table this relationship is
@@ -456,6 +477,7 @@ class LookupDetails(proto.Message):
456477

457478
class Row(proto.Message):
458479
r"""A single row in a table.
480+
459481
Attributes:
460482
name (str):
461483
The resource name of the row. Row names have the form
@@ -476,6 +498,7 @@ class Row(proto.Message):
476498

477499
class Workspace(proto.Message):
478500
r"""A single workspace.
501+
479502
Attributes:
480503
name (str):
481504
The resource name of the workspace. Workspace names have the

packages/google-area120-tables/tests/unit/gapic/tables_v1alpha1/test_tables_service.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from google.api_core import gapic_v1
3030
from google.api_core import grpc_helpers
3131
from google.api_core import grpc_helpers_async
32+
from google.api_core import path_template
3233
from google.area120.tables_v1alpha1.services.tables_service import (
3334
TablesServiceAsyncClient,
3435
)
@@ -2944,6 +2945,9 @@ def test_tables_service_base_transport():
29442945
with pytest.raises(NotImplementedError):
29452946
getattr(transport, method)(request=object())
29462947

2948+
with pytest.raises(NotImplementedError):
2949+
transport.close()
2950+
29472951

29482952
@requires_google_auth_gte_1_25_0
29492953
def test_tables_service_base_transport_with_credentials_file():
@@ -3505,3 +3509,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
35053509
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
35063510
)
35073511
prep.assert_called_once_with(client_info)
3512+
3513+
3514+
@pytest.mark.asyncio
3515+
async def test_transport_close_async():
3516+
client = TablesServiceAsyncClient(
3517+
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
3518+
)
3519+
with mock.patch.object(
3520+
type(getattr(client.transport, "grpc_channel")), "close"
3521+
) as close:
3522+
async with client:
3523+
close.assert_not_called()
3524+
close.assert_called_once()
3525+
3526+
3527+
def test_transport_close():
3528+
transports = {
3529+
"grpc": "_grpc_channel",
3530+
}
3531+
3532+
for transport, close_name in transports.items():
3533+
client = TablesServiceClient(
3534+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
3535+
)
3536+
with mock.patch.object(
3537+
type(getattr(client.transport, close_name)), "close"
3538+
) as close:
3539+
with client:
3540+
close.assert_not_called()
3541+
close.assert_called_once()
3542+
3543+
3544+
def test_client_ctx():
3545+
transports = [
3546+
"grpc",
3547+
]
3548+
for transport in transports:
3549+
client = TablesServiceClient(
3550+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
3551+
)
3552+
# Test client calls underlying transport.
3553+
with mock.patch.object(type(client.transport), "close") as close:
3554+
close.assert_not_called()
3555+
with client:
3556+
pass
3557+
close.assert_called()

0 commit comments

Comments
 (0)