Skip to main content

Hooks

Hooks are callbacks triggered by external HTTP calls implementing webhooks. Their primary use case is handling incoming data from async processes that depend on external services, such as payment systems and document signatures, instead of user actions.

The URL path associated with each Hook may be changed in its settings on our editor web UI.

Request object

from abstra.hooks import get_request

body, query, headers = get_request()

Function definition

def get_request(local_file=None):

Parameters

Keyword Arguments
local_file (string)Fallback file location for testing locally

Returns

Returns
(parsed json or string)Body parsed according to content-type header
(dict)Query parameters
(dict)Headers

Response object

To send the response object you have to use one of our built-in util functions: send_response or send_json.

send_response

from abstra.hooks import send_response

send_response(body="plain text body")
def send_response(body=None, status_code=200, headers=None, local_file=None):
Keyword ArgumentsDescription
body (string)Plain text response body
status_code (number)Http response status code
headers (dict)Headers to be sent back
local_file (string)Fallback file location for testing locally

send_json

from abstra.hooks import send_json

send_json(data={'ok': True}, status_code=200, headers={"ABSTRA": "CLOUD"})
def send_json(data=None, status_code=200, headers=None, local_file=None):
Keyword ArgumentsDescription
data (any serializable value)Data to be serialized and sent back
status_code (number)Http response status code
headers (dict)Headers to be sent back, in addition to content-type: application/json
local_file (string)Fallback file location for testing locally

Hooks in a Workflow

Every Hook that depends on previous stages in a Workflow must use the match_thread function. This is how the Hook matches the incoming information to a thread waiting in that Stage.

Consider a Workflow for contract signature with four stages:

  1. Fill Contract - filling the necessary information with a Form
  2. Send Contract - generating the document and sending it to/Users/ybere/Desktop/abstracloud/docusaurus/docs/hooks/hooks.md the client
  3. Await Signature - awaiting for the document to be sent back
  4. Send Confirmation - notifying the people involved that the process has been completed

Contract Signing

After the Fill Contract and Send Contract stages, threads stay waiting in the Await Signature stage until the Hook acknowledges a contract has been signed to resume the process. To do so, use the match_thread function.

match_thread

def match_thread(filter=dict):
Keyword ArgumentsDescription
filter (dict)Data to match a thread to be resumed

Usage

The match_thread function finds a corresponding waiting thread in the current stage by searching for pieces of data already set in previous stages.

from abstra.workflows import match_thread
from abstra.hooks import get_request, send_json

body, query, headers = get_request()

filter = {
"contractId": body["contract_id"],
"clientId": body["client_id"]
}

try:
match_thread(filter)
send_json(data={'ok': True}, status_code=200)

except:
print(f"Unable to find an corresponding contract")
send_json(data={'ok': False}, status_code=404)

The filter must be an object with only primitive types as values. For the thread to be matched, all keys in filter must be set with the corresponding values in previous stages using the set_data function.

danger

If no thread is found, an error will be thrown. If more than one thread is found, one of them will be randomly selected.

To avoid dubious side effects, try to use values that are unique across all threads.