|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 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 | +# http://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 | +"""Convenience proxies |
| 16 | +
|
| 17 | +Define wrappers for ``api`` functions, :class:`gcloud.pubsub.topic.Topic`, and |
| 18 | +:class:`gcloud.pubsub.subscription.Subscription`, passing the memoized |
| 19 | +connection / project as needed. |
| 20 | +""" |
| 21 | + |
| 22 | +from gcloud._helpers import get_default_project |
| 23 | +from gcloud._helpers import _ClientProxy |
| 24 | +from gcloud.pubsub._implicit_environ import _require_connection |
| 25 | +from gcloud.pubsub import api |
| 26 | +from gcloud.pubsub.subscription import Subscription |
| 27 | +from gcloud.pubsub.topic import Topic |
| 28 | + |
| 29 | + |
| 30 | +class Client(object): |
| 31 | + """Wrap :mod:`gcloud.pubsub` API objects. |
| 32 | +
|
| 33 | + :type connection: :class:`gcloud.pubsub.connection.Connection` or None |
| 34 | + :param connection: The configured connection. Defaults to one inferred |
| 35 | + from the environment. |
| 36 | +
|
| 37 | + :type project: str or None |
| 38 | + :param connection: The configured project. Defaults to the value inferred |
| 39 | + from the environment. |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__(self, connection=None, project=None): |
| 43 | + self.connection = _require_connection(connection) |
| 44 | + if project is None: |
| 45 | + project = get_default_project() |
| 46 | + self.project = project |
| 47 | + |
| 48 | + def topic(self, name): |
| 49 | + """Proxy for :class:`gcloud.pubsub.topic.Topic`. |
| 50 | +
|
| 51 | + :type name: string |
| 52 | + :param name: the name of the topic |
| 53 | +
|
| 54 | + :rtype: :class:`_Topic` |
| 55 | + :returns: a proxy for a newly created Topic, using the passed name |
| 56 | + and the client's project. |
| 57 | + """ |
| 58 | + topic = Topic(name, self.project) |
| 59 | + return _Topic(topic, self) |
| 60 | + |
| 61 | + def list_topics(self, page_size=None, page_token=None): |
| 62 | + """Proxy for :func:`gcloud.pubsub.api.list_topics`. |
| 63 | +
|
| 64 | + Passes configured connection and project. |
| 65 | + """ |
| 66 | + topics, next_page_token = api.list_topics( |
| 67 | + page_size=page_size, |
| 68 | + page_token=page_token, |
| 69 | + connection=self.connection, |
| 70 | + project=self.project) |
| 71 | + proxies = [_Topic(topic, self) for topic in topics] |
| 72 | + return proxies, next_page_token |
| 73 | + |
| 74 | + def list_subscriptions(self, page_size=None, page_token=None, |
| 75 | + topic_name=None): |
| 76 | + """Proxy for :func:`gcloud.pubsub.api.list_subscriptions`. |
| 77 | +
|
| 78 | + Passes configured connection and project. |
| 79 | + """ |
| 80 | + subscriptions, next_page_token = api.list_subscriptions( |
| 81 | + page_size=page_size, |
| 82 | + page_token=page_token, |
| 83 | + topic_name=topic_name, |
| 84 | + connection=self.connection, |
| 85 | + project=self.project) |
| 86 | + topics = dict([(sub.topic.name, _Topic(sub.topic, self)) |
| 87 | + for sub in subscriptions]) |
| 88 | + proxies = [ |
| 89 | + _Subscription(sub, self, topics[sub.topic.name]) |
| 90 | + for sub in subscriptions] |
| 91 | + return proxies, next_page_token |
| 92 | + |
| 93 | + |
| 94 | +class _Topic(_ClientProxy): |
| 95 | + """Proxy for :class:`gcloud.pubsub.topic.Topic`. |
| 96 | +
|
| 97 | + :type wrapped: :class:`gcloud.pubsub.topic.Topic` |
| 98 | + :param wrapped: Topic being proxied. |
| 99 | +
|
| 100 | + :type client: :class:`gcloud.pubsub.client.Client` |
| 101 | + :param client: Client used to pass connection / project. |
| 102 | + """ |
| 103 | + def subscription(self, name, ack_deadline=None, push_endpoint=None): |
| 104 | + """ Proxy through to :class:`gcloud.pubsub.subscription.Subscription`. |
| 105 | +
|
| 106 | + :rtype: :class:`_Subscription` |
| 107 | + """ |
| 108 | + subscription = Subscription( |
| 109 | + name, |
| 110 | + self._wrapped, |
| 111 | + ack_deadline=ack_deadline, |
| 112 | + push_endpoint=push_endpoint) |
| 113 | + return _Subscription(subscription, self._client, self) |
| 114 | + |
| 115 | + |
| 116 | +class _Subscription(_ClientProxy): |
| 117 | + """Proxy for :class:`gcloud.pubsub.subscription.Subscription`. |
| 118 | +
|
| 119 | + :type wrapped: :class:`gcloud.pubsub.topic.Subscription` |
| 120 | + :param wrapped: Subscription being proxied. |
| 121 | +
|
| 122 | + :type client: :class:`gcloud.pubsub.client.Client` |
| 123 | + :param client: Client used to pass connection / project. |
| 124 | +
|
| 125 | + :type topic: :class:`gcloud.pubsub.client._Topic` |
| 126 | + :param topic: proxy for the wrapped subscription's topic. |
| 127 | + """ |
| 128 | + def __init__(self, wrapped, client, topic): |
| 129 | + super(_Subscription, self).__init__(wrapped, client) |
| 130 | + self.topic = topic |
0 commit comments