nylas/nylas-python

Webhook support

yi-fan-song opened this issue · 2 comments

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

I can't use the python sdk to register my webhooks, and the authentication scheme wasn't obvious, it's Basic <client_secret>: where "<client_secret>:" is b64 encoded). I tried it without the ":" at first, then I figured out I needed to b64 a ":" character with my secret as well.

Describe the solution you'd like
A clear and concise description of what you want to happen.

One method to register the webhook and either 1. constants of the webhook types or 2. an enum with the webhook types.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Currently making the request myself

    headers = {
        "Accept": "application/json",
        "Authorization": f"Basic {get_encoded_nylas_secret()}",
        "Content-Type": "application/json",
    }

    data = {
        "callback_url": url,
        "triggers": ["message.opened", "message.link_clicked", "thread.replied"],
        "state": "active",
    }

    return requests.post(
        f"https://api.nylas.com/a/{settings.NYLAS_CLIENT_ID}/webhooks",
        data=json.dumps(data),
        headers=headers,
    )

Additional context
Add any other context or screenshots about the feature request here.

A rough eta of webhook support would be nice, my goal is just to reduce the number of raw calls to nylas in my code, and try to use the sdk wherever possible.

@yi-fan-song Thanks for opening this issue! You'll be delighted to know that supporting webhooks for the Python SDK is on our roadmap and is going to kick off soon! I'll keep you updated on the progress of this feature and when it will get released.

@yi-fan-song Thank you for your patience, webhook support has been completed and will be included in the next Python SDK release!

Below is the usage of the webhook feature:
To create a new webhook:

webhook = nylas.webhooks.create()
webhook.callback_url = "https://your-server.com/webhook"
webhook.triggers = ["message.created"]
webhook.state = "active"
webhook.save()

To get all webhooks:

webhooks = nylas.webhooks.all()

To get a specific webhook:

webhook = nylas.webhooks.get("{WEBHOOK_ID}")

To update a component:

webhook = nylas.webhooks.get("{WEBHOOK_ID}")
webhook.state = "inactive"
webhook.save()

To delete a component:

nylas.webhooks.delete("{WEBHOOK_ID}")

There are also two helper enums provided for Webhooks, Trigger and State:

# The available Webhook triggers:
Webhook.Trigger.ACCOUNT_CONNECTED = "account.connected"
Webhook.Trigger.ACCOUNT_RUNNING = "account.running"
Webhook.Trigger.ACCOUNT_STOPPED = "account.stopped"
Webhook.Trigger.ACCOUNT_INVALID = "account.invalid"
Webhook.Trigger.ACCOUNT_SYNC_ERROR = "account.sync_error"
Webhook.Trigger.MESSAGE_CREATED = "message.created"
Webhook.Trigger.MESSAGE_OPENED = "message.opened"
Webhook.Trigger.MESSAGE_UPDATED = "message.updated"
Webhook.Trigger.MESSAGE_LINK_CLICKED = "message.link_clicked"
Webhook.Trigger.THREAD_REPLIED = "thread.replied"
Webhook.Trigger.CONTACT_CREATED = "contact.created"
Webhook.Trigger.CONTACT_UPDATED = "contact.updated"
Webhook.Trigger.CONTACT_DELETED = "contact.deleted"
Webhook.Trigger.CALENDAR_CREATED = "calendar.created"
Webhook.Trigger.CALENDAR_UPDATED = "calendar.updated"
Webhook.Trigger.CALENDAR_DELETED = "calendar.deleted"
Webhook.Trigger.EVENT_CREATED = "event.created"
Webhook.Trigger.EVENT_UPDATED = "event.updated"
Webhook.Trigger.EVENT_DELETED = "event.deleted"
Webhook.Trigger.JOB_SUCCESSFUL = "job.successful"
Webhook.Trigger.JOB_FAILED = "job.failed"

# The available Webhook states:
Webhook.State.ACTIVE = "active"
Webhook.State.INACTIVE = "inactive"