Skip to content

Commit 4363179

Browse files
gcf-owl-bot[bot]ohmayrparthea
authored
feat: deprecate enabled field for message transforms and add disabled field (#1355)
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: ohmayr <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent fa39b0e commit 4363179

File tree

27 files changed

+5835
-1253
lines changed

27 files changed

+5835
-1253
lines changed

google/pubsub_v1/services/publisher/async_client.py

Lines changed: 136 additions & 55 deletions
Large diffs are not rendered by default.

google/pubsub_v1/services/publisher/client.py

Lines changed: 200 additions & 86 deletions
Large diffs are not rendered by default.

google/pubsub_v1/services/publisher/pagers.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(
6666
*,
6767
retry: OptionalRetry = gapic_v1.method.DEFAULT,
6868
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
69-
metadata: Sequence[Tuple[str, str]] = ()
69+
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
7070
):
7171
"""Instantiate the pager.
7272
@@ -80,8 +80,10 @@ def __init__(
8080
retry (google.api_core.retry.Retry): Designation of what errors,
8181
if any, should be retried.
8282
timeout (float): The timeout for this request.
83-
metadata (Sequence[Tuple[str, str]]): Strings which should be
84-
sent along with the request as metadata.
83+
metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be
84+
sent along with the request as metadata. Normally, each value must be of type `str`,
85+
but for metadata keys ending with the suffix `-bin`, the corresponding values must
86+
be of type `bytes`.
8587
"""
8688
self._method = method
8789
self._request = pubsub.ListTopicsRequest(request)
@@ -140,7 +142,7 @@ def __init__(
140142
*,
141143
retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT,
142144
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
143-
metadata: Sequence[Tuple[str, str]] = ()
145+
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
144146
):
145147
"""Instantiates the pager.
146148
@@ -154,8 +156,10 @@ def __init__(
154156
retry (google.api_core.retry.AsyncRetry): Designation of what errors,
155157
if any, should be retried.
156158
timeout (float): The timeout for this request.
157-
metadata (Sequence[Tuple[str, str]]): Strings which should be
158-
sent along with the request as metadata.
159+
metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be
160+
sent along with the request as metadata. Normally, each value must be of type `str`,
161+
but for metadata keys ending with the suffix `-bin`, the corresponding values must
162+
be of type `bytes`.
159163
"""
160164
self._method = method
161165
self._request = pubsub.ListTopicsRequest(request)
@@ -218,7 +222,7 @@ def __init__(
218222
*,
219223
retry: OptionalRetry = gapic_v1.method.DEFAULT,
220224
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
221-
metadata: Sequence[Tuple[str, str]] = ()
225+
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
222226
):
223227
"""Instantiate the pager.
224228
@@ -232,8 +236,10 @@ def __init__(
232236
retry (google.api_core.retry.Retry): Designation of what errors,
233237
if any, should be retried.
234238
timeout (float): The timeout for this request.
235-
metadata (Sequence[Tuple[str, str]]): Strings which should be
236-
sent along with the request as metadata.
239+
metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be
240+
sent along with the request as metadata. Normally, each value must be of type `str`,
241+
but for metadata keys ending with the suffix `-bin`, the corresponding values must
242+
be of type `bytes`.
237243
"""
238244
self._method = method
239245
self._request = pubsub.ListTopicSubscriptionsRequest(request)
@@ -292,7 +298,7 @@ def __init__(
292298
*,
293299
retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT,
294300
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
295-
metadata: Sequence[Tuple[str, str]] = ()
301+
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
296302
):
297303
"""Instantiates the pager.
298304
@@ -306,8 +312,10 @@ def __init__(
306312
retry (google.api_core.retry.AsyncRetry): Designation of what errors,
307313
if any, should be retried.
308314
timeout (float): The timeout for this request.
309-
metadata (Sequence[Tuple[str, str]]): Strings which should be
310-
sent along with the request as metadata.
315+
metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be
316+
sent along with the request as metadata. Normally, each value must be of type `str`,
317+
but for metadata keys ending with the suffix `-bin`, the corresponding values must
318+
be of type `bytes`.
311319
"""
312320
self._method = method
313321
self._request = pubsub.ListTopicSubscriptionsRequest(request)
@@ -370,7 +378,7 @@ def __init__(
370378
*,
371379
retry: OptionalRetry = gapic_v1.method.DEFAULT,
372380
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
373-
metadata: Sequence[Tuple[str, str]] = ()
381+
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
374382
):
375383
"""Instantiate the pager.
376384
@@ -384,8 +392,10 @@ def __init__(
384392
retry (google.api_core.retry.Retry): Designation of what errors,
385393
if any, should be retried.
386394
timeout (float): The timeout for this request.
387-
metadata (Sequence[Tuple[str, str]]): Strings which should be
388-
sent along with the request as metadata.
395+
metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be
396+
sent along with the request as metadata. Normally, each value must be of type `str`,
397+
but for metadata keys ending with the suffix `-bin`, the corresponding values must
398+
be of type `bytes`.
389399
"""
390400
self._method = method
391401
self._request = pubsub.ListTopicSnapshotsRequest(request)
@@ -444,7 +454,7 @@ def __init__(
444454
*,
445455
retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT,
446456
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
447-
metadata: Sequence[Tuple[str, str]] = ()
457+
metadata: Sequence[Tuple[str, Union[str, bytes]]] = ()
448458
):
449459
"""Instantiates the pager.
450460
@@ -458,8 +468,10 @@ def __init__(
458468
retry (google.api_core.retry.AsyncRetry): Designation of what errors,
459469
if any, should be retried.
460470
timeout (float): The timeout for this request.
461-
metadata (Sequence[Tuple[str, str]]): Strings which should be
462-
sent along with the request as metadata.
471+
metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be
472+
sent along with the request as metadata. Normally, each value must be of type `str`,
473+
but for metadata keys ending with the suffix `-bin`, the corresponding values must
474+
be of type `bytes`.
463475
"""
464476
self._method = method
465477
self._request = pubsub.ListTopicSnapshotsRequest(request)

google/pubsub_v1/services/publisher/transports/grpc.py

Lines changed: 102 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16+
import json
17+
import logging as std_logging
18+
import pickle
1619
import warnings
1720
from typing import Callable, Dict, Optional, Sequence, Tuple, Union
1821

@@ -21,15 +24,93 @@
2124
import google.auth # type: ignore
2225
from google.auth import credentials as ga_credentials # type: ignore
2326
from google.auth.transport.grpc import SslCredentials # type: ignore
27+
from google.protobuf.json_format import MessageToJson
28+
import google.protobuf.message
2429

2530
import grpc # type: ignore
31+
import proto # type: ignore
2632

2733
from google.iam.v1 import iam_policy_pb2 # type: ignore
2834
from google.iam.v1 import policy_pb2 # type: ignore
2935
from google.protobuf import empty_pb2 # type: ignore
3036
from google.pubsub_v1.types import pubsub
3137
from .base import PublisherTransport, DEFAULT_CLIENT_INFO
3238

39+
try:
40+
from google.api_core import client_logging # type: ignore
41+
42+
CLIENT_LOGGING_SUPPORTED = True # pragma: NO COVER
43+
except ImportError: # pragma: NO COVER
44+
CLIENT_LOGGING_SUPPORTED = False
45+
46+
_LOGGER = std_logging.getLogger(__name__)
47+
48+
49+
class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER
50+
def intercept_unary_unary(self, continuation, client_call_details, request):
51+
logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
52+
std_logging.DEBUG
53+
)
54+
if logging_enabled: # pragma: NO COVER
55+
request_metadata = client_call_details.metadata
56+
if isinstance(request, proto.Message):
57+
request_payload = type(request).to_json(request)
58+
elif isinstance(request, google.protobuf.message.Message):
59+
request_payload = MessageToJson(request)
60+
else:
61+
request_payload = f"{type(request).__name__}: {pickle.dumps(request)}"
62+
63+
request_metadata = {
64+
key: value.decode("utf-8") if isinstance(value, bytes) else value
65+
for key, value in request_metadata
66+
}
67+
grpc_request = {
68+
"payload": request_payload,
69+
"requestMethod": "grpc",
70+
"metadata": dict(request_metadata),
71+
}
72+
_LOGGER.debug(
73+
f"Sending request for {client_call_details.method}",
74+
extra={
75+
"serviceName": "google.pubsub.v1.Publisher",
76+
"rpcName": client_call_details.method,
77+
"request": grpc_request,
78+
"metadata": grpc_request["metadata"],
79+
},
80+
)
81+
82+
response = continuation(client_call_details, request)
83+
if logging_enabled: # pragma: NO COVER
84+
response_metadata = response.trailing_metadata()
85+
# Convert gRPC metadata `<class 'grpc.aio._metadata.Metadata'>` to list of tuples
86+
metadata = (
87+
dict([(k, str(v)) for k, v in response_metadata])
88+
if response_metadata
89+
else None
90+
)
91+
result = response.result()
92+
if isinstance(result, proto.Message):
93+
response_payload = type(result).to_json(result)
94+
elif isinstance(result, google.protobuf.message.Message):
95+
response_payload = MessageToJson(result)
96+
else:
97+
response_payload = f"{type(result).__name__}: {pickle.dumps(result)}"
98+
grpc_response = {
99+
"payload": response_payload,
100+
"metadata": metadata,
101+
"status": "OK",
102+
}
103+
_LOGGER.debug(
104+
f"Received response for {client_call_details.method}.",
105+
extra={
106+
"serviceName": "google.pubsub.v1.Publisher",
107+
"rpcName": client_call_details.method,
108+
"response": grpc_response,
109+
"metadata": grpc_response["metadata"],
110+
},
111+
)
112+
return response
113+
33114

34115
class PublisherGrpcTransport(PublisherTransport):
35116
"""gRPC backend transport for Publisher.
@@ -186,7 +267,12 @@ def __init__(
186267
],
187268
)
188269

189-
# Wrap messages. This must be done after self._grpc_channel exists
270+
self._interceptor = _LoggingClientInterceptor()
271+
self._logged_channel = grpc.intercept_channel(
272+
self._grpc_channel, self._interceptor
273+
)
274+
275+
# Wrap messages. This must be done after self._logged_channel exists
190276
self._prep_wrapped_messages(client_info)
191277

192278
@classmethod
@@ -260,7 +346,7 @@ def create_topic(self) -> Callable[[pubsub.Topic], pubsub.Topic]:
260346
# gRPC handles serialization and deserialization, so we just need
261347
# to pass in the functions for each.
262348
if "create_topic" not in self._stubs:
263-
self._stubs["create_topic"] = self.grpc_channel.unary_unary(
349+
self._stubs["create_topic"] = self._logged_channel.unary_unary(
264350
"/google.pubsub.v1.Publisher/CreateTopic",
265351
request_serializer=pubsub.Topic.serialize,
266352
response_deserializer=pubsub.Topic.deserialize,
@@ -286,7 +372,7 @@ def update_topic(self) -> Callable[[pubsub.UpdateTopicRequest], pubsub.Topic]:
286372
# gRPC handles serialization and deserialization, so we just need
287373
# to pass in the functions for each.
288374
if "update_topic" not in self._stubs:
289-
self._stubs["update_topic"] = self.grpc_channel.unary_unary(
375+
self._stubs["update_topic"] = self._logged_channel.unary_unary(
290376
"/google.pubsub.v1.Publisher/UpdateTopic",
291377
request_serializer=pubsub.UpdateTopicRequest.serialize,
292378
response_deserializer=pubsub.Topic.deserialize,
@@ -311,7 +397,7 @@ def publish(self) -> Callable[[pubsub.PublishRequest], pubsub.PublishResponse]:
311397
# gRPC handles serialization and deserialization, so we just need
312398
# to pass in the functions for each.
313399
if "publish" not in self._stubs:
314-
self._stubs["publish"] = self.grpc_channel.unary_unary(
400+
self._stubs["publish"] = self._logged_channel.unary_unary(
315401
"/google.pubsub.v1.Publisher/Publish",
316402
request_serializer=pubsub.PublishRequest.serialize,
317403
response_deserializer=pubsub.PublishResponse.deserialize,
@@ -335,7 +421,7 @@ def get_topic(self) -> Callable[[pubsub.GetTopicRequest], pubsub.Topic]:
335421
# gRPC handles serialization and deserialization, so we just need
336422
# to pass in the functions for each.
337423
if "get_topic" not in self._stubs:
338-
self._stubs["get_topic"] = self.grpc_channel.unary_unary(
424+
self._stubs["get_topic"] = self._logged_channel.unary_unary(
339425
"/google.pubsub.v1.Publisher/GetTopic",
340426
request_serializer=pubsub.GetTopicRequest.serialize,
341427
response_deserializer=pubsub.Topic.deserialize,
@@ -361,7 +447,7 @@ def list_topics(
361447
# gRPC handles serialization and deserialization, so we just need
362448
# to pass in the functions for each.
363449
if "list_topics" not in self._stubs:
364-
self._stubs["list_topics"] = self.grpc_channel.unary_unary(
450+
self._stubs["list_topics"] = self._logged_channel.unary_unary(
365451
"/google.pubsub.v1.Publisher/ListTopics",
366452
request_serializer=pubsub.ListTopicsRequest.serialize,
367453
response_deserializer=pubsub.ListTopicsResponse.deserialize,
@@ -390,7 +476,7 @@ def list_topic_subscriptions(
390476
# gRPC handles serialization and deserialization, so we just need
391477
# to pass in the functions for each.
392478
if "list_topic_subscriptions" not in self._stubs:
393-
self._stubs["list_topic_subscriptions"] = self.grpc_channel.unary_unary(
479+
self._stubs["list_topic_subscriptions"] = self._logged_channel.unary_unary(
394480
"/google.pubsub.v1.Publisher/ListTopicSubscriptions",
395481
request_serializer=pubsub.ListTopicSubscriptionsRequest.serialize,
396482
response_deserializer=pubsub.ListTopicSubscriptionsResponse.deserialize,
@@ -423,7 +509,7 @@ def list_topic_snapshots(
423509
# gRPC handles serialization and deserialization, so we just need
424510
# to pass in the functions for each.
425511
if "list_topic_snapshots" not in self._stubs:
426-
self._stubs["list_topic_snapshots"] = self.grpc_channel.unary_unary(
512+
self._stubs["list_topic_snapshots"] = self._logged_channel.unary_unary(
427513
"/google.pubsub.v1.Publisher/ListTopicSnapshots",
428514
request_serializer=pubsub.ListTopicSnapshotsRequest.serialize,
429515
response_deserializer=pubsub.ListTopicSnapshotsResponse.deserialize,
@@ -452,7 +538,7 @@ def delete_topic(self) -> Callable[[pubsub.DeleteTopicRequest], empty_pb2.Empty]
452538
# gRPC handles serialization and deserialization, so we just need
453539
# to pass in the functions for each.
454540
if "delete_topic" not in self._stubs:
455-
self._stubs["delete_topic"] = self.grpc_channel.unary_unary(
541+
self._stubs["delete_topic"] = self._logged_channel.unary_unary(
456542
"/google.pubsub.v1.Publisher/DeleteTopic",
457543
request_serializer=pubsub.DeleteTopicRequest.serialize,
458544
response_deserializer=empty_pb2.Empty.FromString,
@@ -484,13 +570,16 @@ def detach_subscription(
484570
# gRPC handles serialization and deserialization, so we just need
485571
# to pass in the functions for each.
486572
if "detach_subscription" not in self._stubs:
487-
self._stubs["detach_subscription"] = self.grpc_channel.unary_unary(
573+
self._stubs["detach_subscription"] = self._logged_channel.unary_unary(
488574
"/google.pubsub.v1.Publisher/DetachSubscription",
489575
request_serializer=pubsub.DetachSubscriptionRequest.serialize,
490576
response_deserializer=pubsub.DetachSubscriptionResponse.deserialize,
491577
)
492578
return self._stubs["detach_subscription"]
493579

580+
def close(self):
581+
self._logged_channel.close()
582+
494583
@property
495584
def set_iam_policy(
496585
self,
@@ -509,7 +598,7 @@ def set_iam_policy(
509598
# gRPC handles serialization and deserialization, so we just need
510599
# to pass in the functions for each.
511600
if "set_iam_policy" not in self._stubs:
512-
self._stubs["set_iam_policy"] = self.grpc_channel.unary_unary(
601+
self._stubs["set_iam_policy"] = self._logged_channel.unary_unary(
513602
"/google.iam.v1.IAMPolicy/SetIamPolicy",
514603
request_serializer=iam_policy_pb2.SetIamPolicyRequest.SerializeToString,
515604
response_deserializer=policy_pb2.Policy.FromString,
@@ -535,7 +624,7 @@ def get_iam_policy(
535624
# gRPC handles serialization and deserialization, so we just need
536625
# to pass in the functions for each.
537626
if "get_iam_policy" not in self._stubs:
538-
self._stubs["get_iam_policy"] = self.grpc_channel.unary_unary(
627+
self._stubs["get_iam_policy"] = self._logged_channel.unary_unary(
539628
"/google.iam.v1.IAMPolicy/GetIamPolicy",
540629
request_serializer=iam_policy_pb2.GetIamPolicyRequest.SerializeToString,
541630
response_deserializer=policy_pb2.Policy.FromString,
@@ -564,16 +653,13 @@ def test_iam_permissions(
564653
# gRPC handles serialization and deserialization, so we just need
565654
# to pass in the functions for each.
566655
if "test_iam_permissions" not in self._stubs:
567-
self._stubs["test_iam_permissions"] = self.grpc_channel.unary_unary(
656+
self._stubs["test_iam_permissions"] = self._logged_channel.unary_unary(
568657
"/google.iam.v1.IAMPolicy/TestIamPermissions",
569658
request_serializer=iam_policy_pb2.TestIamPermissionsRequest.SerializeToString,
570659
response_deserializer=iam_policy_pb2.TestIamPermissionsResponse.FromString,
571660
)
572661
return self._stubs["test_iam_permissions"]
573662

574-
def close(self):
575-
self.grpc_channel.close()
576-
577663
@property
578664
def kind(self) -> str:
579665
return "grpc"

0 commit comments

Comments
 (0)