|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import google.api_core.grpc_helpers |
| 16 | + |
| 17 | +from google.cloud.pubsub_v1.proto import pubsub_pb2_grpc |
| 18 | +from google.iam.v1 import iam_policy_pb2 |
| 19 | + |
| 20 | + |
| 21 | +class PublisherGrpcTransport(object): |
| 22 | + """gRPC transport class providing stubs for |
| 23 | + google.pubsub.v1 Publisher API. |
| 24 | +
|
| 25 | + The transport provides access to the raw gRPC stubs, |
| 26 | + which can be used to take advantage of advanced |
| 27 | + features of gRPC. |
| 28 | + """ |
| 29 | + # The scopes needed to make gRPC calls to all of the methods defined |
| 30 | + # in this service. |
| 31 | + _OAUTH_SCOPES = ( |
| 32 | + 'https://www.googleapis.com/auth/cloud-platform', |
| 33 | + 'https://www.googleapis.com/auth/pubsub', |
| 34 | + ) |
| 35 | + |
| 36 | + def __init__(self, |
| 37 | + channel=None, |
| 38 | + credentials=None, |
| 39 | + address='pubsub.googleapis.com:443'): |
| 40 | + """Instantiate the transport class. |
| 41 | +
|
| 42 | + Args: |
| 43 | + channel (grpc.Channel): A ``Channel`` instance through |
| 44 | + which to make calls. This argument is mutually exclusive |
| 45 | + with ``credentials``; providing both will raise an exception. |
| 46 | + credentials (google.auth.credentials.Credentials): The |
| 47 | + authorization credentials to attach to requests. These |
| 48 | + credentials identify this application to the service. If none |
| 49 | + are specified, the client will attempt to ascertain the |
| 50 | + credentials from the environment. |
| 51 | + address (str): The address where the service is hosted. |
| 52 | + """ |
| 53 | + # If both `channel` and `credentials` are specified, raise an |
| 54 | + # exception (channels come with credentials baked in already). |
| 55 | + if channel is not None and credentials is not None: |
| 56 | + raise ValueError( |
| 57 | + 'The `channel` and `credentials` arguments are mutually ' |
| 58 | + 'exclusive.', ) |
| 59 | + |
| 60 | + # Create the channel. |
| 61 | + if channel is None: |
| 62 | + channel = self.create_channel( |
| 63 | + address=address, |
| 64 | + credentials=credentials, |
| 65 | + ) |
| 66 | + |
| 67 | + # gRPC uses objects called "stubs" that are bound to the |
| 68 | + # channel and provide a basic method for each RPC. |
| 69 | + self._stubs = { |
| 70 | + 'iam_policy_stub': iam_policy_pb2.IAMPolicyStub(channel), |
| 71 | + 'publisher_stub': pubsub_pb2_grpc.PublisherStub(channel), |
| 72 | + } |
| 73 | + |
| 74 | + @classmethod |
| 75 | + def create_channel(cls, |
| 76 | + address='pubsub.googleapis.com:443', |
| 77 | + credentials=None): |
| 78 | + """Create and return a gRPC channel object. |
| 79 | +
|
| 80 | + Args: |
| 81 | + address (str): The host for the channel to use. |
| 82 | + credentials (~.Credentials): The |
| 83 | + authorization credentials to attach to requests. These |
| 84 | + credentials identify this application to the service. If |
| 85 | + none are specified, the client will attempt to ascertain |
| 86 | + the credentials from the environment. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + grpc.Channel: A gRPC channel object. |
| 90 | + """ |
| 91 | + return google.api_core.grpc_helpers.create_channel( |
| 92 | + address, |
| 93 | + credentials=credentials, |
| 94 | + scopes=cls._OAUTH_SCOPES, |
| 95 | + ) |
| 96 | + |
| 97 | + @property |
| 98 | + def create_topic(self): |
| 99 | + """Return the gRPC stub for {$apiMethod.name}. |
| 100 | +
|
| 101 | + Creates the given topic with the given name. See the |
| 102 | + <a href=\"/pubsub/docs/admin#resource_names\"> resource name rules</a>. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + Callable: A callable which accepts the appropriate |
| 106 | + deserialized request object and returns a |
| 107 | + deserialized response object. |
| 108 | + """ |
| 109 | + return self._stubs['publisher_stub'].CreateTopic |
| 110 | + |
| 111 | + @property |
| 112 | + def update_topic(self): |
| 113 | + """Return the gRPC stub for {$apiMethod.name}. |
| 114 | +
|
| 115 | + Updates an existing topic. Note that certain properties of a |
| 116 | + topic are not modifiable. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + Callable: A callable which accepts the appropriate |
| 120 | + deserialized request object and returns a |
| 121 | + deserialized response object. |
| 122 | + """ |
| 123 | + return self._stubs['publisher_stub'].UpdateTopic |
| 124 | + |
| 125 | + @property |
| 126 | + def publish(self): |
| 127 | + """Return the gRPC stub for {$apiMethod.name}. |
| 128 | +
|
| 129 | + Adds one or more messages to the topic. Returns ``NOT_FOUND`` if the topic |
| 130 | + does not exist. The message payload must not be empty; it must contain |
| 131 | + either a non-empty data field, or at least one attribute. |
| 132 | +
|
| 133 | + Returns: |
| 134 | + Callable: A callable which accepts the appropriate |
| 135 | + deserialized request object and returns a |
| 136 | + deserialized response object. |
| 137 | + """ |
| 138 | + return self._stubs['publisher_stub'].Publish |
| 139 | + |
| 140 | + @property |
| 141 | + def get_topic(self): |
| 142 | + """Return the gRPC stub for {$apiMethod.name}. |
| 143 | +
|
| 144 | + Gets the configuration of a topic. |
| 145 | +
|
| 146 | + Returns: |
| 147 | + Callable: A callable which accepts the appropriate |
| 148 | + deserialized request object and returns a |
| 149 | + deserialized response object. |
| 150 | + """ |
| 151 | + return self._stubs['publisher_stub'].GetTopic |
| 152 | + |
| 153 | + @property |
| 154 | + def list_topics(self): |
| 155 | + """Return the gRPC stub for {$apiMethod.name}. |
| 156 | +
|
| 157 | + Lists matching topics. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Callable: A callable which accepts the appropriate |
| 161 | + deserialized request object and returns a |
| 162 | + deserialized response object. |
| 163 | + """ |
| 164 | + return self._stubs['publisher_stub'].ListTopics |
| 165 | + |
| 166 | + @property |
| 167 | + def list_topic_subscriptions(self): |
| 168 | + """Return the gRPC stub for {$apiMethod.name}. |
| 169 | +
|
| 170 | + Lists the names of the subscriptions on this topic. |
| 171 | +
|
| 172 | + Returns: |
| 173 | + Callable: A callable which accepts the appropriate |
| 174 | + deserialized request object and returns a |
| 175 | + deserialized response object. |
| 176 | + """ |
| 177 | + return self._stubs['publisher_stub'].ListTopicSubscriptions |
| 178 | + |
| 179 | + @property |
| 180 | + def delete_topic(self): |
| 181 | + """Return the gRPC stub for {$apiMethod.name}. |
| 182 | +
|
| 183 | + Deletes the topic with the given name. Returns ``NOT_FOUND`` if the topic |
| 184 | + does not exist. After a topic is deleted, a new topic may be created with |
| 185 | + the same name; this is an entirely new topic with none of the old |
| 186 | + configuration or subscriptions. Existing subscriptions to this topic are |
| 187 | + not deleted, but their ``topic`` field is set to ``_deleted-topic_``. |
| 188 | +
|
| 189 | + Returns: |
| 190 | + Callable: A callable which accepts the appropriate |
| 191 | + deserialized request object and returns a |
| 192 | + deserialized response object. |
| 193 | + """ |
| 194 | + return self._stubs['publisher_stub'].DeleteTopic |
| 195 | + |
| 196 | + @property |
| 197 | + def set_iam_policy(self): |
| 198 | + """Return the gRPC stub for {$apiMethod.name}. |
| 199 | +
|
| 200 | + Sets the access control policy on the specified resource. Replaces any |
| 201 | + existing policy. |
| 202 | +
|
| 203 | + Returns: |
| 204 | + Callable: A callable which accepts the appropriate |
| 205 | + deserialized request object and returns a |
| 206 | + deserialized response object. |
| 207 | + """ |
| 208 | + return self._stubs['iam_policy_stub'].SetIamPolicy |
| 209 | + |
| 210 | + @property |
| 211 | + def get_iam_policy(self): |
| 212 | + """Return the gRPC stub for {$apiMethod.name}. |
| 213 | +
|
| 214 | + Gets the access control policy for a resource. |
| 215 | + Returns an empty policy if the resource exists and does not have a policy |
| 216 | + set. |
| 217 | +
|
| 218 | + Returns: |
| 219 | + Callable: A callable which accepts the appropriate |
| 220 | + deserialized request object and returns a |
| 221 | + deserialized response object. |
| 222 | + """ |
| 223 | + return self._stubs['iam_policy_stub'].GetIamPolicy |
| 224 | + |
| 225 | + @property |
| 226 | + def test_iam_permissions(self): |
| 227 | + """Return the gRPC stub for {$apiMethod.name}. |
| 228 | +
|
| 229 | + Returns permissions that a caller has on the specified resource. |
| 230 | + If the resource does not exist, this will return an empty set of |
| 231 | + permissions, not a NOT_FOUND error. |
| 232 | +
|
| 233 | + Returns: |
| 234 | + Callable: A callable which accepts the appropriate |
| 235 | + deserialized request object and returns a |
| 236 | + deserialized response object. |
| 237 | + """ |
| 238 | + return self._stubs['iam_policy_stub'].TestIamPermissions |
0 commit comments