Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Commit ac75850

Browse files
feat: add context manager support in client (#229)
- [ ] 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: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent e490fb8 commit ac75850

File tree

41 files changed

+623
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+623
-27
lines changed

google/cloud/videointelligence_v1/services/video_intelligence_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ async def annotate_video(
276276
# Done; return the response.
277277
return response
278278

279+
async def __aenter__(self):
280+
return self
281+
282+
async def __aexit__(self, exc_type, exc, tb):
283+
await self.transport.close()
284+
279285

280286
try:
281287
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/videointelligence_v1/services/video_intelligence_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,7 @@ def __init__(
331331
client_cert_source_for_mtls=client_cert_source_func,
332332
quota_project_id=client_options.quota_project_id,
333333
client_info=client_info,
334-
always_use_jwt_access=(
335-
Transport == type(self).get_transport_class("grpc")
336-
or Transport == type(self).get_transport_class("grpc_asyncio")
337-
),
334+
always_use_jwt_access=True,
338335
)
339336

340337
def annotate_video(
@@ -438,6 +435,19 @@ def annotate_video(
438435
# Done; return the response.
439436
return response
440437

438+
def __enter__(self):
439+
return self
440+
441+
def __exit__(self, type, value, traceback):
442+
"""Releases underlying transport's resources.
443+
444+
.. warning::
445+
ONLY use as a context manager if the transport is NOT shared
446+
with other clients! Exiting the with block will CLOSE the transport
447+
and may cause errors in other clients!
448+
"""
449+
self.transport.close()
450+
441451

442452
try:
443453
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/videointelligence_v1/services/video_intelligence_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ def _prep_wrapped_messages(self, client_info):
173173
),
174174
}
175175

176+
def close(self):
177+
"""Closes resources associated with the transport.
178+
179+
.. warning::
180+
Only call this method if the transport is NOT shared
181+
with other clients - this may cause errors in other clients!
182+
"""
183+
raise NotImplementedError()
184+
176185
@property
177186
def operations_client(self) -> operations_v1.OperationsClient:
178187
"""Return the client designed to process long-running operations."""

google/cloud/videointelligence_v1/services/video_intelligence_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,8 @@ def annotate_video(
272272
)
273273
return self._stubs["annotate_video"]
274274

275+
def close(self):
276+
self.grpc_channel.close()
277+
275278

276279
__all__ = ("VideoIntelligenceServiceGrpcTransport",)

google/cloud/videointelligence_v1/services/video_intelligence_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,5 +279,8 @@ def annotate_video(
279279
)
280280
return self._stubs["annotate_video"]
281281

282+
def close(self):
283+
return self.grpc_channel.close()
284+
282285

283286
__all__ = ("VideoIntelligenceServiceGrpcAsyncIOTransport",)

google/cloud/videointelligence_v1/types/video_intelligence.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class Likelihood(proto.Enum):
107107

108108
class AnnotateVideoRequest(proto.Message):
109109
r"""Video annotation request.
110+
110111
Attributes:
111112
input_uri (str):
112113
Input video location. Currently, only `Cloud
@@ -157,6 +158,7 @@ class AnnotateVideoRequest(proto.Message):
157158

158159
class VideoContext(proto.Message):
159160
r"""Video context and/or feature-specific parameters.
161+
160162
Attributes:
161163
segments (Sequence[google.cloud.videointelligence_v1.types.VideoSegment]):
162164
Video segments to annotate. The segments may
@@ -210,6 +212,7 @@ class VideoContext(proto.Message):
210212

211213
class LabelDetectionConfig(proto.Message):
212214
r"""Config for LABEL_DETECTION.
215+
213216
Attributes:
214217
label_detection_mode (google.cloud.videointelligence_v1.types.LabelDetectionMode):
215218
What labels should be detected with LABEL_DETECTION, in
@@ -251,6 +254,7 @@ class LabelDetectionConfig(proto.Message):
251254

252255
class ShotChangeDetectionConfig(proto.Message):
253256
r"""Config for SHOT_CHANGE_DETECTION.
257+
254258
Attributes:
255259
model (str):
256260
Model to use for shot change detection.
@@ -263,6 +267,7 @@ class ShotChangeDetectionConfig(proto.Message):
263267

264268
class ObjectTrackingConfig(proto.Message):
265269
r"""Config for OBJECT_TRACKING.
270+
266271
Attributes:
267272
model (str):
268273
Model to use for object tracking.
@@ -275,6 +280,7 @@ class ObjectTrackingConfig(proto.Message):
275280

276281
class FaceDetectionConfig(proto.Message):
277282
r"""Config for FACE_DETECTION.
283+
278284
Attributes:
279285
model (str):
280286
Model to use for face detection.
@@ -296,6 +302,7 @@ class FaceDetectionConfig(proto.Message):
296302

297303
class PersonDetectionConfig(proto.Message):
298304
r"""Config for PERSON_DETECTION.
305+
299306
Attributes:
300307
include_bounding_boxes (bool):
301308
Whether bounding boxes are included in the
@@ -317,6 +324,7 @@ class PersonDetectionConfig(proto.Message):
317324

318325
class ExplicitContentDetectionConfig(proto.Message):
319326
r"""Config for EXPLICIT_CONTENT_DETECTION.
327+
320328
Attributes:
321329
model (str):
322330
Model to use for explicit content detection.
@@ -329,6 +337,7 @@ class ExplicitContentDetectionConfig(proto.Message):
329337

330338
class TextDetectionConfig(proto.Message):
331339
r"""Config for TEXT_DETECTION.
340+
332341
Attributes:
333342
language_hints (Sequence[str]):
334343
Language hint can be specified if the
@@ -351,6 +360,7 @@ class TextDetectionConfig(proto.Message):
351360

352361
class VideoSegment(proto.Message):
353362
r"""Video segment.
363+
354364
Attributes:
355365
start_time_offset (google.protobuf.duration_pb2.Duration):
356366
Time-offset, relative to the beginning of the
@@ -372,6 +382,7 @@ class VideoSegment(proto.Message):
372382

373383
class LabelSegment(proto.Message):
374384
r"""Video segment level annotation results for label detection.
385+
375386
Attributes:
376387
segment (google.cloud.videointelligence_v1.types.VideoSegment):
377388
Video segment where a label was detected.
@@ -385,6 +396,7 @@ class LabelSegment(proto.Message):
385396

386397
class LabelFrame(proto.Message):
387398
r"""Video frame level annotation results for label detection.
399+
388400
Attributes:
389401
time_offset (google.protobuf.duration_pb2.Duration):
390402
Time-offset, relative to the beginning of the
@@ -400,6 +412,7 @@ class LabelFrame(proto.Message):
400412

401413
class Entity(proto.Message):
402414
r"""Detected entity from video analysis.
415+
403416
Attributes:
404417
entity_id (str):
405418
Opaque entity ID. Some IDs may be available in `Google
@@ -418,6 +431,7 @@ class Entity(proto.Message):
418431

419432
class LabelAnnotation(proto.Message):
420433
r"""Label annotation.
434+
421435
Attributes:
422436
entity (google.cloud.videointelligence_v1.types.Entity):
423437
Detected entity.
@@ -444,6 +458,7 @@ class LabelAnnotation(proto.Message):
444458

445459
class ExplicitContentFrame(proto.Message):
446460
r"""Video frame level annotation results for explicit content.
461+
447462
Attributes:
448463
time_offset (google.protobuf.duration_pb2.Duration):
449464
Time-offset, relative to the beginning of the
@@ -499,6 +514,7 @@ class NormalizedBoundingBox(proto.Message):
499514

500515
class FaceDetectionAnnotation(proto.Message):
501516
r"""Face detection annotation.
517+
502518
Attributes:
503519
tracks (Sequence[google.cloud.videointelligence_v1.types.Track]):
504520
The face tracks with attributes.
@@ -515,6 +531,7 @@ class FaceDetectionAnnotation(proto.Message):
515531

516532
class PersonDetectionAnnotation(proto.Message):
517533
r"""Person detection annotation per video.
534+
518535
Attributes:
519536
tracks (Sequence[google.cloud.videointelligence_v1.types.Track]):
520537
The detected tracks of a person.
@@ -528,6 +545,7 @@ class PersonDetectionAnnotation(proto.Message):
528545

529546
class FaceSegment(proto.Message):
530547
r"""Video segment level annotation results for face detection.
548+
531549
Attributes:
532550
segment (google.cloud.videointelligence_v1.types.VideoSegment):
533551
Video segment where a face was detected.
@@ -538,6 +556,7 @@ class FaceSegment(proto.Message):
538556

539557
class FaceFrame(proto.Message):
540558
r"""Deprecated. No effect.
559+
541560
Attributes:
542561
normalized_bounding_boxes (Sequence[google.cloud.videointelligence_v1.types.NormalizedBoundingBox]):
543562
Normalized Bounding boxes in a frame.
@@ -558,6 +577,7 @@ class FaceFrame(proto.Message):
558577

559578
class FaceAnnotation(proto.Message):
560579
r"""Deprecated. No effect.
580+
561581
Attributes:
562582
thumbnail (bytes):
563583
Thumbnail of a representative face view (in
@@ -606,6 +626,7 @@ class TimestampedObject(proto.Message):
606626

607627
class Track(proto.Message):
608628
r"""A track of an object instance.
629+
609630
Attributes:
610631
segment (google.cloud.videointelligence_v1.types.VideoSegment):
611632
Video segment of a track.
@@ -675,6 +696,7 @@ class DetectedLandmark(proto.Message):
675696

676697
class VideoAnnotationResults(proto.Message):
677698
r"""Annotation results for a single video.
699+
678700
Attributes:
679701
input_uri (str):
680702
Video file location in `Cloud
@@ -806,6 +828,7 @@ class AnnotateVideoResponse(proto.Message):
806828

807829
class VideoAnnotationProgress(proto.Message):
808830
r"""Annotation progress for a single video.
831+
809832
Attributes:
810833
input_uri (str):
811834
Video file location in `Cloud
@@ -851,6 +874,7 @@ class AnnotateVideoProgress(proto.Message):
851874

852875
class SpeechTranscriptionConfig(proto.Message):
853876
r"""Config for SPEECH_TRANSCRIPTION.
877+
854878
Attributes:
855879
language_code (str):
856880
Required. *Required* The language of the supplied audio as a
@@ -969,6 +993,7 @@ class SpeechTranscription(proto.Message):
969993

970994
class SpeechRecognitionAlternative(proto.Message):
971995
r"""Alternative hypotheses (a.k.a. n-best list).
996+
972997
Attributes:
973998
transcript (str):
974999
Transcript text representing the words that
@@ -1077,6 +1102,7 @@ class NormalizedBoundingPoly(proto.Message):
10771102

10781103
class TextSegment(proto.Message):
10791104
r"""Video segment level annotation results for text detection.
1105+
10801106
Attributes:
10811107
segment (google.cloud.videointelligence_v1.types.VideoSegment):
10821108
Video segment where a text snippet was
@@ -1155,6 +1181,7 @@ class ObjectTrackingFrame(proto.Message):
11551181

11561182
class ObjectTrackingAnnotation(proto.Message):
11571183
r"""Annotations corresponding to one tracked object.
1184+
11581185
Attributes:
11591186
segment (google.cloud.videointelligence_v1.types.VideoSegment):
11601187
Non-streaming batch mode ONLY.

google/cloud/videointelligence_v1beta2/services/video_intelligence_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ async def annotate_video(
276276
# Done; return the response.
277277
return response
278278

279+
async def __aenter__(self):
280+
return self
281+
282+
async def __aexit__(self, exc_type, exc, tb):
283+
await self.transport.close()
284+
279285

280286
try:
281287
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/videointelligence_v1beta2/services/video_intelligence_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,7 @@ def __init__(
331331
client_cert_source_for_mtls=client_cert_source_func,
332332
quota_project_id=client_options.quota_project_id,
333333
client_info=client_info,
334-
always_use_jwt_access=(
335-
Transport == type(self).get_transport_class("grpc")
336-
or Transport == type(self).get_transport_class("grpc_asyncio")
337-
),
334+
always_use_jwt_access=True,
338335
)
339336

340337
def annotate_video(
@@ -438,6 +435,19 @@ def annotate_video(
438435
# Done; return the response.
439436
return response
440437

438+
def __enter__(self):
439+
return self
440+
441+
def __exit__(self, type, value, traceback):
442+
"""Releases underlying transport's resources.
443+
444+
.. warning::
445+
ONLY use as a context manager if the transport is NOT shared
446+
with other clients! Exiting the with block will CLOSE the transport
447+
and may cause errors in other clients!
448+
"""
449+
self.transport.close()
450+
441451

442452
try:
443453
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/videointelligence_v1beta2/services/video_intelligence_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ def _prep_wrapped_messages(self, client_info):
173173
),
174174
}
175175

176+
def close(self):
177+
"""Closes resources associated with the transport.
178+
179+
.. warning::
180+
Only call this method if the transport is NOT shared
181+
with other clients - this may cause errors in other clients!
182+
"""
183+
raise NotImplementedError()
184+
176185
@property
177186
def operations_client(self) -> operations_v1.OperationsClient:
178187
"""Return the client designed to process long-running operations."""

google/cloud/videointelligence_v1beta2/services/video_intelligence_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,8 @@ def annotate_video(
272272
)
273273
return self._stubs["annotate_video"]
274274

275+
def close(self):
276+
self.grpc_channel.close()
277+
275278

276279
__all__ = ("VideoIntelligenceServiceGrpcTransport",)

0 commit comments

Comments
 (0)