We recently added native CloudEvent support to our Event Grid libraries. I created an app for my Intro to the new Azure SDK Python video and discovered that converting a message from a Service Bus Message to CloudEvent wasn’t super intuitive. It looked like this:
event = CloudEvent.from_dict( json.loads(str(msg), object_hook=datetime_parser) )
I had to first convert the
msg to a string, then call
json.loads and pass that an
object_hook to parse the dates. It took me a pretty long time and a few discussions with the team to figure that out.
I suggested to the Azure SDK Python team that we have a
from_json method takes care of all of that for all the various message types, like Service Bus, Event Hub, Storage Queues.
They ran with my suggestion and just shipped it in
You can find the packages here: https://pypi.org/project/azure-core, https://pypi.org/project/azure-eventgrid/ and all the other Azure SDK packages here: https://azure.com/sdk. Here’s the commit if you are interested in seeing all the changes to support this: https://github.com/Azure/azure-sdk-for-python/commit/acf226436e5cd6add2a96d099ee9862b8bab07ce#diff-650118ee0761cbfcc2d3c1d31965d3707aedfb8a4b3cb27edefc69cb17eb8c41
Here’s my new code with the new
event = CloudEvent.from_json(msg)
This could be considered a small win, but the way I think about it is that the rest of the Azure development community now doesn’t have to go through the struggle I did when trying to go from one message type to another. And my job is about experiencing pain and doing something about it so the rest of the Azure development community doesn’t have to. So, it’s a win for me and win for you.
Here’s the before and after - love it!