-
-
Save SIFAR786/f2b134e279881b515756a39ed9c2c6ad to your computer and use it in GitHub Desktop.
Decode SendGrid Event ID in Python
This file contains hidden or 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