You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
627 B
Python

import json
import os
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic = publisher.topic_path(os.environ["GCP_PROJECT"], os.environ["GCP_TOPIC"])
def push_to_pubsub(request_params):
publisher.publish(topic, json.dumps(request_params).encode("utf-8")).result()
def main(request):
request_json = request.get_json(silent=True)
request_args = request.args
if request_json:
push_to_pubsub(request_json)
elif request_args:
push_to_pubsub(request_args)
else:
return ("No data provided.", 400)
return {"status": "ok", "source": "cf_to_pubsub"}