Skip to content

Commit ec0cc34

Browse files
gcf-owl-bot[bot]mukund-ananthuparthea
authored
feat: add use_topic_schema for Cloud Storage Subscriptions (#1154)
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: mukund-ananthu <83691193+mukund-ananthu@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 5869a74 commit ec0cc34

File tree

22 files changed

+5424
-960
lines changed

22 files changed

+5424
-960
lines changed

google/pubsub_v1/services/publisher/async_client.py

Lines changed: 87 additions & 169 deletions
Large diffs are not rendered by default.

google/pubsub_v1/services/publisher/client.py

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import re
2020
from typing import (
2121
Dict,
22+
Callable,
2223
Mapping,
2324
MutableMapping,
2425
MutableSequence,
@@ -579,7 +580,9 @@ def __init__(
579580
self,
580581
*,
581582
credentials: Optional[ga_credentials.Credentials] = None,
582-
transport: Optional[Union[str, PublisherTransport]] = None,
583+
transport: Optional[
584+
Union[str, PublisherTransport, Callable[..., PublisherTransport]]
585+
] = None,
583586
client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None,
584587
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
585588
) -> None:
@@ -591,9 +594,11 @@ def __init__(
591594
credentials identify the application to the service; if none
592595
are specified, the client will attempt to ascertain the
593596
credentials from the environment.
594-
transport (Union[str, PublisherTransport]): The
595-
transport to use. If set to None, a transport is chosen
596-
automatically.
597+
transport (Optional[Union[str,PublisherTransport,Callable[..., PublisherTransport]]]):
598+
The transport to use, or a Callable that constructs and returns a new transport.
599+
If a Callable is given, it will be called with the same set of initialization
600+
arguments as used in the PublisherTransport constructor.
601+
If set to None, a transport is chosen automatically.
597602
client_options (Optional[Union[google.api_core.client_options.ClientOptions, dict]]):
598603
Custom options for the client.
599604
@@ -699,17 +704,24 @@ def __init__(
699704
api_key_value
700705
)
701706

702-
Transport = type(self).get_transport_class(cast(str, transport))
707+
transport_init: Union[
708+
Type[PublisherTransport], Callable[..., PublisherTransport]
709+
] = (
710+
type(self).get_transport_class(transport)
711+
if isinstance(transport, str) or transport is None
712+
else cast(Callable[..., PublisherTransport], transport)
713+
)
714+
# initialize with the provided callable or the passed in class
703715

704716
emulator_host = os.environ.get("PUBSUB_EMULATOR_HOST")
705717
if emulator_host:
706-
if issubclass(Transport, type(self)._transport_registry["grpc"]):
718+
if issubclass(transport_init, type(self)._transport_registry["grpc"]):
707719
channel = grpc.insecure_channel(target=emulator_host)
708720
else:
709721
channel = grpc.aio.insecure_channel(target=emulator_host)
710-
Transport = functools.partial(Transport, channel=channel)
722+
transport_init = functools.partial(transport_init, channel=channel)
711723

712-
self._transport = Transport(
724+
self._transport = transport_init(
713725
credentials=credentials,
714726
credentials_file=self._client_options.credentials_file,
715727
host=self._api_endpoint,
@@ -788,19 +800,17 @@ def sample_create_topic():
788800
A topic resource.
789801
"""
790802
# Create or coerce a protobuf request object.
791-
# Quick check: If we got a request object, we should *not* have
792-
# gotten any keyword arguments that map to the request.
803+
# - Quick check: If we got a request object, we should *not* have
804+
# gotten any keyword arguments that map to the request.
793805
has_flattened_params = any([name])
794806
if request is not None and has_flattened_params:
795807
raise ValueError(
796808
"If the `request` argument is set, then none of "
797809
"the individual field arguments should be set."
798810
)
799811

800-
# Minor optimization to avoid making a copy if the user passes
801-
# in a pubsub.Topic.
802-
# There's no risk of modifying the input as we've already verified
803-
# there are no flattened fields.
812+
# - Use the request object if provided (there's no risk of modifying the input as
813+
# there are no flattened fields), or create one.
804814
if not isinstance(request, pubsub.Topic):
805815
request = pubsub.Topic(request)
806816
# If we have keyword arguments corresponding to fields on the
@@ -907,19 +917,17 @@ def sample_update_topic():
907917
A topic resource.
908918
"""
909919
# Create or coerce a protobuf request object.
910-
# Quick check: If we got a request object, we should *not* have
911-
# gotten any keyword arguments that map to the request.
920+
# - Quick check: If we got a request object, we should *not* have
921+
# gotten any keyword arguments that map to the request.
912922
has_flattened_params = any([topic, update_mask])
913923
if request is not None and has_flattened_params:
914924
raise ValueError(
915925
"If the `request` argument is set, then none of "
916926
"the individual field arguments should be set."
917927
)
918928

919-
# Minor optimization to avoid making a copy if the user passes
920-
# in a pubsub.UpdateTopicRequest.
921-
# There's no risk of modifying the input as we've already verified
922-
# there are no flattened fields.
929+
# - Use the request object if provided (there's no risk of modifying the input as
930+
# there are no flattened fields), or create one.
923931
if not isinstance(request, pubsub.UpdateTopicRequest):
924932
request = pubsub.UpdateTopicRequest(request)
925933
# If we have keyword arguments corresponding to fields on the
@@ -1022,19 +1030,17 @@ def sample_publish():
10221030
Response for the Publish method.
10231031
"""
10241032
# Create or coerce a protobuf request object.
1025-
# Quick check: If we got a request object, we should *not* have
1026-
# gotten any keyword arguments that map to the request.
1033+
# - Quick check: If we got a request object, we should *not* have
1034+
# gotten any keyword arguments that map to the request.
10271035
has_flattened_params = any([topic, messages])
10281036
if request is not None and has_flattened_params:
10291037
raise ValueError(
10301038
"If the `request` argument is set, then none of "
10311039
"the individual field arguments should be set."
10321040
)
10331041

1034-
# Minor optimization to avoid making a copy if the user passes
1035-
# in a pubsub.PublishRequest.
1036-
# There's no risk of modifying the input as we've already verified
1037-
# there are no flattened fields.
1042+
# - Use the request object if provided (there's no risk of modifying the input as
1043+
# there are no flattened fields), or create one.
10381044
if not isinstance(request, pubsub.PublishRequest):
10391045
request = pubsub.PublishRequest(request)
10401046
# If we have keyword arguments corresponding to fields on the
@@ -1127,19 +1133,17 @@ def sample_get_topic():
11271133
A topic resource.
11281134
"""
11291135
# Create or coerce a protobuf request object.
1130-
# Quick check: If we got a request object, we should *not* have
1131-
# gotten any keyword arguments that map to the request.
1136+
# - Quick check: If we got a request object, we should *not* have
1137+
# gotten any keyword arguments that map to the request.
11321138
has_flattened_params = any([topic])
11331139
if request is not None and has_flattened_params:
11341140
raise ValueError(
11351141
"If the `request` argument is set, then none of "
11361142
"the individual field arguments should be set."
11371143
)
11381144

1139-
# Minor optimization to avoid making a copy if the user passes
1140-
# in a pubsub.GetTopicRequest.
1141-
# There's no risk of modifying the input as we've already verified
1142-
# there are no flattened fields.
1145+
# - Use the request object if provided (there's no risk of modifying the input as
1146+
# there are no flattened fields), or create one.
11431147
if not isinstance(request, pubsub.GetTopicRequest):
11441148
request = pubsub.GetTopicRequest(request)
11451149
# If we have keyword arguments corresponding to fields on the
@@ -1235,19 +1239,17 @@ def sample_list_topics():
12351239
12361240
"""
12371241
# Create or coerce a protobuf request object.
1238-
# Quick check: If we got a request object, we should *not* have
1239-
# gotten any keyword arguments that map to the request.
1242+
# - Quick check: If we got a request object, we should *not* have
1243+
# gotten any keyword arguments that map to the request.
12401244
has_flattened_params = any([project])
12411245
if request is not None and has_flattened_params:
12421246
raise ValueError(
12431247
"If the `request` argument is set, then none of "
12441248
"the individual field arguments should be set."
12451249
)
12461250

1247-
# Minor optimization to avoid making a copy if the user passes
1248-
# in a pubsub.ListTopicsRequest.
1249-
# There's no risk of modifying the input as we've already verified
1250-
# there are no flattened fields.
1251+
# - Use the request object if provided (there's no risk of modifying the input as
1252+
# there are no flattened fields), or create one.
12511253
if not isinstance(request, pubsub.ListTopicsRequest):
12521254
request = pubsub.ListTopicsRequest(request)
12531255
# If we have keyword arguments corresponding to fields on the
@@ -1354,19 +1356,17 @@ def sample_list_topic_subscriptions():
13541356
13551357
"""
13561358
# Create or coerce a protobuf request object.
1357-
# Quick check: If we got a request object, we should *not* have
1358-
# gotten any keyword arguments that map to the request.
1359+
# - Quick check: If we got a request object, we should *not* have
1360+
# gotten any keyword arguments that map to the request.
13591361
has_flattened_params = any([topic])
13601362
if request is not None and has_flattened_params:
13611363
raise ValueError(
13621364
"If the `request` argument is set, then none of "
13631365
"the individual field arguments should be set."
13641366
)
13651367

1366-
# Minor optimization to avoid making a copy if the user passes
1367-
# in a pubsub.ListTopicSubscriptionsRequest.
1368-
# There's no risk of modifying the input as we've already verified
1369-
# there are no flattened fields.
1368+
# - Use the request object if provided (there's no risk of modifying the input as
1369+
# there are no flattened fields), or create one.
13701370
if not isinstance(request, pubsub.ListTopicSubscriptionsRequest):
13711371
request = pubsub.ListTopicSubscriptionsRequest(request)
13721372
# If we have keyword arguments corresponding to fields on the
@@ -1477,19 +1477,17 @@ def sample_list_topic_snapshots():
14771477
14781478
"""
14791479
# Create or coerce a protobuf request object.
1480-
# Quick check: If we got a request object, we should *not* have
1481-
# gotten any keyword arguments that map to the request.
1480+
# - Quick check: If we got a request object, we should *not* have
1481+
# gotten any keyword arguments that map to the request.
14821482
has_flattened_params = any([topic])
14831483
if request is not None and has_flattened_params:
14841484
raise ValueError(
14851485
"If the `request` argument is set, then none of "
14861486
"the individual field arguments should be set."
14871487
)
14881488

1489-
# Minor optimization to avoid making a copy if the user passes
1490-
# in a pubsub.ListTopicSnapshotsRequest.
1491-
# There's no risk of modifying the input as we've already verified
1492-
# there are no flattened fields.
1489+
# - Use the request object if provided (there's no risk of modifying the input as
1490+
# there are no flattened fields), or create one.
14931491
if not isinstance(request, pubsub.ListTopicSnapshotsRequest):
14941492
request = pubsub.ListTopicSnapshotsRequest(request)
14951493
# If we have keyword arguments corresponding to fields on the
@@ -1587,19 +1585,17 @@ def sample_delete_topic():
15871585
sent along with the request as metadata.
15881586
"""
15891587
# Create or coerce a protobuf request object.
1590-
# Quick check: If we got a request object, we should *not* have
1591-
# gotten any keyword arguments that map to the request.
1588+
# - Quick check: If we got a request object, we should *not* have
1589+
# gotten any keyword arguments that map to the request.
15921590
has_flattened_params = any([topic])
15931591
if request is not None and has_flattened_params:
15941592
raise ValueError(
15951593
"If the `request` argument is set, then none of "
15961594
"the individual field arguments should be set."
15971595
)
15981596

1599-
# Minor optimization to avoid making a copy if the user passes
1600-
# in a pubsub.DeleteTopicRequest.
1601-
# There's no risk of modifying the input as we've already verified
1602-
# there are no flattened fields.
1597+
# - Use the request object if provided (there's no risk of modifying the input as
1598+
# there are no flattened fields), or create one.
16031599
if not isinstance(request, pubsub.DeleteTopicRequest):
16041600
request = pubsub.DeleteTopicRequest(request)
16051601
# If we have keyword arguments corresponding to fields on the
@@ -1686,10 +1682,8 @@ def sample_detach_subscription():
16861682
16871683
"""
16881684
# Create or coerce a protobuf request object.
1689-
# Minor optimization to avoid making a copy if the user passes
1690-
# in a pubsub.DetachSubscriptionRequest.
1691-
# There's no risk of modifying the input as we've already verified
1692-
# there are no flattened fields.
1685+
# - Use the request object if provided (there's no risk of modifying the input as
1686+
# there are no flattened fields), or create one.
16931687
if not isinstance(request, pubsub.DetachSubscriptionRequest):
16941688
request = pubsub.DetachSubscriptionRequest(request)
16951689

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def __init__(
8888

8989
# Save the scopes.
9090
self._scopes = scopes
91+
if not hasattr(self, "_ignore_credentials"):
92+
self._ignore_credentials: bool = False
9193

9294
# If no credentials are provided, then determine the appropriate
9395
# defaults.
@@ -100,7 +102,7 @@ def __init__(
100102
credentials, _ = google.auth.load_credentials_from_file(
101103
credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
102104
)
103-
elif credentials is None:
105+
elif credentials is None and not self._ignore_credentials:
104106
credentials, _ = google.auth.default(
105107
**scopes_kwargs, quota_project_id=quota_project_id
106108
)

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(
5454
credentials: Optional[ga_credentials.Credentials] = None,
5555
credentials_file: Optional[str] = None,
5656
scopes: Optional[Sequence[str]] = None,
57-
channel: Optional[grpc.Channel] = None,
57+
channel: Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]] = None,
5858
api_mtls_endpoint: Optional[str] = None,
5959
client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None,
6060
ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None,
@@ -74,14 +74,17 @@ def __init__(
7474
credentials identify the application to the service; if none
7575
are specified, the client will attempt to ascertain the
7676
credentials from the environment.
77-
This argument is ignored if ``channel`` is provided.
77+
This argument is ignored if a ``channel`` instance is provided.
7878
credentials_file (Optional[str]): A file with credentials that can
7979
be loaded with :func:`google.auth.load_credentials_from_file`.
80-
This argument is ignored if ``channel`` is provided.
80+
This argument is ignored if a ``channel`` instance is provided.
8181
scopes (Optional(Sequence[str])): A list of scopes. This argument is
82-
ignored if ``channel`` is provided.
83-
channel (Optional[grpc.Channel]): A ``Channel`` instance through
84-
which to make calls.
82+
ignored if a ``channel`` instance is provided.
83+
channel (Optional[Union[grpc.Channel, Callable[..., grpc.Channel]]]):
84+
A ``Channel`` instance through which to make calls, or a Callable
85+
that constructs and returns one. If set to None, ``self.create_channel``
86+
is used to create the channel. If a Callable is given, it will be called
87+
with the same arguments as used in ``self.create_channel``.
8588
api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint.
8689
If provided, it overrides the ``host`` argument and tries to create
8790
a mutual TLS channel with client SSL credentials from
@@ -91,11 +94,11 @@ def __init__(
9194
private key bytes, both in PEM format. It is ignored if
9295
``api_mtls_endpoint`` is None.
9396
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
94-
for the grpc channel. It is ignored if ``channel`` is provided.
97+
for the grpc channel. It is ignored if a ``channel`` instance is provided.
9598
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
9699
A callback to provide client certificate bytes and private key bytes,
97100
both in PEM format. It is used to configure a mutual TLS channel. It is
98-
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
101+
ignored if a ``channel`` instance or ``ssl_channel_credentials`` is provided.
99102
quota_project_id (Optional[str]): An optional project to use for billing
100103
and quota.
101104
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -121,9 +124,10 @@ def __init__(
121124
if client_cert_source:
122125
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
123126

124-
if channel:
127+
if isinstance(channel, grpc.Channel):
125128
# Ignore credentials if a channel was passed.
126-
credentials = False
129+
credentials = None
130+
self._ignore_credentials = True
127131
# If a channel was explicitly provided, set it.
128132
self._grpc_channel = channel
129133
self._ssl_channel_credentials = None
@@ -162,7 +166,9 @@ def __init__(
162166
)
163167

164168
if not self._grpc_channel:
165-
self._grpc_channel = type(self).create_channel(
169+
# initialize with the provided callable or the default channel
170+
channel_init = channel or type(self).create_channel
171+
self._grpc_channel = channel_init(
166172
self._host,
167173
# use the credentials which are saved
168174
credentials=self._credentials,

0 commit comments

Comments
 (0)