Skip to content

Instantly share code, notes, and snippets.

@SIFAR786
Forked from emiel/sendgrid_eid.py
Created April 11, 2018 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SIFAR786/f2b134e279881b515756a39ed9c2c6ad to your computer and use it in GitHub Desktop.
Save SIFAR786/f2b134e279881b515756a39ed9c2c6ad to your computer and use it in GitHub Desktop.
Decode SendGrid Event ID in Python
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