File: objects_endpoint.py

package info (click to toggle)
freenub 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: python: 10,664; makefile: 7; sh: 6
file content (214 lines) | stat: -rw-r--r-- 5,730 bytes parent folder | download
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
209
210
211
212
213
214
import logging
from abc import ABCMeta

from pubnub import utils
from pubnub.endpoints.endpoint import Endpoint
from pubnub.errors import PNERR_CHANNEL_MISSING, PNERR_UUID_MISSING
from pubnub.exceptions import PubNubException
from pubnub.models.consumer.objects_v2.page import Next, Previous

logger = logging.getLogger("pubnub")


class ObjectsEndpoint(Endpoint):
    __metaclass__ = ABCMeta

    def __init__(self, pubnub):
        Endpoint.__init__(self, pubnub)

    def is_auth_required(self):
        return True

    def connect_timeout(self):
        return self.pubnub.config.connect_timeout

    def request_timeout(self):
        return self.pubnub.config.non_subscribe_request_timeout

    def validate_params(self):
        self.validate_subscribe_key()
        self.validate_specific_params()

    def validate_specific_params(self):
        pass

    def encoded_params(self):
        params = {}
        if isinstance(self, ListEndpoint):
            if self._filter:
                params["filter"] = utils.url_encode(str(self._filter))
        return params

    def custom_params(self):
        params = {}
        inclusions = []

        if isinstance(self, IncludeCustomEndpoint):
            if self._include_custom:
                inclusions.append("custom")

        if isinstance(self, UUIDIncludeEndpoint):
            if self._uuid_details_level:
                if self._uuid_details_level == UUIDIncludeEndpoint.UUID:
                    inclusions.append("uuid")
                elif self._uuid_details_level == UUIDIncludeEndpoint.UUID_WITH_CUSTOM:
                    inclusions.append("uuid.custom")

        if isinstance(self, ChannelIncludeEndpoint):
            if self._channel_details_level:
                if self._channel_details_level == ChannelIncludeEndpoint.CHANNEL:
                    inclusions.append("channel")
                elif (
                    self._channel_details_level
                    == ChannelIncludeEndpoint.CHANNEL_WITH_CUSTOM
                ):
                    inclusions.append("channel.custom")

        if isinstance(self, ListEndpoint):
            if self._filter:
                params["filter"] = str(self._filter)

            if self._limit:
                params["limit"] = int(self._limit)

            if self._include_total_count:
                params["count"] = bool(self._include_total_count)

            if self._sort_keys:
                joined_sort_params_array = []
                for sort_key in self._sort_keys:
                    joined_sort_params_array.append(
                        f"{sort_key.key_str()}:{sort_key.dir_str()}"
                    )

                params["sort"] = ",".join(joined_sort_params_array)

            if self._page:
                if isinstance(self._page, Next):
                    params["start"] = self._page.hash
                elif isinstance(self._page, Previous):
                    params["end"] = self._page.hash
                else:
                    raise ValueError()

        if len(inclusions) > 0:
            params["include"] = ",".join(inclusions)

        return params


class CustomAwareEndpoint:
    __metaclass__ = ABCMeta

    def __init__(self):
        self._custom = None

    def custom(self, custom):
        self._custom = dict(custom)
        return self


class ChannelEndpoint:
    __metaclass__ = ABCMeta

    def __init__(self):
        self._channel = None

    def channel(self, channel):
        self._channel = str(channel)
        return self

    def _validate_channel(self):
        if self._channel is None or len(self._channel) == 0:
            raise PubNubException(pn_error=PNERR_CHANNEL_MISSING)


class UuidEndpoint:
    __metaclass__ = ABCMeta

    def __init__(self):
        self._uuid = None

    def uuid(self, uuid):
        self._uuid = str(uuid)
        return self

    def _effective_uuid(self):
        if self._uuid is not None:
            return self._uuid
        else:
            return self.pubnub.config.uuid

    def _validate_uuid(self):
        if self._effective_uuid() is None or len(self._effective_uuid()) == 0:
            raise PubNubException(pn_error=PNERR_UUID_MISSING)


class ListEndpoint:
    __metaclass__ = ABCMeta

    def __init__(self):
        self._limit = None
        self._filter = None
        self._include_total_count = None
        self._sort_keys = None
        self._page = None

    def limit(self, limit):
        self._limit = int(limit)
        return self

    def filter(self, filter):
        self._filter = str(filter)
        return self

    def include_total_count(self, include_total_count):
        self._include_total_count = bool(include_total_count)
        return self

    def sort(self, *sort_keys):
        self._sort_keys = sort_keys
        return self

    def page(self, page):
        self._page = page
        return self


class IncludeCustomEndpoint:
    __metaclass__ = ABCMeta

    def __init__(self):
        self._include_custom = None

    def include_custom(self, include_custom):
        self._include_custom = bool(include_custom)
        return self


class UUIDIncludeEndpoint:
    __metaclass__ = ABCMeta

    UUID = 1
    UUID_WITH_CUSTOM = 2

    def __init__(self):
        self._uuid_details_level = None

    def include_uuid(self, uuid_details_level):
        self._uuid_details_level = uuid_details_level
        return self


class ChannelIncludeEndpoint:
    __metaclass__ = ABCMeta

    CHANNEL = 1
    CHANNEL_WITH_CUSTOM = 2

    def __init__(self):
        self._channel_details_level = None

    def include_channel(self, channel_details_level):
        self._channel_details_level = channel_details_level
        return self