Skip to content

Commit

Permalink
Merge pull request #1214 from nyaruka/webhooks_v2
Browse files Browse the repository at this point in the history
Make `@webhook` an object with `headers` and `json` properties
  • Loading branch information
rowanseymour authored Mar 4, 2024
2 parents 982cfbc + 230d9d2 commit 6754507
Show file tree
Hide file tree
Showing 31 changed files with 771 additions and 128 deletions.
2 changes: 1 addition & 1 deletion cmd/docgen/docs/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestGenerateDocs(t *testing.T) {
assert.Equal(t, 88, len(functions))

types := context["types"].([]any)
assert.Equal(t, 19, len(types))
assert.Equal(t, 20, len(types))

root := context["root"].([]any)
assert.Equal(t, 14, len(root))
Expand Down
12 changes: 0 additions & 12 deletions flows/actions/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/flows/modifiers"
Expand Down Expand Up @@ -156,17 +155,6 @@ func (a *baseAction) saveWebhookResult(run flows.Run, step flows.Step, name stri
a.saveResult(run, step, name, value, category, "", input, extra, logEvent)
}

func (a *baseAction) updateWebhook(run flows.Run, call *flows.WebhookCall) {
parsed := types.JSONToXValue(call.ResponseJSON)

switch typed := parsed.(type) {
case nil, *types.XError:
run.SetWebhook(types.XObjectEmpty)
default:
run.SetWebhook(typed)
}
}

// helper to apply a contact modifier
func (a *baseAction) applyModifier(run flows.Run, mod flows.Modifier, logModifier flows.ModifierCallback, logEvent flows.EventCallback) bool {
logModifier(mod)
Expand Down
2 changes: 1 addition & 1 deletion flows/actions/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func testActionType(t *testing.T, assetsJSON json.RawMessage, typeName string) {
actual.Events, _ = jsonx.Marshal(runEvents[ignoreEventCount:])

if tc.Webhook != nil {
actual.Webhook, _ = jsonx.Marshal(run.Webhook())
actual.Webhook = jsonx.MustMarshal(run.Webhook())
}
if tc.ContactAfter != nil {
actual.ContactAfter, _ = jsonx.Marshal(session.Contact())
Expand Down
2 changes: 1 addition & 1 deletion flows/actions/call_resthook.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (a *CallResthookAction) Execute(run flows.Run, step flows.Step, logModifier

asResult := a.pickResultCall(calls)
if asResult != nil {
a.updateWebhook(run, asResult)
run.SetWebhook(asResult)
}

if a.ResultName != "" {
Expand Down
2 changes: 1 addition & 1 deletion flows/actions/call_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (a *CallWebhookAction) call(run flows.Run, step flows.Step, url, method, bo
logEvent(events.NewError(err))
}
if call != nil {
a.updateWebhook(run, call)
run.SetWebhook(call)

status := callStatus(call, err, false)

Expand Down
2 changes: 1 addition & 1 deletion flows/actions/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"uuid": "bead76f5-dac4-4c9d-996c-c62b326e8c0a",
"name": "Action Tester",
"spec_version": "13.2",
"spec_version": "13.3.0",
"language": "eng",
"type": "messaging",
"revision": 123,
Expand Down
69 changes: 47 additions & 22 deletions flows/actions/testdata/call_resthook.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@
"http://temba.io/": [
{
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{ \"ok\": \"true\" }"
}
],
"http://unavailable.com/": [
{
"status": 503,
"body": "{ \"errors\": [\"service unavailable\"] }"
"headers": {
"Content-Type": "text/plain"
},
"body": "service unavailable"
}
]
},
Expand Down Expand Up @@ -99,7 +105,7 @@
"url": "http://temba.io/",
"status_code": 200,
"request": "POST / HTTP/1.1\r\nHost: temba.io\r\nUser-Agent: goflow-testing\r\nContent-Length: 898\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{\"channel\":null,\"contact\":{\"language\":\"eng\",\"name\":\"Ryan Lewis\",\"urn\":\"tel:+12065551212\",\"uuid\":\"5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f\"},\"flow\":{\"name\":\"Action Tester\",\"revision\":123,\"uuid\":\"bead76f5-dac4-4c9d-996c-c62b326e8c0a\"},\"input\":{\"attachments\":[{\"content_type\":\"image/jpeg\",\"url\":\"http://http://s3.amazon.com/bucket/test.jpg\"},{\"content_type\":\"audio/mp3\",\"url\":\"http://s3.amazon.com/bucket/test.mp3\"}],\"channel\":null,\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"text\":\"Hi everybody\",\"type\":\"msg\",\"urn\":{\"display\":\"(206) 555-1212\",\"path\":\"+12065551212\",\"scheme\":\"tel\"},\"uuid\":\"aa90ce99-3b4d-44ba-b0ca-79e63d9ed842\"},\"path\":[{\"arrived_on\":\"2018-10-18T14:20:30.000123Z\",\"exit_uuid\":\"\",\"node_uuid\":\"72a1f5df-49f9-45df-94c9-d86f7ea064e5\",\"uuid\":\"59d74b86-3e2f-4a93-aece-b05d2fdcde0c\"}],\"results\":{},\"run\":{\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"uuid\":\"e7187099-7d38-4f60-955c-325957214c42\"}}",
"response": "HTTP/1.0 200 OK\r\nContent-Length: 16\r\n\r\n{ \"ok\": \"true\" }",
"response": "HTTP/1.0 200 OK\r\nContent-Length: 16\r\nContent-Type: application/json\r\n\r\n{ \"ok\": \"true\" }",
"elapsed_ms": 0,
"retries": 0,
"status": "success",
Expand All @@ -113,12 +119,12 @@
"url": "http://unavailable.com/",
"status_code": 503,
"request": "POST / HTTP/1.1\r\nHost: unavailable.com\r\nUser-Agent: goflow-testing\r\nContent-Length: 898\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{\"channel\":null,\"contact\":{\"language\":\"eng\",\"name\":\"Ryan Lewis\",\"urn\":\"tel:+12065551212\",\"uuid\":\"5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f\"},\"flow\":{\"name\":\"Action Tester\",\"revision\":123,\"uuid\":\"bead76f5-dac4-4c9d-996c-c62b326e8c0a\"},\"input\":{\"attachments\":[{\"content_type\":\"image/jpeg\",\"url\":\"http://http://s3.amazon.com/bucket/test.jpg\"},{\"content_type\":\"audio/mp3\",\"url\":\"http://s3.amazon.com/bucket/test.mp3\"}],\"channel\":null,\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"text\":\"Hi everybody\",\"type\":\"msg\",\"urn\":{\"display\":\"(206) 555-1212\",\"path\":\"+12065551212\",\"scheme\":\"tel\"},\"uuid\":\"aa90ce99-3b4d-44ba-b0ca-79e63d9ed842\"},\"path\":[{\"arrived_on\":\"2018-10-18T14:20:30.000123Z\",\"exit_uuid\":\"\",\"node_uuid\":\"72a1f5df-49f9-45df-94c9-d86f7ea064e5\",\"uuid\":\"59d74b86-3e2f-4a93-aece-b05d2fdcde0c\"}],\"results\":{},\"run\":{\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"uuid\":\"e7187099-7d38-4f60-955c-325957214c42\"}}",
"response": "HTTP/1.0 503 Service Unavailable\r\nContent-Length: 37\r\n\r\n{ \"errors\": [\"service unavailable\"] }",
"response": "HTTP/1.0 503 Service Unavailable\r\nContent-Length: 19\r\nContent-Type: text/plain\r\n\r\nservice unavailable",
"elapsed_ms": 0,
"retries": 0,
"status": "response_error",
"resthook": "new-registration",
"extraction": "valid"
"extraction": "ignored"
}
],
"inspection": {
Expand All @@ -135,13 +141,19 @@
"http://temba.io/": [
{
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{ \"ok\": \"true\" }"
}
],
"http://unavailable.com/": [
{
"status": 503,
"body": "{ \"errors\": [\"service unavailable\"] }"
"headers": {
"Content-Type": "text/plain"
},
"body": "service unavailable"
}
]
},
Expand Down Expand Up @@ -214,7 +226,7 @@
"url": "http://temba.io/",
"status_code": 200,
"request": "POST / HTTP/1.1\r\nHost: temba.io\r\nUser-Agent: goflow-testing\r\nContent-Length: 898\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{\"channel\":null,\"contact\":{\"language\":\"eng\",\"name\":\"Ryan Lewis\",\"urn\":\"tel:+12065551212\",\"uuid\":\"5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f\"},\"flow\":{\"name\":\"Action Tester\",\"revision\":123,\"uuid\":\"bead76f5-dac4-4c9d-996c-c62b326e8c0a\"},\"input\":{\"attachments\":[{\"content_type\":\"image/jpeg\",\"url\":\"http://http://s3.amazon.com/bucket/test.jpg\"},{\"content_type\":\"audio/mp3\",\"url\":\"http://s3.amazon.com/bucket/test.mp3\"}],\"channel\":null,\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"text\":\"Hi everybody\",\"type\":\"msg\",\"urn\":{\"display\":\"(206) 555-1212\",\"path\":\"+12065551212\",\"scheme\":\"tel\"},\"uuid\":\"aa90ce99-3b4d-44ba-b0ca-79e63d9ed842\"},\"path\":[{\"arrived_on\":\"2018-10-18T14:20:30.000123Z\",\"exit_uuid\":\"\",\"node_uuid\":\"72a1f5df-49f9-45df-94c9-d86f7ea064e5\",\"uuid\":\"59d74b86-3e2f-4a93-aece-b05d2fdcde0c\"}],\"results\":{},\"run\":{\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"uuid\":\"e7187099-7d38-4f60-955c-325957214c42\"}}",
"response": "HTTP/1.0 200 OK\r\nContent-Length: 16\r\n\r\n{ \"ok\": \"true\" }",
"response": "HTTP/1.0 200 OK\r\nContent-Length: 16\r\nContent-Type: application/json\r\n\r\n{ \"ok\": \"true\" }",
"elapsed_ms": 0,
"retries": 0,
"status": "success",
Expand All @@ -228,12 +240,12 @@
"url": "http://unavailable.com/",
"status_code": 503,
"request": "POST / HTTP/1.1\r\nHost: unavailable.com\r\nUser-Agent: goflow-testing\r\nContent-Length: 898\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{\"channel\":null,\"contact\":{\"language\":\"eng\",\"name\":\"Ryan Lewis\",\"urn\":\"tel:+12065551212\",\"uuid\":\"5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f\"},\"flow\":{\"name\":\"Action Tester\",\"revision\":123,\"uuid\":\"bead76f5-dac4-4c9d-996c-c62b326e8c0a\"},\"input\":{\"attachments\":[{\"content_type\":\"image/jpeg\",\"url\":\"http://http://s3.amazon.com/bucket/test.jpg\"},{\"content_type\":\"audio/mp3\",\"url\":\"http://s3.amazon.com/bucket/test.mp3\"}],\"channel\":null,\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"text\":\"Hi everybody\",\"type\":\"msg\",\"urn\":{\"display\":\"(206) 555-1212\",\"path\":\"+12065551212\",\"scheme\":\"tel\"},\"uuid\":\"aa90ce99-3b4d-44ba-b0ca-79e63d9ed842\"},\"path\":[{\"arrived_on\":\"2018-10-18T14:20:30.000123Z\",\"exit_uuid\":\"\",\"node_uuid\":\"72a1f5df-49f9-45df-94c9-d86f7ea064e5\",\"uuid\":\"59d74b86-3e2f-4a93-aece-b05d2fdcde0c\"}],\"results\":{},\"run\":{\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"uuid\":\"e7187099-7d38-4f60-955c-325957214c42\"}}",
"response": "HTTP/1.0 503 Service Unavailable\r\nContent-Length: 37\r\n\r\n{ \"errors\": [\"service unavailable\"] }",
"response": "HTTP/1.0 503 Service Unavailable\r\nContent-Length: 19\r\nContent-Type: text/plain\r\n\r\nservice unavailable",
"elapsed_ms": 0,
"retries": 0,
"status": "response_error",
"resthook": "new-registration",
"extraction": "valid"
"extraction": "ignored"
},
{
"type": "run_result_changed",
Expand All @@ -242,18 +254,16 @@
"name": "My Result",
"value": "503",
"category": "Failure",
"input": "POST http://unavailable.com/",
"extra": {
"errors": [
"service unavailable"
]
}
"input": "POST http://unavailable.com/"
}
],
"webhook": {
"errors": [
"service unavailable"
]
"__default__": "POST http://unavailable.com/",
"headers": {
"Content-Type": "text/plain"
},
"json": null,
"status": 503
},
"inspection": {
"dependencies": [],
Expand Down Expand Up @@ -287,6 +297,9 @@
"http://temba.io/": [
{
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{ \"ok\": \"true\" }"
}
]
Expand Down Expand Up @@ -360,7 +373,7 @@
"url": "http://temba.io/",
"status_code": 200,
"request": "POST / HTTP/1.1\r\nHost: temba.io\r\nUser-Agent: goflow-testing\r\nContent-Length: 898\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{\"channel\":null,\"contact\":{\"language\":\"eng\",\"name\":\"Ryan Lewis\",\"urn\":\"tel:+12065551212\",\"uuid\":\"5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f\"},\"flow\":{\"name\":\"Action Tester\",\"revision\":123,\"uuid\":\"bead76f5-dac4-4c9d-996c-c62b326e8c0a\"},\"input\":{\"attachments\":[{\"content_type\":\"image/jpeg\",\"url\":\"http://http://s3.amazon.com/bucket/test.jpg\"},{\"content_type\":\"audio/mp3\",\"url\":\"http://s3.amazon.com/bucket/test.mp3\"}],\"channel\":null,\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"text\":\"Hi everybody\",\"type\":\"msg\",\"urn\":{\"display\":\"(206) 555-1212\",\"path\":\"+12065551212\",\"scheme\":\"tel\"},\"uuid\":\"aa90ce99-3b4d-44ba-b0ca-79e63d9ed842\"},\"path\":[{\"arrived_on\":\"2018-10-18T14:20:30.000123Z\",\"exit_uuid\":\"\",\"node_uuid\":\"72a1f5df-49f9-45df-94c9-d86f7ea064e5\",\"uuid\":\"59d74b86-3e2f-4a93-aece-b05d2fdcde0c\"}],\"results\":{},\"run\":{\"created_on\":\"2018-10-18T14:20:30.000123Z\",\"uuid\":\"e7187099-7d38-4f60-955c-325957214c42\"}}",
"response": "HTTP/1.0 200 OK\r\nContent-Length: 16\r\n\r\n{ \"ok\": \"true\" }",
"response": "HTTP/1.0 200 OK\r\nContent-Length: 16\r\nContent-Type: application/json\r\n\r\n{ \"ok\": \"true\" }",
"elapsed_ms": 0,
"retries": 0,
"status": "success",
Expand Down Expand Up @@ -395,7 +408,14 @@
}
],
"webhook": {
"ok": "true"
"__default__": "POST http://temba.io/",
"headers": {
"Content-Type": "application/json"
},
"json": {
"ok": "true"
},
"status": 200
},
"inspection": {
"dependencies": [],
Expand Down Expand Up @@ -622,9 +642,14 @@
}
],
"webhook": {
"errors": [
"service unavailable"
]
"__default__": "POST http://unavailable.com/",
"headers": {},
"json": {
"errors": [
"service unavailable"
]
},
"status": 503
},
"inspection": {
"dependencies": [],
Expand Down
Loading

0 comments on commit 6754507

Please sign in to comment.