pdk-script 0.0.0-alpha.6

PDK
Documentation
pdk-script-0.0.0-alpha.6 has been yanked.

PDK Script   Latest Version pdk--script msrv

pdk-script provides scripting utilities and helpers for PDK-based custom policies.


You may be looking for:

pdk-script in action

[dependencies]
# This package is included as a transitive dependency of the pdk crate.
pdk = { version = "1.6"}
mod generated {
    use serde::Deserialize;
    #[derive(Deserialize, Clone, Debug)]
    pub struct Config {
        #[serde(
            alias = "exampleDateweaveProperty",
            deserialize_with = "de_example_dateweave_property_0"
        )]
        pub example_dateweave_property: pdk::script::Script,
    }
    fn de_example_dateweave_property_0<'de, D>(
        deserializer: D,
    ) -> Result<pdk::script::Script, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let exp: pdk::script::Expression = serde::de::Deserialize::deserialize(
            deserializer,
        )?;
        pdk::script::ScriptingEngine::script(&exp)
            .input(pdk::script::Input::Attributes)
            .input(pdk::script::Input::Authentication)
            .input(pdk::script::Input::Payload(pdk::script::Format::Json))
            .input(pdk::script::Input::Payload(pdk::script::Format::Xml))
            .input(pdk::script::Input::Payload(pdk::script::Format::PlainText))
            .input(pdk::script::Input::Vars("exampleVar"))
            .compile()
            .map_err(serde::de::Error::custom)
    }
}

use std::collections::HashMap;
use anyhow::{anyhow, Result};
use pdk::authentication::{Authentication, AuthenticationHandler};
use pdk::hl::*;
use pdk::script::{EvaluationError, HandlerAttributesBinding, Value};
use crate::generated::config::Config;


async fn request_filter(request_state: RequestState, stream_properties: StreamProperties, authentication: Authentication, config: &Config) {
    let headers_state = request_state.into_headers_state().await;

    // Create an evaluator of the dataweave expression provided in the configuration.
    let mut eval = config.example_dateweave_property.evaluator();

    // bind "attributes" to the dataweave expression. Only necessary if the binding "attributes" was set to true in the gcl.yaml.
    eval.bind_attributes(&HandlerAttributesBinding::new(headers_state.handler(), &stream_properties));

    // bind "authentication" to the dataweave expression. Only necessary if the binding "authentication" was set to true in the gcl.yaml.
    eval.bind_authentication(&authentication.authentication());

    let body_state = headers_state.into_body_state().await;

    // bind "payload" to the dataweave expression. Only necessary if the binding "payload" was configured in the gcl.yaml.
    eval.bind_payload(&body_state);

    // bind the var "exampleVar" to the dataweave expression. Only necessary if the binding "vars" was configured in the gcl.yaml.
    eval.bind_vars("exampleVar", "exampleValue");

    let result: Result<Value, EvaluationError> = eval.eval();
}

#[entrypoint]
async fn configure(launcher: Launcher, Configuration(bytes): Configuration) -> Result<()> {
    let config: Config = serde_json::from_slice(&bytes).map_err(|err| {
        anyhow!(
            "Failed to parse configuration '{}'. Cause: {}",
            String::from_utf8_lossy(&bytes),
            err
        )
    })?;

    // Inject the StreamProperties and Authentication to the on_request function.
    let filter = on_request(|rs, stream_properties, authentication| request_filter(rs, stream_properties, authentication, &config));

    launcher.launch(filter).await?;
    Ok(())
}

License