Last active
April 11, 2018 15:16
-
-
Save emiel/99e5c103dfffaf05629ca305ff546c18 to your computer and use it in GitHub Desktop.
Decode SendGrid Event ID in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decode_eid(eid): | |
""" | |
Decodes a SendGrid event id (sg_event_id) and returns as uuid | |
""" | |
eid_len = len(eid) | |
if eid_len == 22: | |
res = uuid.UUID(bytes=base64.urlsafe_b64decode(eid + "==")) | |
elif eid_len == 48: | |
res = uuid.UUID(base64.urlsafe_b64decode(eid).decode("ascii")) | |
else: | |
raise Exception("Unsupported sendgrid event id: {}".format(eid)) | |
assert res.variant == uuid.RFC_4122 and res.version == 4 | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment