def publish()

in streaming-data-to-analytics/code/main.py [0:0]


def publish():
    try:
        # Request validation
        args = request.args
        entity = args.get("entity")
        if not entity:
            entity = "unknown"

        # Get the request data
        data = request.get_data()
        
        # TO-DO - If you need to validate the request, add your code here

        # Pub/sub publisher
        publisher = pubsub_v1.PublisherClient()
        topic_path = publisher.topic_path(PROJECT_ID, TOPIC_ID)
        
        # Publish the message to Pub/sub
        publisher.publish(topic_path, data, entity=entity)
    except Exception as ex:
        logging.error(ex)
        return 'error:{}'.format(ex), http.HTTPStatus.INTERNAL_SERVER_ERROR

    return 'success'