Customizing with Lifecycle Hooks

Background

Lifecycle Hooks

Writing a Lifecycle Hooks Script

  1. Write a Python script (e.g. lifecycle_hooks.py)

  2. In this script, define the lifecycle events to hook into

  3. Define what kind of behavior to execute for each of those events

Below is a sample script that prints some strings during two lifecycle events.

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

def handle_before_request(context: InterceptContext):
    print('Before request!')

def handle_before_record(context: RecordContext):
    print('Before record!')
  • handle_before_request is for before a request gets sent

  • handle_before_record is for before a request gets recorded

After a request gets intercepted, the string "Before request!" will be printed. After the response returns, but before the request gets recorded, "Before record!" will be printed.

Enabling Lifecycle Hook Script Use

Pass the path to the lifecycle hooks scripts using the --lifecycle-hooks-path option:

stoobly-agent run --lifecycle-hooks-path ~/path-to-file/lifecycle_hooks.py

Example

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

def handle_before_record(context: RecordContext):
    request = context.flow.request
    respose = context.flow.response
    
    request.headers['Authorization'] = '<REDACTED>'
    request.headers['Cookie'] = ''
    
    response.headers['Set-Cookie'] = ''

Last updated

Was this helpful?