Skip to content

Commit 1a6f01e

Browse files
feat: add context manager support in client (#145)
- [ ] 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 f75054d commit 1a6f01e

File tree

16 files changed

+188
-10
lines changed

16 files changed

+188
-10
lines changed

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,12 @@ async def delete_budget(
624624
request, retry=retry, timeout=timeout, metadata=metadata,
625625
)
626626

627+
async def __aenter__(self):
628+
return self
629+
630+
async def __aexit__(self, exc_type, exc, tb):
631+
await self.transport.close()
632+
627633

628634
try:
629635
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,7 @@ def __init__(
346346
client_cert_source_for_mtls=client_cert_source_func,
347347
quota_project_id=client_options.quota_project_id,
348348
client_info=client_info,
349-
always_use_jwt_access=(
350-
Transport == type(self).get_transport_class("grpc")
351-
or Transport == type(self).get_transport_class("grpc_asyncio")
352-
),
349+
always_use_jwt_access=True,
353350
)
354351

355352
def create_budget(
@@ -770,6 +767,19 @@ def delete_budget(
770767
request, retry=retry, timeout=timeout, metadata=metadata,
771768
)
772769

770+
def __enter__(self):
771+
return self
772+
773+
def __exit__(self, type, value, traceback):
774+
"""Releases underlying transport's resources.
775+
776+
.. warning::
777+
ONLY use as a context manager if the transport is NOT shared
778+
with other clients! Exiting the with block will CLOSE the transport
779+
and may cause errors in other clients!
780+
"""
781+
self.transport.close()
782+
773783

774784
try:
775785
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ def _prep_wrapped_messages(self, client_info):
224224
),
225225
}
226226

227+
def close(self):
228+
"""Closes resources associated with the transport.
229+
230+
.. warning::
231+
Only call this method if the transport is NOT shared
232+
with other clients - this may cause errors in other clients!
233+
"""
234+
raise NotImplementedError()
235+
227236
@property
228237
def create_budget(
229238
self,

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,8 @@ def delete_budget(
379379
)
380380
return self._stubs["delete_budget"]
381381

382+
def close(self):
383+
self.grpc_channel.close()
384+
382385

383386
__all__ = ("BudgetServiceGrpcTransport",)

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/services/budget_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,5 +383,8 @@ def delete_budget(
383383
)
384384
return self._stubs["delete_budget"]
385385

386+
def close(self):
387+
return self.grpc_channel.close()
388+
386389

387390
__all__ = ("BudgetServiceGrpcAsyncIOTransport",)

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class Budget(proto.Message):
101101

102102
class BudgetAmount(proto.Message):
103103
r"""The budgeted amount for each usage period.
104+
104105
Attributes:
105106
specified_amount (google.type.money_pb2.Money):
106107
A specified amount to use as the budget. ``currency_code``
@@ -135,7 +136,8 @@ class LastPeriodAmount(proto.Message):
135136
configuring a percentage of last period's spend). LastPeriodAmount
136137
cannot be set for a budget configured with a
137138
[Filter.custom_period][google.cloud.billing.budgets.v1.Filter.custom_period].
138-
"""
139+
140+
"""
139141

140142

141143
class ThresholdRule(proto.Message):

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1/types/budget_service.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
class CreateBudgetRequest(proto.Message):
3636
r"""Request for CreateBudget
37+
3738
Attributes:
3839
parent (str):
3940
Required. The name of the billing account to create the
@@ -49,6 +50,7 @@ class CreateBudgetRequest(proto.Message):
4950

5051
class UpdateBudgetRequest(proto.Message):
5152
r"""Request for UpdateBudget
53+
5254
Attributes:
5355
budget (google.cloud.billing.budgets_v1.types.Budget):
5456
Required. The updated budget object.
@@ -71,6 +73,7 @@ class UpdateBudgetRequest(proto.Message):
7173

7274
class GetBudgetRequest(proto.Message):
7375
r"""Request for GetBudget
76+
7477
Attributes:
7578
name (str):
7679
Required. Name of budget to get. Values are of the form
@@ -82,6 +85,7 @@ class GetBudgetRequest(proto.Message):
8285

8386
class ListBudgetsRequest(proto.Message):
8487
r"""Request for ListBudgets
88+
8589
Attributes:
8690
parent (str):
8791
Required. Name of billing account to list budgets under.
@@ -105,6 +109,7 @@ class ListBudgetsRequest(proto.Message):
105109

106110
class ListBudgetsResponse(proto.Message):
107111
r"""Response for ListBudgets
112+
108113
Attributes:
109114
budgets (Sequence[google.cloud.billing.budgets_v1.types.Budget]):
110115
List of the budgets owned by the requested
@@ -125,6 +130,7 @@ def raw_page(self):
125130

126131
class DeleteBudgetRequest(proto.Message):
127132
r"""Request for DeleteBudget
133+
128134
Attributes:
129135
name (str):
130136
Required. Name of the budget to delete. Values are of the

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ async def delete_budget(
487487
request, retry=retry, timeout=timeout, metadata=metadata,
488488
)
489489

490+
async def __aenter__(self):
491+
return self
492+
493+
async def __aexit__(self, exc_type, exc, tb):
494+
await self.transport.close()
495+
490496

491497
try:
492498
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,7 @@ def __init__(
345345
client_cert_source_for_mtls=client_cert_source_func,
346346
quota_project_id=client_options.quota_project_id,
347347
client_info=client_info,
348-
always_use_jwt_access=(
349-
Transport == type(self).get_transport_class("grpc")
350-
or Transport == type(self).get_transport_class("grpc_asyncio")
351-
),
348+
always_use_jwt_access=True,
352349
)
353350

354351
def create_budget(
@@ -638,6 +635,19 @@ def delete_budget(
638635
request, retry=retry, timeout=timeout, metadata=metadata,
639636
)
640637

638+
def __enter__(self):
639+
return self
640+
641+
def __exit__(self, type, value, traceback):
642+
"""Releases underlying transport's resources.
643+
644+
.. warning::
645+
ONLY use as a context manager if the transport is NOT shared
646+
with other clients! Exiting the with block will CLOSE the transport
647+
and may cause errors in other clients!
648+
"""
649+
self.transport.close()
650+
641651

642652
try:
643653
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-billing-budgets/google/cloud/billing/budgets_v1beta1/services/budget_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ def _prep_wrapped_messages(self, client_info):
224224
),
225225
}
226226

227+
def close(self):
228+
"""Closes resources associated with the transport.
229+
230+
.. warning::
231+
Only call this method if the transport is NOT shared
232+
with other clients - this may cause errors in other clients!
233+
"""
234+
raise NotImplementedError()
235+
227236
@property
228237
def create_budget(
229238
self,

0 commit comments

Comments
 (0)