Skip to content

docs: describe event and context in Pub/Sub example#5862

Merged
anguillanneuf merged 3 commits into
masterfrom
pubsub
May 21, 2021
Merged

docs: describe event and context in Pub/Sub example#5862
anguillanneuf merged 3 commits into
masterfrom
pubsub

Conversation

@anguillanneuf
Copy link
Copy Markdown
Member

@anguillanneuf anguillanneuf commented May 20, 2021

Improve the Pub/Sub Cloud Functions example by describing what event and context are.

Show a way to extract topic name from context in the sample as well.

Tested that it indeed works in a demo I published in my personal repo: https://github.com/anguillanneuf/buganizer/tree/main/177311703

@anguillanneuf anguillanneuf requested review from a team, ace-n and grant as code owners May 20, 2021 23:46
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented May 20, 2021

No region tags are edited in this PR.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added the samples Issues that are directly related to samples. label May 20, 2021
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label May 20, 2021
Copy link
Copy Markdown

@ace-n ace-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if context.resource["name"] works on GCF itself.

@anguillanneuf
Copy link
Copy Markdown
Member Author

@ace-n Yep, I tested and it works! See the logs output with

    message_id = context.event_id
    incoming_topic_id = context.resource["name"]
    publish_timestamp = context.timestamp

    message_data = "an empty message"
    if "data" in event:
        message_data = base64.b64decode(event["data"]).decode("utf-8")

    print(
        """Function triggered by {} of messageId {} published to {} at {}
    """.format(
            message_data, message_id, incoming_topic_id, publish_timestamp
        )
    )

Screen Shot 2021-05-21 at 9 25 26 AM

@anguillanneuf anguillanneuf merged commit 5de120f into master May 21, 2021
@anguillanneuf anguillanneuf deleted the pubsub branch May 21, 2021 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants