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
|
from pubnub import utils
from pubnub.endpoints.endpoint import Endpoint
from pubnub.enums import HttpMethod, PNOperationType
from pubnub.exceptions import PubNubException
from pubnub.models.consumer.message_count import PNMessageCountResult
class MessageCount(Endpoint):
MESSAGE_COUNT_PATH = "/v3/history/sub-key/%s/message-counts/%s"
def __init__(self, pubnub):
Endpoint.__init__(self, pubnub)
self._channel = []
self._channels_timetoken = []
def channel(self, channel):
utils.extend_list(self._channel, channel)
return self
def channel_timetokens(self, timetokens):
timetokens = [str(item) for item in timetokens]
utils.extend_list(self._channels_timetoken, timetokens)
return self
def custom_params(self):
params = {}
if len(self._channels_timetoken) > 0:
if len(self._channels_timetoken) > 1:
params["channelsTimetoken"] = utils.join_items(self._channels_timetoken)
else:
params["timetoken"] = self._channels_timetoken[0]
return params
def build_path(self):
return MessageCount.MESSAGE_COUNT_PATH % (
self.pubnub.config.subscribe_key,
utils.join_channels(self._channel),
)
def http_method(self):
return HttpMethod.GET
def is_auth_required(self):
return True
def validate_params(self):
self.validate_subscribe_key()
self.validate_channel()
if len(self._channels_timetoken) != len(self._channel):
raise PubNubException(
"The number of channels and the number of timetokens do not match."
)
def create_response(self, result): # pylint: disable=W0221
return PNMessageCountResult(result)
def request_timeout(self):
return self.pubnub.config.non_subscribe_request_timeout
def connect_timeout(self):
return self.pubnub.config.connect_timeout
def operation_type(self):
return PNOperationType.PNMessageCountOperation
def name(self):
return "Message Count"
|