Skip to content

Commit

Permalink
V2 webhook updates (#14)
Browse files Browse the repository at this point in the history
- Bumped benchling-sdk to v1.13.0 for updated webhook models
- Updated handler.py to use V2 webhooks ahead of V0 deprecation (docs.benchling.com/changelog/explicit-webhook-subscriptions-and-webhook-version-update)
  • Loading branch information
keithmacaulay authored Jul 18, 2024
1 parent 4ba13f9 commit 2a46c32
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
**/.client_secret
**/.pytest_cache
.mypy_cache
.ruff_cache
.ruff_cache
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from benchling_sdk.apps.framework import App
from benchling_sdk.apps.status.errors import AppUserFacingError
from benchling_sdk.models import AppCanvasUpdate, Molecule
from benchling_sdk.models.webhooks.v0 import CanvasInteractionWebhookV0
from benchling_sdk.models.webhooks.v0 import CanvasInteractionWebhookV2

from local_app.benchling_app.molecules import create_molecule
from local_app.benchling_app.views.canvas_initialize import input_blocks
Expand All @@ -29,7 +29,7 @@ class UnsupportedButtonError(Exception):
pass


def route_interaction_webhook(app: App, canvas_interaction: CanvasInteractionWebhookV0) -> None:
def route_interaction_webhook(app: App, canvas_interaction: CanvasInteractionWebhookV2) -> None:
canvas_id = canvas_interaction.canvas_id
if canvas_interaction.button_id == SEARCH_BUTTON_ID:
with app.create_session_context("Search Chemicals", timeout_seconds=20) as session:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from benchling_sdk.apps.status.errors import AppUserFacingError
from benchling_sdk.models.webhooks.v0 import (
CanvasInitializeWebhookV0,
CanvasInteractionWebhookV0,
CanvasInitializeWebhookV2,
CanvasInteractionWebhookV2,
WebhookEnvelopeV0,
)

Expand All @@ -27,9 +27,9 @@ def handle_webhook(webhook_dict: dict[str, Any]) -> None:
# Note: if your manifest specifies more than one item in `features`,
# then `webhook.message.feature_id` may also need to be part of your routing logic
try:
if isinstance(webhook.message, CanvasInitializeWebhookV0):
if isinstance(webhook.message, CanvasInitializeWebhookV2):
render_search_canvas(app, webhook.message)
elif isinstance(webhook.message, CanvasInteractionWebhookV0):
elif isinstance(webhook.message, CanvasInteractionWebhookV2):
route_interaction_webhook(app, webhook.message)
else:
# Should only happen if the app's manifest requests webhooks that aren't handled in its code paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
TextInputUiBlock,
TextInputUiBlockType,
)
from benchling_sdk.models.webhooks.v0 import CanvasInitializeWebhookV0
from benchling_sdk.models.webhooks.v0 import CanvasInitializeWebhookV2

from local_app.benchling_app.views.constants import SEARCH_BUTTON_ID, SEARCH_TEXT_ID


def render_search_canvas(app: App, canvas_initialized: CanvasInitializeWebhookV0) -> None:
def render_search_canvas(app: App, canvas_initialized: CanvasInitializeWebhookV2) -> None:
with app.create_session_context("Show Sync Search", timeout_seconds=20):
canvas_builder = CanvasBuilder(
app_id=app.id,
Expand Down
6 changes: 5 additions & 1 deletion examples/chem-sync-local-flask/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ features:
- name: Sync Step
id: sync_step
type: ASSAY_RUN

subscriptions:
deliveryMethod: WEBHOOK
messages:
- type: v2.canvas.initialized
- type: v2.canvas.userInteracted
configuration:
- name: Sync Folder
type: folder
Expand Down
2 changes: 1 addition & 1 deletion examples/chem-sync-local-flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flask~=3.0.2
# Cryptography extra needed for webhook verification
benchling-sdk[cryptography]==1.12.0
benchling-sdk[cryptography]==1.13.0
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"message": {
"featureId": "feat_1234",
"resourceId": "assayrun_1234",
"type": "v0.canvas.initialized",
"type": "v2.canvas.initialized",
"deprecated": false,
"excludedProperties": []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"channel": "app_signals",
"message": {
"type": "v0.canvas.userInteracted",
"type": "v2.canvas.userInteracted",
"buttonId": "button_1",
"canvasId": "cnvs_1234",
"deprecated": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from benchling_sdk.apps.status.framework import SessionContextManager
from benchling_sdk.apps.types import JsonType
from benchling_sdk.models import AppCanvas, AppCanvasUpdate, Molecule, TextInputUiBlock
from benchling_sdk.models.webhooks.v0 import CanvasInteractionWebhookV0
from benchling_sdk.models.webhooks.v0 import CanvasInteractionWebhookV2

from local_app.benchling_app.canvas_interaction import UnsupportedButtonError, route_interaction_webhook
from local_app.benchling_app.views.canvas_initialize import input_blocks
Expand Down Expand Up @@ -191,8 +191,8 @@ def _mock_canvas(blocks: list[UiBlock] | None = None, data: JsonType | None = No
return mock_canvas


def _mock_interaction_webhook(canvas_id: str, button_id: str) -> CanvasInteractionWebhookV0:
interaction_webhook = MagicMock(CanvasInteractionWebhookV0)
def _mock_interaction_webhook(canvas_id: str, button_id: str) -> CanvasInteractionWebhookV2:
interaction_webhook = MagicMock(CanvasInteractionWebhookV2)
interaction_webhook.button_id = button_id
interaction_webhook.canvas_id = canvas_id
return interaction_webhook

0 comments on commit 2a46c32

Please sign in to comment.