def __init__(self, platform_type=None, priority=None, collapse_key=None):
        """
        Args:
            platform_type (int): Type for the platform payload - can be None when using for default payload.
            priority (int): Priority for push notification - may be None.
            collapse_key (string): Collapse key for push notification - may be None.
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that our platform_type looks right
        if platform_type:
            validate_is_type(int, not_empty=False, platform_type=platform_type)
            PlatformPayload._validate_platform_type(platform_type)
        self.platform_type = platform_type

        # Check that our priority looks right
        if priority:
            validate_is_type(int, not_empty=False, priority=priority)
            if priority not in PlatformPayloadPriority.types:
                raise TypeError("Unsupported PlatformPayload priority: {}".format(priority))
        self.priority = priority

        # Check that our collapse key looks right
        if collapse_key:
            validate_is_string(collapse_key=collapse_key)
        self.collapse_key = collapse_key
    def __init__(self, platform_type=None, priority=None, collapse_key=None):
        """
        Args:
            platform_type (int): Type for the platform payload - can be None when using for default payload.
            priority (int): Priority for push notification - may be None.
            collapse_key (string): Collapse key for push notification - may be None.
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that our platform_type looks right
        if platform_type:
            validate_is_type(int, not_empty=False, platform_type=platform_type)
            PlatformPayload._validate_platform_type(platform_type)
        self.platform_type = platform_type

        # Check that our priority looks right
        if priority:
            validate_is_type(int, not_empty=False, priority=priority)
            if priority not in PlatformPayloadPriority.types:
                raise TypeError(
                    "Unsupported PlatformPayload priority: {}".format(
                        priority))
        self.priority = priority

        # Check that our collapse key looks right
        if collapse_key:
            validate_is_string(collapse_key=collapse_key)
        self.collapse_key = collapse_key
    def __init__(self, tokens, topic, action_type):
        """
        Args:
            tokens (list, string): A non-empty list of device registration tokens. Also accepts a single device token as a string.
                List may not have more than 1000 elements.
            topic (string): Name of the topic for the request. May contain the ``/topics/`` prefix.
            action (SubscriptionActionType, int): Action to execute via the request - should be a SubscriptionActionType const
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Ensure our tokens are right - non-empty strings, in a list
        if isinstance(tokens, basestring):
            tokens = [tokens]
        validate_is_type(list, tokens=tokens)
        invalid_str = [t for t in tokens if not isinstance(t, basestring) or not t]
        if invalid_str:
            raise ValueError('SubscriptionBatchRequest tokens must be non-empty strings.')
        if len(tokens) > 1000:
            raise ValueError('SubscriptionBatchRequest tokens must have no more than 1000 tokens.')

        # Ensure our topic is right - format like `/topics/something`
        validate_is_string(topic=topic)
        if not topic.startswith('/topics/'):
            topic = '/topics/{0}'.format(topic)

        if action_type not in SubscriptionActionType.batch_actions:
            raise ValueError('SubscriptionBatchRequest unsupported action {}.'.format(action_type))

        self.tokens = tokens
        self.topic = topic
        self._action_type = action_type
    def send(self, api_key):
        """ Attempt to send SubscriptionBatchRequest.

        Args:
            api_key (string): FCM API key to use for authorization.

        Returns:
            SubscriptionBatchResponse
        """
        from google.appengine.api import urlfetch
        from tbans.models.requests.subscriptions.subscription_batch_response import SubscriptionBatchResponse
        from tbans.utils.validation_utils import validate_is_string

        validate_is_string(api_key=api_key)

        headers = {
            'Authorization': 'key=' + api_key,
            'Content-Type': 'application/json'
        }
        try:
            response = urlfetch.fetch(
                url=self._batch_url,
                payload=self._json_string,
                method='POST',
                headers=headers
            )
            return SubscriptionBatchResponse(tokens=self.tokens, response=response)
        except Exception, e:
            # https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.api.urlfetch_errors
            return SubscriptionBatchResponse(tokens=self.tokens, error=str(e))
    def send(self, api_key):
        """ Attempt to send SubscriptionStatusRequest.

        Args:
            api_key (string): FCM API key to use for authorization.

        Return:
            SubscriptionStatusResponse
        """
        from google.appengine.api import urlfetch
        from tbans.models.requests.subscriptions.subscription_status_response import SubscriptionStatusResponse
        from tbans.utils.validation_utils import validate_is_string

        validate_is_string(api_key=api_key)

        headers = {
            'Authorization': 'key=' + api_key
        }
        try:
            response = urlfetch.fetch(
                url=self._iid_info_url,
                method='GET',
                headers=headers
            )
            return SubscriptionStatusResponse(response=response)
        except Exception, e:
            # https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.api.urlfetch_errors
            return SubscriptionStatusResponse(error=str(e))
    def __init__(self, response=None, error=None):
        """
        Args:
            response (object, content/status_code/headers): response from urlfetch Instance ID API call (https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.api.urlfetch)
            error (string): Directly pass an error instead of trying to parse the error from the JSON response.
        """
        if not response and not error:
            raise ValueError(
                'SubscriptionResponse must be initilized with either a response or an error'
            )

        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that response looks right
        if response:
            if response.content:
                validate_is_string(content=response.content)
            # Check that we have a status_code
            validate_is_type(int,
                             not_empty=False,
                             status_code=response.status_code)
        self._response = response

        # Check that error looks right
        if error:
            validate_is_string(error=error)
        self._error = error
    def __init__(self, token):
        """
        Args:
            token (string): The Instance ID token for the subscriber - this is the same token as FCM tokens.
        """
        from tbans.utils.validation_utils import validate_is_string

        validate_is_string(token=token)
        self.token = token
Beispiel #8
0
    def __init__(self, token):
        """
        Args:
            token (string): The Instance ID token for the subscriber - this is the same token as FCM tokens.
        """
        from tbans.utils.validation_utils import validate_is_string

        validate_is_string(token=token)
        self.token = token
Beispiel #9
0
    def __init__(self, title, body):
        """
        Args:
            title (string): Title for the notification.
            body (string): Body message for the notification.
        """
        from tbans.utils.validation_utils import validate_is_string

        # Check that we have a title
        validate_is_string(title=title)
        self.title = title

        # Check that we have a body
        validate_is_string(body=body)
        self.body = body
    def __init__(self, title, body):
        """
        Args:
            title (string): Title for the notification.
            body (string): Body message for the notification.
        """
        from tbans.utils.validation_utils import validate_is_string

        # Check that we have a title
        validate_is_string(title=title)
        self.title = title

        # Check that we have a body
        validate_is_string(body=body)
        self.body = body
    def __init__(self, token, result):
        """
        Args:
            token (string): Instance ID for the subscriber.
            result (dict): Dictionary result for the subscriber.
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that token looks right.
        validate_is_string(token=token)
        self.token = token

        # Check that result looks right.
        validate_is_type(dict, not_empty=False, result=result)
        self.error = result.get('error', None)
    def __init__(self, url, secret):
        """
        Args:
            url (string): The URL to send the notification payload to.
            secret (string): The secret to calculate the payload checksum with.
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type
        # Check url
        validate_is_string(url=url)

        # Check secret
        validate_is_type(basestring, not_empty=False, secret=secret)

        self.url = url
        self.secret = secret
        self._generate_key()
    def __init__(self, status_code, content=None):
        """
        Args:
            status_code (int): The stauts code from the HTTP response
            content (string): The content from the HTTP response - may be None
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that we have a status_code
        validate_is_type(int, not_empty=False, status_code=status_code)
        self.status_code = status_code

        # Check that content looks right
        if content:
            validate_is_string(content=content)
        self.content = content
Beispiel #14
0
    def __init__(self, url, secret):
        """
        Args:
            url (string): The URL to send the notification payload to.
            secret (string): The secret to calculate the payload checksum with.
        """
        from tbans.utils.validation_utils import validate_is_string
        # Check url, secret
        for (key, value) in [('url', url), ('secret', secret)]:
            # Make sure our value exists
            args = {key: value}
            validate_is_string(**args)

        self.url = url
        self.secret = secret
        self._generate_key()
Beispiel #15
0
    def __init__(self, url, secret):
        """
        Args:
            url (string): The URL to send the notification payload to.
            secret (string): The secret to calculate the payload checksum with.
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type
        # Check url
        validate_is_string(url=url)

        # Check secret
        validate_is_type(basestring, not_empty=False, secret=secret)

        self.url = url
        self.secret = secret
        self._generate_key()
    def __init__(self, url, secret):
        """
        Args:
            url (string): The URL to send the notification payload to.
            secret (string): The secret to calculate the payload checksum with.
        """
        from tbans.utils.validation_utils import validate_is_string
        # Check url, secret
        for (key, value) in [('url', url), ('secret', secret)]:
            # Make sure our value exists
            args = {key: value}
            validate_is_string(**args)

        self.url = url
        self.secret = secret
        self._generate_key()
    def __init__(self, notification, url, secret):
        """
        Args:
            notification (Notification): The Notification to send.
            url (string): The URL to send the notification payload to.
            secret (string): The secret to calculate the payload checksum with.
        """
        super(WebhookRequest, self).__init__(notification)

        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that we have a url
        validate_is_string(url=url)
        self.url = url

        # Check that we have a secret
        validate_is_type(basestring, not_empty=False, secret=secret)
        self.secret = secret
    def __init__(self, collapse_key=None, priority=None):
        """
        Args:
            collapse_key (string): Collapse key for push notification - may be None.
            priority (int): Priority for push notification - may be None.
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that our collapse key looks right
        if collapse_key:
            validate_is_string(collapse_key=collapse_key)
        self.collapse_key = collapse_key

        # Check that our priority looks right
        if priority:
            validate_is_type(int, not_empty=False, priority=priority)
            PlatformPriority.validate(priority)
        self.priority = priority
Beispiel #19
0
    def __init__(self, notification, url, secret):
        """
        Args:
            notification (Notification): The Notification to send.
            url (string): The URL to send the notification payload to.
            secret (string): The secret to calculate the payload checksum with.
        """
        super(WebhookRequest, self).__init__(notification)

        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that we have a url
        validate_is_string(url=url)
        self.url = url

        # Check that we have a secret
        validate_is_type(basestring, not_empty=False, secret=secret)
        self.secret = secret
    def __init__(self, type_name, user_id, sending_device_key):
        """
        Args:
            type_name (string): Human readable string that represents the type for this notification (ex: 'favorite')
            user_id (string): User/account ID that we're sending notifications for - used for collapse key.
            sending_device_key (string): The FCM token for the device that triggered the notification.
        """
        from tbans.utils.validation_utils import validate_is_string
        # Check type_name, user_id, and sending_device_key
        for (value, name) in [(type_name, 'type_name'), (user_id, 'user_id'),
                              (sending_device_key, 'sending_device_key')]:
            # Make sure our value exists
            args = {name: value}
            validate_is_string(**args)

        self.type_name = type_name
        self.user_id = user_id
        self.sending_device_key = sending_device_key
    def __init__(self, collapse_key=None, priority=None):
        """
        Args:
            collapse_key (string): Collapse key for push notification - may be None.
            priority (int): Priority for push notification - may be None.
        """
        from tbans.utils.validation_utils import validate_is_string, validate_is_type

        # Check that our collapse key looks right
        if collapse_key:
            validate_is_string(collapse_key=collapse_key)
        self.collapse_key = collapse_key

        # Check that our priority looks right
        if priority:
            validate_is_type(int, not_empty=False, priority=priority)
            PlatformPriority.validate(priority)
        self.priority = priority
Beispiel #22
0
    def send(self, api_key):
        """ Attempt to send SubscriptionStatusRequest.

        Args:
            api_key (string): FCM API key to use for authorization.

        Return:
            SubscriptionStatusResponse
        """
        from google.appengine.api import urlfetch
        from tbans.models.requests.subscriptions.subscription_status_response import SubscriptionStatusResponse
        from tbans.utils.validation_utils import validate_is_string

        validate_is_string(api_key=api_key)

        headers = {'Authorization': 'key=' + api_key}
        try:
            response = urlfetch.fetch(url=self._iid_info_url,
                                      method='GET',
                                      headers=headers)
            return SubscriptionStatusResponse(response=response)
        except Exception, e:
            # https://cloud.google.com/appengine/docs/standard/python/refdocs/google.appengine.api.urlfetch_errors
            return SubscriptionStatusResponse(error=str(e))