Contact Us 1-800-596-4880

Injecting Parameters

Flex Gateway Policy Development Kit (PDK) provides the following parameters that you can inject into the #[entrypoint] configuration function:

  • Configuration: Provides the policy’s configuration parameters. For information about how to define configuration parameters, see Defining a Policy Schema Definition.

  • Metadata: Provides metadata about the policy, the Flex Gateway instance, the API instance, and the Anypoint Organization. For more information about the metadata provided, see Accessing Policy Metadata.

  • HttpClient: Enables the policy to make HTTP calls. For more information about how to make HTTP calls from the policy, see Performing an HTTP Call.

  • CacheBuilder: Provides the caching features of the policy. For more information about caching, see Sharing Data Between Workers and Configuring Caching.

  • StreamProperties: Provides a structure to share properties with other policies that process the same request. For more information about sharing information between policies, see Sharing Properties Across Requests.

You can also directly inject the HttpClient and StreamProperties parameter into the on_request or on_response wrapped functions. For example, if you need to perform an HTTP call inside the on_request function, inject HTTPClient directly into that function.

For best practice, inject the parameters only where they must be used. However, if the parameters require initialization steps that need to be executed only one time when the policy is applied, it is best to inject the parameters into the #[entrypoint] to complete the configuration steps to increase the performance of the wrapped functions.

Inject Parameters into the on_request and on_response Wrapped Functions

The wrapped functions accept the following parameters:

  • HttpClient: Enables the policy to make HTTP calls. For more information about how to make HTTP calls from the policy, see Performing an HTTP Call.

  • StreamProperties: Provides a structure to share properties with other policies that process the same request. For more information about sharing information between policies, see Sharing Properties Across Requests.

  • RequestState: Provides access to the different stages of request filtering to operate over the incoming requests headers and body. RequestState is only available in on_request functions. To read and write to the headers and body in the`RequestState`, see Reading and Writing Request Headers and Bodies.

  • ResponseState: Provides access to the different stages of request filtering to operate over the upstream responses headers and body. ResponseState is only available in on_response functions. To read and write to the headers and body in the ResponseState, see Reading and Writing Request Headers and Bodies.

  • RequestData: Share data between the on_request and`on_response` functions. RequestData is only available in on_response functions. To learn more about RequestData, see Sharing Data Between Requests and Responses.

  • Authentication: Provides access to read authentication data or share it with other policies. To learn more about Authentication, see Accessing Request Authentication Information.

If your wrapped functions only receive these parameters, the lambda is not required in the wrapper. For example, if the signature of the function is:

async fn request_filter(state: RequestState) -> RequestData<String>;
async fn response_filter(state: ResponseState, path: RequestData<String>);

The following example demonstrates defining the lambda of a function definition:

let filter = on_request(|request_state| request_filter(request_state))
    .on_response(|response_state, request_data| response_filter(response_state, request_data));

Instead of defining the lambda, you can use the following function definition:

let filter = on_request(request_filter)
        .on_response(response_filter);

If your wrapped functions require other parameters, such as the configuration parameters defined in the #[entrypoint] function, inject the parameters into the wrapped functions. To pass the parameters, define the lambda and send a variable reference, for example:

async fn request_filter(state: RequestState, conf: &Config, tuple: &(u32, u32)) {
...
}

#[entrypoint]
async fn configure(launcher: Launcher, Configuration(bytes): Configuration) -> Result<()> {
    let config = serde_json::from_slice(&bytes)?;
    let tuple: (u32, u32) = (10, 10);

    let filter = on_request(|request_state| request_filter(request_state, &config, &tuple));

    launcher
        .launch(filter)
        .await?;

    Ok(())
}