コード例 #1
0
 def _setup_topic(self):
     """Creates a topic if it does not exist."""
     topic_name = pubsub_utils.get_full_topic_name()
     try:
         topic = self.client.topics().get(topic=topic_name).execute()
     except errors.HttpError as e:
         if e.resp.status == 404:
             body = {'name': topic_name}
             self.client.topics().create(body=body).execute()
         else:
             logging.exception(e)
             raise
コード例 #2
0
 def _setup_topic(self):
     """Creates a topic if it does not exist."""
     topic_name = pubsub_utils.get_full_topic_name()
     try:
         self.client.projects().topics().get(topic=topic_name).execute()
     except errors.HttpError as e:
         if e.resp.status == 404:
             self.client.projects().topics().create(name=topic_name,
                                                    body={}).execute()
         else:
             logging.exception(e)
             raise
コード例 #3
0
    def post(self):
        message = self.request.body
        client = pubsub_utils.get_client()

        topic_name = pubsub_utils.get_full_topic_name()
        body = {
            'messages': [{
                'data': base64.b64encode(message.encode('utf-8'))
            }]
        }
        client.projects().topics().publish(
                topic=topic_name, body=body).execute()
        self.response.write(body)
コード例 #4
0
 def post(self):
     """Publishes the message via the Pub/Sub API."""
     client = pubsub_utils.get_client()
     message = self.request.get('message')
     if message:
         topic_name = pubsub_utils.get_full_topic_name()
         body = {
             'messages': [{
                 'data': base64.b64encode(message.encode('utf-8'))
             }]
         }
         client.projects().topics().publish(topic=topic_name,
                                            body=body).execute()
     self.response.status = 204
コード例 #5
0
 def post(self):
     """Publishes the message via the Pub/Sub API."""
     client = pubsub_utils.get_client()
     message = self.request.get('message')
     if message:
         topic_name = pubsub_utils.get_full_topic_name()
         body = {
             'topic': topic_name,
             'messages': [{
                 'data': base64.b64encode(message.encode('utf-8'))
             }]
         }
         client.topics().publishBatch(body=body).execute()
     self.response.status = 204
コード例 #6
0
 def _setup_topic(self):
     """Creates a topic if it does not exist."""
     self.response.headers['Access-Control-Allow-Origin'] = '*'
     topic_name = pubsub_utils.get_full_topic_name()
     try:
         self.client.projects().topics().get(
             topic=topic_name).execute()
     except errors.HttpError as e:
         if e.resp.status == 404:
             self.client.projects().topics().create(
                 name=topic_name, body={}).execute()
         else:
             logging.exception(e)
             raise
コード例 #7
0
 def _setup_subscription(self):
     """Creates a subscription if it does not exist."""
     subscription_name = pubsub_utils.get_full_subscription_name()
     try:
         self.client.projects().subscriptions().get(
             subscription=subscription_name).execute()
     except errors.HttpError as e:
         if e.resp.status == 404:
             body = {
                 'topic': pubsub_utils.get_full_topic_name(),
                 'pushConfig': {
                     'pushEndpoint': pubsub_utils.get_app_endpoint_url()
                 }
             }
             self.client.projects().subscriptions().create(
                 name=subscription_name, body=body).execute()
         else:
             logging.exception(e)
             raise
コード例 #8
0
 def _setup_subscription(self):
     """Creates a subscription if it does not exist."""
     subscription_name = pubsub_utils.get_full_subscription_name()
     try:
         subscription = self.client.subscriptions().get(
             subscription=subscription_name).execute()
     except errors.HttpError as e:
         if e.resp.status == 404:
             body = {
                 'name': subscription_name,
                 'topic': pubsub_utils.get_full_topic_name(),
                 'pushConfig': {
                     'pushEndpoint': pubsub_utils.get_app_endpoint_url()
                 }
             }
             self.client.subscriptions().create(body=body).execute()
         else:
             logging.exception(e)
             raise
コード例 #9
0
 def _setup_subscription(self):
     """Creates a subscription if it does not exist."""
     self.response.headers['Access-Control-Allow-Origin'] = '*'
     subscription_name = pubsub_utils.get_full_subscription_name()
     try:
         self.client.projects().subscriptions().get(
             subscription=subscription_name).execute()
     except errors.HttpError as e:
         if e.resp.status == 404:
             body = {
                 'topic': pubsub_utils.get_full_topic_name(),
                 'pushConfig': {
                     'pushEndpoint': pubsub_utils.get_app_endpoint_url()
                 }
             }
             self.client.projects().subscriptions().create(
                 name=subscription_name, body=body).execute()
         else:
             logging.exception(e)
             raise
コード例 #10
0
    def post(self):
        """Publishes the message via the Pub/Sub API."""
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        client = pubsub_utils.get_client()
        message = self.request.get('message')
        full_message = {
            'message_data': message,
            'created': int(time.time())
        }
        user_id = self.request.get('user_id')
        if user_id and UserHandler.users.get(user_id) is not None:
            full_message['user'] = UserHandler.users.get(user_id).__dict__

        if full_message:
            topic_name = pubsub_utils.get_full_topic_name()
            body = {
                'messages': [{
                    'data': base64.b64encode(json.dumps(full_message).encode('utf-8'))
                }]
            }
            client.projects().topics().publish(
                topic=topic_name, body=body).execute()
        self.response.status = 204