March Developer Challenge – CloudEvents: Week 2

As we learned last week, CloudEvents is a specification for describing event data in common formats and the goal is to provide interoperability across services, platforms and systems. This week we will expand on what we learnt last week and we will create our first CloudEvent programmatically.

Links to March’s developer challenge:

CloudEvent format

Event Formats specify how to serialize a CloudEvent with certain encoding formats. Depending on our requirements it is possible that we might need to use a specific format to encode our CloudEvent message. Maybe we have a requirement where we need to send the data in something different than the JSON format, e.g. AVRO, Protobuf. Check out the specification document if you want to learn more about these different formats.

For simplicity purposes we will stick to the JSON Event format as it is the most common and easiest to interact with.

Now that we are familiar with what a CloudEvent is, what a CloudEvent message looks like, and the different Event Formats available, let’s see how we can create one programmatically.

CloudEvents SDK

There are language-specific SDKs that can be used to create a message that complies with the CloudEvents standard. Below is a list of the different languages that there’s an SDK for:

You might be thinking, why would I need an SDK when I can create the message “manually” as well? Using an SDK allows us to easily create a CloudEvent message, ensure that it follows the guidelines defined in the standard, and simplify generating the output format. Not only that but it can also be used to parse a CloudEvent message that our application consumes. As an example, I will use the Python SDK to create the CloudEvent message for the Ticket Management System that I used as an example previously.

 

 

from cloudevents.http import CloudEvent
from cloudevents.conversion import to_binary
import requests

ticket_id = “IT00010232”

# Create a CloudEvent
attributes = {
“type”: “com.ajmaradiaga.tms.Ticket.Created.v1”,
“source”: “https://tms-prod.ajmaradiaga.com/tickets”,
“subject”: ticket_id,
}
data = {
“ID”: ticket_id,
“Description”: “Install ColdTurkey to block distracting websites.”,
“Urgency”: {
“ID”: 1,
“Description”: “High”
}
}
event = CloudEvent(attributes, data)

# Creates the HTTP request representation of the CloudEvent in binary content mode
headers, body = to_binary(event)

print(body)

 

 

Week 2 challenge – Create a CloudEvent using an SDK

👉 Your task for this week is: Pick your favourite language from the list above and create the message that you shared as part of week’s 1 challenge but this time instead of doing it manually, you will do it using an SDK. To complete this week’s challenge, share the code you used to create the CloudEvent message and a screenshot of your program’s output.

What if there is no SDK available for your favourite language? Not a problem, you can create the message structure “manually”. Just share the code used to generate a message in your programming language and a screenshot of the program’s output.

Scroll to Top