def create(message, app_id): try: pm = PushManager.query.filter_by(uuid=app_id).one_or_none() if pm is None: raise GatlinException("This app not exist", 404) push = Push(message, pm.id) return push.save() except Exception as e: raise GatlinException(e, 400)
def get_message(message_id, app_id): try: push = PushManager.query.join(Push).filter( PushManager.uuid == app_id, Push.sent == False, Push.uuid == message_id).one_or_none() if push is None: raise GatlinException("Message not exists", 404) if len(push.installation) == 0: raise GatlinException("No devices with this app", 200) return push.json except Exception as e: raise GatlinException(e, 400)
def delete_platform(self, platform_id): try: sns_client.delete_platform_application( PlatformApplicationArn=platform_id) except ClientError as e: raise GatlinException(e.response['Error']['Message'], 400)
def get(uuid): """ Get a push manager """ pmanager = PushManager.query.filter_by(uuid=uuid).one_or_none() if pmanager is None: raise GatlinException("App not exist", 404) return pmanager
def __init__(self, provider): """ Instantiate the provider """ if isinstance(provider, Provider): self._provider = provider else: raise GatlinException("Invalid Provider", 400)
def create_endpoint(self, device_id, platform_id): try: response = sns_client.create_platform_endpoint( PlatformApplicationArn=platform_id, Token=device_id) return response['EndpointArn'] except ClientError as err: raise GatlinException(err.response['Error']['Message'], 400)
def create(self, device_id, app_id): """ app_id is the uuid device_id id generated by the android/ios device """ pm = PushManager.query.filter_by( uuid=app_id ).one_or_none() if pm is None: raise GatlinException("App doesn't exist", 400) try: endpoint_arn = self._provider.create_endpoint(device_id, pm.sns_arn) installation = Installation(pm.id, device_id, endpoint_arn) return installation.save() except GatlinException as iexc: raise iexc except IntegrityError: raise GatlinException("This device is already registered with this app", 400)
def set_android_platform(self, android_key): try: response = sns_client.create_platform_application( Name=self._app_name, Platform="GCM", Attributes={"PlatformCredential": android_key}) return response['PlatformApplicationArn'] except ClientError as e: raise GatlinException(e.response['Error']['Message'], 400)
def delete(self, uuid): """ Remove a push manager """ try: pmanager = PushManager.query.filter_by(uuid=uuid).one_or_none() if pmanager is None: raise GatlinException("App not exist", 404) self._provider.delete_platform(pmanager.sns_arn) pmanager.delete() except GatlinException as exception: raise exception
def get(uuid): """ Return one installation for a given uuid """ installation = Installation.query.filter_by( uuid=uuid ).one_or_none() if installation is None: raise GatlinException("installation not exists", 404) return installation
def create(self, android_key): """ Create a new push manager """ try: sns_arn = self._provider.set_android_platform(android_key) push_manager = PushManager(android_key, self._provider.app_name, sns_arn) return push_manager.save() except GatlinException as exception: raise exception except IntegrityError: raise GatlinException("Error while saving the app", 400)
def delete(self, uuid): """ Delete one particular installation """ installation = Installation.query.filter_by( uuid=uuid ).one_or_none() if installation is None: raise GatlinException("Installation not exists", 404) try: self._provider.delete_endpoint(installation.endpoint_arn) installation.delete() except GatlinException as iexc: return iexc
def update(self, uuid, android_key): """ Update the android key of the given push manager """ try: pmanager = PushManager.query.filter_by(uuid=uuid).one_or_none() if pmanager is None: raise GatlinException("App not exist", 404) self._provider.app_name = pmanager.app_name _ = self._provider.set_android_platform(android_key) pmanager.android_key = android_key return pmanager.save() except GatlinException as exception: raise exception
def get(uuid): push = Push.query.filter_by(uuid=uuid).one_or_none() if push is None: raise GatlinException("Push not exist", 404) return push
def delete_endpoint(self, platform_id): try: response = sns_client.delete_endpoint(EndpointArn=platform_id) except ClientError as e: raise GatlinException(e.response['Error']['Message'], 400)