Lifecycle Hooks

circle-info

We currently only support lifecycle hook scripts written in Python.

What are Lifecycle Hooks?

A lifecycle hook is a way to add custom functionality during specific events in the life of a Stoobly resource such as a request or a response.

Stoobly's Lifecycle Hooks can be written as Python functions which use the Stoobly Python library to hook into these events and do addtional tasks.

Why use Lifecycle Hooks?

These hooks enable you to add custom functionality with your own code at different stages.

Some examples include:

  • Rewriting a header's value before or after its request gets sent to not store sensitive data

  • Adding, removing or modifying data such as in headers, query parameters, bodies, etc.

  • Adding extra debug information or publishing metrics

What are the Different Lifecycle Hook Events Supported?

chevron-righthandle_before_requesthashtag
from stoobly_agent.app.proxy.context import InterceptContext

def handle_before_request(context: InterceptContext):
    pass
chevron-righthandle_before_recordhashtag
chevron-righthandle_before_mockhashtag
chevron-righthandle_before_testhashtag
chevron-righthandle_after_recordhashtag
chevron-righthandle_after_mockhashtag
chevron-righthandle_after_testhashtag
chevron-righthandle_before_replayhashtag
chevron-righthandle_after_replayhashtag
chevron-righthandle_before_responsehashtag
chevron-righthandle_before_request_component_createhashtag
chevron-righthandle_before_request_component_deletehashtag

For the full list, see this linkarrow-up-right to the Stoobly Python definitions of the events available. All of the strings on the right side of the variable assignment are also the function names available in lifecycle hook scripts.

Types of Contexts

There are several types of contexts available for your scripts. The parameter type will change depending on what type of hook you are using. To see the fields and methods available, view the full definition as well as any parent classes they might be inherited from.

RecordContext

For record events use the RecordContext. The full definition can be found herearrow-up-right.

To import it, add this to the top of your script:

ReplayContext

For replay events use the ReplayContext. The full definition can be found herearrow-up-right.

To import it, add this to the top of your script:

MockContext

For mock events use the MockContext. The full definition can be found herearrow-up-right.

To import it, add this to the top of your script:

Enabling Lifecycle Hook Scripts Use

Once your script is written, it can now be used by the Agent. Lifecycle hooks are supported for the following commands:

  • stoobly-agent run

  • stoobly-agent request replay

  • stoobly-agent scenario replay

  • stoobly-agent endpoint import

Next Steps

Next see our guide on how to create your own lifecycle hooks and how to use them:

How to Customize Recordingschevron-right

Last updated