forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
208 lines (170 loc) · 7.86 KB
/
Copy pathclient.py
File metadata and controls
208 lines (170 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Client for interacting with the Google Cloud Pub/Sub API."""
import os
from google.cloud.client import ClientWithProject
from google.cloud.environment_vars import DISABLE_GRPC
from google.cloud.pubsub._http import Connection
from google.cloud.pubsub._http import _PublisherAPI as JSONPublisherAPI
from google.cloud.pubsub._http import _SubscriberAPI as JSONSubscriberAPI
from google.cloud.pubsub._http import _IAMPolicyAPI
from google.cloud.pubsub.topic import Topic
try:
from google.cloud.pubsub._gax import _PublisherAPI as GAXPublisherAPI
from google.cloud.pubsub._gax import _SubscriberAPI as GAXSubscriberAPI
from google.cloud.pubsub._gax import make_gax_publisher_api
from google.cloud.pubsub._gax import make_gax_subscriber_api
except ImportError: # pragma: NO COVER
_HAVE_GAX = False
GAXPublisherAPI = None
GAXSubscriberAPI = None
make_gax_publisher_api = None
make_gax_subscriber_api = None
else:
_HAVE_GAX = True
_DISABLE_GAX = os.getenv(DISABLE_GRPC, False)
_USE_GAX = _HAVE_GAX and not _DISABLE_GAX
class Client(ClientWithProject):
"""Client to bundle configuration needed for API requests.
:type project: str
:param project: the project which the client acts on behalf of. Will be
passed when creating a topic. If not passed,
falls back to the default inferred from the environment.
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
passed), falls back to the default inferred from the
environment.
:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
:type use_gax: bool
:param use_gax: (Optional) Explicitly specifies whether
to use the gRPC transport (via GAX) or HTTP. If unset,
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
variable
"""
_publisher_api = None
_subscriber_api = None
_iam_policy_api = None
SCOPE = ('https://www.googleapis.com/auth/pubsub',
'https://www.googleapis.com/auth/cloud-platform')
"""The scopes required for authenticating as a Cloud Pub/Sub consumer."""
def __init__(self, project=None, credentials=None,
http=None, use_gax=None):
super(Client, self).__init__(
project=project, credentials=credentials, http=http)
self._connection = Connection(self)
if use_gax is None:
self._use_gax = _USE_GAX
else:
self._use_gax = use_gax
@property
def publisher_api(self):
"""Helper for publisher-related API calls."""
if self._publisher_api is None:
if self._use_gax:
if self._connection.in_emulator:
generated = make_gax_publisher_api(
host=self._connection.host)
else:
generated = make_gax_publisher_api(
credentials=self._credentials)
self._publisher_api = GAXPublisherAPI(generated, self)
else:
self._publisher_api = JSONPublisherAPI(self)
return self._publisher_api
@property
def subscriber_api(self):
"""Helper for subscriber-related API calls."""
if self._subscriber_api is None:
if self._use_gax:
if self._connection.in_emulator:
generated = make_gax_subscriber_api(
host=self._connection.host)
else:
generated = make_gax_subscriber_api(
credentials=self._credentials)
self._subscriber_api = GAXSubscriberAPI(generated, self)
else:
self._subscriber_api = JSONSubscriberAPI(self)
return self._subscriber_api
@property
def iam_policy_api(self):
"""Helper for IAM policy-related API calls."""
if self._iam_policy_api is None:
self._iam_policy_api = _IAMPolicyAPI(self)
return self._iam_policy_api
def list_topics(self, page_size=None, page_token=None):
"""List topics for the project associated with this client.
See:
https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list
Example:
.. literalinclude:: pubsub_snippets.py
:start-after: [START client_list_topics]
:end-before: [END client_list_topics]
:type page_size: int
:param page_size: maximum number of topics to return, If not passed,
defaults to a value set by the API.
:type page_token: str
:param page_token: opaque marker for the next "page" of topics. If not
passed, the API will return the first page of
topics.
:rtype: :class:`~google.cloud.iterator.Iterator`
:returns: Iterator of :class:`~google.cloud.pubsub.topic.Topic`
accessible to the current API.
"""
api = self.publisher_api
return api.list_topics(
self.project, page_size, page_token)
def list_subscriptions(self, page_size=None, page_token=None):
"""List subscriptions for the project associated with this client.
See:
https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list
Example:
.. literalinclude:: pubsub_snippets.py
:start-after: [START client_list_subscriptions]
:end-before: [END client_list_subscriptions]
:type page_size: int
:param page_size: maximum number of topics to return, If not passed,
defaults to a value set by the API.
:type page_token: str
:param page_token: opaque marker for the next "page" of topics. If not
passed, the API will return the first page of
topics.
:rtype: :class:`~google.cloud.iterator.Iterator`
:returns: Iterator of
:class:`~google.cloud.pubsub.subscription.Subscription`
accessible to the current client.
"""
api = self.subscriber_api
return api.list_subscriptions(
self.project, page_size, page_token)
def topic(self, name, timestamp_messages=False):
"""Creates a topic bound to the current client.
Example:
.. literalinclude:: pubsub_snippets.py
:start-after: [START client_topic]
:end-before: [END client_topic]
:type name: str
:param name: the name of the topic to be constructed.
:type timestamp_messages: bool
:param timestamp_messages: To be passed to ``Topic`` constructor.
:rtype: :class:`google.cloud.pubsub.topic.Topic`
:returns: Topic created with the current client.
"""
return Topic(name, client=self, timestamp_messages=timestamp_messages)