Пример #1
0
    def validate_params(self):
        self.validate_subscribe_key()

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

        if self._include_meta is None:
            self._include_meta = False

        if self._include_message_actions is None:
            self._include_message_actions = False

        if self._include_message_actions is False:
            if self._maximum_per_channel is None or self._maximum_per_channel < FetchMessages.DEFAULT_MESSAGES:
                self._maximum_per_channel = FetchMessages.DEFAULT_MESSAGES
                logger.info("maximum_per_channel param defaulting to %d",
                            FetchMessages.DEFAULT_MESSAGES)
            elif self._maximum_per_channel > FetchMessages.MAX_MESSAGES:
                self._maximum_per_channel = FetchMessages.MAX_MESSAGES
                logger.info("maximum_per_channel param defaulting to %d",
                            FetchMessages.DEFAULT_MESSAGES)
        else:
            if len(self._channels) > 1:
                raise PubNubException(
                    pn_error=PNERR_HISTORY_MESSAGE_ACTIONS_MULTIPLE_CHANNELS)

            if self._maximum_per_channel is None or self._maximum_per_channel < 1 or\
                    self._maximum_per_channel > FetchMessages.MAX_MESSAGES_ACTIONS:
                self._maximum_per_channel = FetchMessages.MAX_MESSAGES_ACTIONS
                logger.info("maximum_per_channel param defaulting to %d",
                            FetchMessages.DEFAULT_MESSAGES)
Пример #2
0
    def validate_timetokens(self):

        if self._message_timetoken is None:
            raise PubNubException(pn_error=PNERR_MESSAGE_TIMETOKEN_MISSING)

        if self._action_timetoken is None:
            raise PubNubException(
                pn_error=PNERR_MESSAGE_ACTION_TIMETOKEN_MISSING)
    def validate_params(self):
        self.validate_subscribe_key()

        if not isinstance(self._device_id, six.string_types) or len(self._device_id) == 0:
            raise PubNubException(pn_error=PNERR_PUSH_DEVICE_MISSING)

        if self._push_type is None:
            raise PubNubException(pn_error=PNERROR_PUSH_TYPE_MISSING)
Пример #4
0
    def validate_params(self):
        self.validate_subscribe_key()

        if len(self._channels) == 0:
            raise PubNubException(pn_error=PNERR_CHANNELS_MISSING)

        if not isinstance(self._channel_group, str) or len(self._channel_group) == 0:
            raise PubNubException(pn_error=PNERR_GROUP_MISSING)
Пример #5
0
    def _invoke_request(self, p_options, e_options, base_origin):
        assert isinstance(p_options, PlatformOptions)
        assert isinstance(e_options, RequestOptions)

        if base_origin:
            url = p_options.pn_config.scheme(
            ) + "://" + base_origin + e_options.path
        else:
            url = e_options.path

        if e_options.request_headers:
            p_options.update(e_options.request_headers)

        args = {
            "method": e_options.method_string,
            "headers": p_options.headers,
            "url": url,
            "params": e_options.query_string,
            "timeout": (e_options.connect_timeout, e_options.request_timeout),
            "allow_redirects": e_options.allow_redirects
        }

        if e_options.is_post() or e_options.is_patch():
            args["data"] = e_options.data
            args["files"] = e_options.files
            logger.debug(
                "%s %s %s" %
                (e_options.method_string,
                 utils.build_url(p_options.pn_config.scheme(), base_origin,
                                 e_options.path,
                                 e_options.query_string), e_options.data))
        else:
            logger.debug(
                "%s %s" %
                (e_options.method_string,
                 utils.build_url(p_options.pn_config.scheme(), base_origin,
                                 e_options.path, e_options.query_string)))

        try:
            res = self.session.request(**args)
            logger.debug("GOT %s" % res.text)
        except requests.exceptions.ConnectionError as e:
            raise PubNubException(pn_error=PNERR_CONNECTION_ERROR,
                                  errormsg=str(e))
        except requests.exceptions.HTTPError as e:
            raise PubNubException(pn_error=PNERR_HTTP_ERROR, errormsg=str(e))
        except requests.exceptions.Timeout as e:
            raise PubNubException(pn_error=PNERR_CLIENT_TIMEOUT,
                                  errormsg=str(e))
        except requests.exceptions.TooManyRedirects as e:
            raise PubNubException(pn_error=PNERR_TOO_MANY_REDIRECTS_ERROR,
                                  errormsg=str(e))
        except Exception as e:
            raise PubNubException(pn_error=PNERR_UNKNOWN_ERROR,
                                  errormsg=str(e))

        return res
Пример #6
0
    def validate_params(self):
        self.validate_subscribe_key()
        self.validate_channels_and_groups()

        if len(self._channels) == 0 and len(self._groups) > 0:
            raise PubNubException(
                pn_error=PNERR_STATE_SETTER_FOR_GROUPS_NOT_SUPPORTED_YET)

        if self._state is None or not isinstance(self._state, dict):
            raise PubNubException(pn_error=PNERR_STATE_MISSING)
Пример #7
0
    def validate_params(self):
        self.validate_subscribe_key()

        if not isinstance(self._device_id, str) or len(self._device_id) == 0:
            raise PubNubException(pn_error=PNERR_PUSH_DEVICE_MISSING)

        if self._push_type is None:
            raise PubNubException(pn_error=PNERROR_PUSH_TYPE_MISSING)

        if self._push_type == PNPushType.APNS2:
            if not isinstance(self._topic, str) or len(self._topic) == 0:
                raise PubNubException(pn_error=PNERR_PUSH_TOPIC_MISSING)
Пример #8
0
    def _invoke_request(p_options, e_options, base_origin):
        assert isinstance(p_options, PlatformOptions)
        assert isinstance(e_options, RequestOptions)

        url = utils.build_url(p_options.pn_config.scheme(), base_origin,
                              e_options.path, e_options.query_string)

        args = {
            "method": e_options.method_string,
            'headers': p_options.headers,
            "url": url,
            'params': e_options.query_string,
            'timeout': (e_options.connect_timeout, e_options.request_timeout)
        }

        if e_options.is_post():
            args['data'] = e_options.data
            logger.debug("%s %s %s" % (e_options.method_string, url, e_options.data))
        else:
            logger.debug("%s %s" % (e_options.method_string, url))

        try:
            req = urllib.request.Request(url, e_options.data, p_options.headers)
            res = urllib.request.urlopen(req)
        except urllib.error.URLError as e:
            # For Python 2.6
            if isinstance(e.reason, socket.timeout):
                raise PubNubException(
                    pn_error=PNERR_CLIENT_TIMEOUT,
                    errormsg=str(e)
                )
            else:
                # TODO: wrap
                raise

        except urllib.error.HTTPError as e:
            raise PubNubException(
                pn_error=PNERR_HTTP_ERROR,
                errormsg=str(e)
            )
        except socket.timeout as e:
            raise PubNubException(
                pn_error=PNERR_CLIENT_TIMEOUT,
                errormsg=str(e)
            )
        except Exception as e:
            raise PubNubException(
                pn_error=PNERR_UNKNOWN_ERROR,
                errormsg=str(e)
            )

        return res
Пример #9
0
    def validate_message_action(self):
        if self._message_action is None:
            raise PubNubException(pn_error=PNERR_MESSAGE_ACTION_MISSING)

        if self._message_action.message_timetoken is None:
            raise PubNubException(pn_error=PNERR_MESSAGE_TIMETOKEN_MISSING)

        if self._message_action.type is None or len(
                self._message_action.type) == 0:
            raise PubNubException(pn_error=PNERR_MESSAGE_ACTION_TYPE_MISSING)

        if self._message_action.value is None or len(
                self._message_action.value) == 0:
            raise PubNubException(pn_error=PNERR_MESSAGE_ACTION_VALUE_MISSING)
Пример #10
0
    def build_data(self):
        params = {'ttl': str(self._ttl)}

        permissions = {}
        resources = {}
        patterns = {}

        utils.parse_resources(self._channels, "channels", resources, patterns)
        utils.parse_resources(self._groups, "groups", resources, patterns)
        utils.parse_resources(self._users, "users", resources, patterns)
        utils.parse_resources(self._spaces, "spaces", resources, patterns)

        permissions['resources'] = resources
        permissions['patterns'] = patterns

        if self._meta is not None:
            if isinstance(self._meta, dict):
                permissions['meta'] = self._meta
            else:
                raise PubNubException(pn_error=PNERR_INVALID_META)
        else:
            permissions['meta'] = {}

        params['permissions'] = permissions

        return utils.write_value_as_string(params)
Пример #11
0
    def validate_params(self):
        self.validate_subscribe_key()
        self.validate_secret_key()
        # self.validate_channels_and_groups()

        if self._write is None and self._read is None and self._manage is None:
            raise PubNubException(pn_error=PNERR_PAM_NO_FLAGS)
Пример #12
0
    def validate_params(self):
        self.validate_channel()

        if self._message is None:
            raise PubNubException(pn_error=PNERR_MESSAGE_MISSING)

        self.validate_subscribe_key()
        self.validate_publish_key()
Пример #13
0
    def validate_params(self):
        self.validate_subscribe_key()
        self.validate_secret_key()
        self.validate_publish_key()
        # self.validate_channels_and_groups()

        if self._channels and self._groups and self._uuids:
            raise PubNubException(
                pn_error=PNERR_PAM_INVALID_ARGUMENTS,
                errormsg="Grants for channels or channelGroups can't be changed together with grants for UUIDs")

        if self._uuids and not self._auth_keys:
            raise PubNubException(pn_error=PNERR_PAM_INVALID_ARGUMENTS, errormsg="UUIDs grant management require "
                                                                                 "providing non empty authKeys"
                                  )

        if self._write is None and self._read is None and self._manage is None and self._get is None \
                and self._update is None and self._join is None:
            raise PubNubException(pn_error=PNERR_PAM_NO_FLAGS)
Пример #14
0
    def validate_params(self):
        self.validate_subscribe_key()

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

        if self._include_meta is None:
            self._include_meta = False

        if self._include_message_actions is None:
            self._include_message_actions = False

        if not self._include_message_actions:
            if len(self._channels) == 1:
                if self._count is None or self._count < 1:
                    self._count = FetchMessages.DEFAULT_SINGLE_CHANNEL_MESSAGES
                    logger.info("count param defaulting to %d", self._count)
                elif self._count > FetchMessages.SINGLE_CHANNEL_MAX_MESSAGES:
                    self._count = FetchMessages.DEFAULT_SINGLE_CHANNEL_MESSAGES
                    logger.info("count param defaulting to %d", self._count)
            else:
                if self._count is None or self._count < 1:
                    self._count = FetchMessages.DEFAULT_MULTIPLE_CHANNELS_MESSAGES
                    logger.info("count param defaulting to %d", self._count)
                elif self._count > FetchMessages.MULTIPLE_CHANNELS_MAX_MESSAGES:
                    self._count = FetchMessages.DEFAULT_MULTIPLE_CHANNELS_MESSAGES
                    logger.info("count param defaulting to %d", self._count)
        else:
            if len(self._channels) > 1:
                raise PubNubException(
                    pn_error=PNERR_HISTORY_MESSAGE_ACTIONS_MULTIPLE_CHANNELS)

            if self._count is None or self._count < 1 or\
                    self._count > FetchMessages.MAX_MESSAGES_ACTIONS:
                self._count = FetchMessages.DEFAULT_MESSAGES_ACTIONS
                logger.info("count param defaulting to %d", self._count)
 def validate_params(self):
     self.validate_subscribe_key()
     if self._data is None:
         raise PubNubException('No data supplied.')
Пример #16
0
 def build_path(self):
     if self._space_id is None:
         raise PubNubException('Provide space id.')
     return DeleteSpace.DELETE_DELETE_PATH % (
         self.pubnub.config.subscribe_key, self._space_id)
 def build_path(self):
     if self._space_id is None:
         raise PubNubException('Provide space id.')
     return UpdateSpace.UPDATE_SPACE_PATH % (
         self.pubnub.config.subscribe_key, self._space_id)
Пример #18
0
    def validate_params(self):
        self.validate_subscribe_key()

        if len(self._channels) == 0 and len(self._groups) == 0:
            raise PubNubException(pn_error=PNERR_CHANNEL_OR_GROUP_MISSING)
Пример #19
0
 def validate_file_name(self):
     if not self._file_name:
         raise PubNubException(pn_error=PNERR_FILE_NAME_MISSING)
Пример #20
0
 def validate_publish_key(self):
     if self.pubnub.config.publish_key is None or len(
             self.pubnub.config.publish_key) == 0:
         raise PubNubException(pn_error=PNERR_PUBLISH_KEY_MISSING)
    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.')
Пример #22
0
 def validate_secret_key(self):
     if self.pubnub.config.secret_key is None or len(
             self.pubnub.config.secret_key) == 0:
         raise PubNubException(pn_error=PNERR_SECRET_KEY_MISSING)
Пример #23
0
 def validate_channels_and_groups(self):
     if len(self._channels) == 0 and len(self._groups) == 0:
         raise PubNubException(pn_error=PNERR_CHANNEL_OR_GROUP_MISSING)
Пример #24
0
 def validate_subscribe_key(self):
     if self.pubnub.config.subscribe_key is None or len(
             self.pubnub.config.subscribe_key) == 0:
         raise PubNubException(pn_error=PNERR_SUBSCRIBE_KEY_MISSING)
Пример #25
0
 def validate_file_id(self):
     if not self._file_id:
         raise PubNubException(pn_error=PNERR_FILE_ID_MISSING)
Пример #26
0
    def validate_params(self):
        self.validate_subscribe_key()

        if not isinstance(self._channel_group, six.string_types)\
                or len(self._channel_group) == 0:
            raise PubNubException(pn_error=PNERR_GROUP_MISSING)
Пример #27
0
 def _validate_channel(self):
     if self._channel is None or len(self._channel) == 0:
         raise PubNubException(pn_error=PNERR_CHANNEL_MISSING)
Пример #28
0
    def _build_envelope(self, p_options, e_options):
        """ A wrapper for _invoke_url to separate request logic """

        status_category = PNStatusCategory.PNUnknownCategory
        response_info = None

        try:
            res = self._invoke_request(p_options, e_options,
                                       self.pubnub.base_origin)
        except PubNubException as e:
            if e._pn_error is PNERR_CONNECTION_ERROR:
                status_category = PNStatusCategory.PNUnexpectedDisconnectCategory
            elif e._pn_error is PNERR_CLIENT_TIMEOUT:
                status_category = PNStatusCategory.PNTimeoutCategory

            return Envelope(result=None,
                            status=e_options.create_status(
                                category=status_category,
                                response=None,
                                response_info=response_info,
                                exception=e))

        if res is not None:
            url = six.moves.urllib.parse.urlparse(res.url)
            query = six.moves.urllib.parse.parse_qs(url.query)
            uuid = None
            auth_key = None

            if 'uuid' in query and len(query['uuid']) > 0:
                uuid = query['uuid'][0]

            if 'auth_key' in query and len(query['auth_key']) > 0:
                auth_key = query['auth_key'][0]

            response_info = ResponseInfo(status_code=res.status_code,
                                         tls_enabled='https' == url.scheme,
                                         origin=url.hostname,
                                         uuid=uuid,
                                         auth_key=auth_key,
                                         client_request=res.request)

        if res.status_code != requests.codes.ok:
            if res.status_code == 403:
                status_category = PNStatusCategory.PNAccessDeniedCategory

            if res.status_code == 400:
                status_category = PNStatusCategory.PNBadRequestCategory

            if res.text is None:
                text = "N/A"
            else:
                text = res.text

            if res.status_code >= 500:
                err = PNERR_SERVER_ERROR
            else:
                err = PNERR_CLIENT_ERROR

            return Envelope(result=None,
                            status=e_options.create_status(
                                category=status_category,
                                response=res.json(),
                                response_info=response_info,
                                exception=PubNubException(
                                    pn_error=err,
                                    errormsg=text,
                                    status_code=res.status_code)))
        else:
            return Envelope(
                result=e_options.create_response(res.json()),
                status=e_options.create_status(
                    category=PNStatusCategory.PNAcknowledgmentCategory,
                    response=res.json(),
                    response_info=response_info,
                    exception=None))
Пример #29
0
 def _validate_uuid(self):
     if self._effective_uuid() is None or len(self._effective_uuid()) == 0:
         raise PubNubException(pn_error=PNERR_UUID_MISSING)
Пример #30
0
 def build_path(self):
     if self._user_id is None:
         raise PubNubException('Provide user_id.')
     return DeleteUser.DELETE_USER_PATH % (self.pubnub.config.subscribe_key, self._user_id)