Skip to content

Commit

Permalink
GCF: Add Pub/Sub publisher sample (#3312)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri authored Apr 8, 2020
1 parent 35ded53 commit 8f49b4e
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
67 changes: 67 additions & 0 deletions functions/pubsub/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START functions_pubsub_setup]
import base64
from google.cloud import pubsub_v1
import json
import os


# Instantiates a Pub/Sub client
publisher = pubsub_v1.PublisherClient()
PROJECT_ID = os.getenv('GCP_PROJECT')
# [END functions_pubsub_setup]


# [START functions_pubsub_publish]
# Publishes a message to a Cloud Pub/Sub topic.
def publish(request):
request_json = request.get_json(silent=True)

topic_name = request_json.get("topic")
message = request_json.get("message")

if not topic_name or not message:
return ('Missing "topic" and/or "subscription" parameter.', 500)

print(f'Publishing message to topic {topic_name}')

# References an existing topic
topic_path = publisher.topic_path(PROJECT_ID, topic_name)

message_json = json.dumps({
'data': {'message': message},
})
message_bytes = message_json.encode('utf-8')
print(message_bytes)

# Publishes a message
try:
publish_future = publisher.publish(topic_path, data=message_bytes)
publish_future.result() # Verify the publish succeeded
return 'Message published.'
except Exception as e:
print(e)
return (e, 500)

# [END functions_pubsub_publish]


# [START functions_pubsub_subscribe]
# Triggered from a message on a Cloud Pub/Sub topic.
def subscribe(event, context):
# Print out the data from Pub/Sub, to prove that it worked
print(base64.b64decode(event['data']))
# [END functions_pubsub_subscribe]
51 changes: 51 additions & 0 deletions functions/pubsub/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import base64
import flask
from mock import MagicMock
import os

import main


FUNCTIONS_TOPIC = os.getenv("FUNCTIONS_TOPIC")


def test_functions_pubsub_publish_should_fail_without_params():
request = MagicMock()
request.body.topic = None
response = main.publish(request)

assert 'Missing "topic" and/or "subscription" parameter.' in response


def test_functions_pubsub_publish_should_publish_message():
request = MagicMock()
request.body.topic = FUNCTIONS_TOPIC
request.body.message = "my_message"

response = main.publish(request)

assert response == "Message published."


def test_functions_pubsub_subscribe_should_print_message(capsys):
pubsub_message = MagicMock()
pubsub_message.data = base64.b64encode(b"Hello, world!")

main.subscribe(pubsub_message)

out, _ = capsys.readouterr()
assert "Hello, world!" in out
1 change: 1 addition & 0 deletions functions/pubsub/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-pubsub==1.3.0

0 comments on commit 8f49b4e

Please sign in to comment.