Lifecycle Hooks

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?

handle_after_mock
from stoobly_agent.app.proxy.mock.context import MockContext

def handle_after_mock(context: MockContext):
    pass
handle_after_record
from stoobly_agent.app.proxy.record.context import RecordContext

def handle_after_record(context: RecordContext):
    pass
handle_after_replay
from stoobly_agent.app.proxy.replay.context import ReplayContext

def handle_after_replay(context: ReplayContext):
    pass
handle_before_mock
from stoobly_agent.app.proxy.mock.context import MockContext

def handle_before_mock(context: MockContext):
    pass
handle_before_record
from stoobly_agent.app.proxy.record.context import RecordContext

def handle_before_record(context: RecordContext):
    pass
handle_before_replay
from stoobly_agent.app.proxy.replay.context import ReplayContext

def handle_before_replay(context: ReplayContext):
    pass
handle_before_request
from stoobly_agent.app.proxy.context import InterceptContext

def handle_before_request(context: InterceptContext):
    pass
handle_before_response
from stoobly_agent.app.proxy.context import InterceptContext

def handle_before_response(context: InterceptContext):
    pass
handle_before_request_component_create
from stoobly_agent.app.proxy.test.context import TestContext

def handle_before_request_component_create(context: TestContext):
    pass
handle_before_request_component_delete
from stoobly_agent.app.proxy.test.context import TestContext

def handle_before_request_component_delete(context: TestContext):
    pass

For the full list, see this link 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 here.

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

from stoobly_agent.app.proxy.record.context import RecordContext

ReplayContext

For replay events use the ReplayContext. The full definition can be found here.

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

from stoobly_agent.app.proxy.replay.context import ReplayContext

MockContext

For mock events use the MockContext. The full definition can be found here.

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

from stoobly_agent.app.proxy.mock.context import MockContext

Next Steps

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

How to Create Lifecycle Hooks

Last updated