coil-runtime 0.1.0

HTTP runtime and request handling for the Coil framework.
Documentation
use super::*;

impl WasmHost {
    pub(super) fn request_context(
        &self,
        execution: &RequestExecution,
        input: InvocationInput,
    ) -> Result<InvocationContext, WasmModelError> {
        let customer_app =
            self.customer_app_context(Some(&execution.locale), execution.site_id.as_deref())?;
        let principal = match (
            execution.principal.principal_id.as_deref(),
            execution.principal.principal_kind,
        ) {
            (Some(principal_id), RequestPrincipalKind::ServiceAccount) => {
                PrincipalRef::service_account(principal_id.to_string())?
            }
            (Some(principal_id), _) => PrincipalRef::user(principal_id.to_string())?,
            (None, _) => PrincipalRef::anonymous(),
        };
        let trace = TraceContext::new(execution.trace.request_id.clone())?
            .with_request_id(execution.trace.request_id.clone())?;

        Ok(InvocationContext::new(
            customer_app,
            principal,
            trace,
            input,
        ))
    }

    pub(super) fn async_context(
        &self,
        trace_id: String,
        principal: ExtensionPrincipal,
        input: InvocationInput,
    ) -> Result<InvocationContext, WasmModelError> {
        let customer_app = self.customer_app_context(None, None)?;
        let principal = principal.to_wasm_principal()?;
        let trace = TraceContext::new(trace_id)?;

        Ok(InvocationContext::new(
            customer_app,
            principal,
            trace,
            input,
        ))
    }

    pub(super) fn customer_app_context(
        &self,
        locale: Option<&str>,
        site_id: Option<&str>,
    ) -> Result<CustomerAppContext, WasmModelError> {
        let mut customer_app = CustomerAppContext::new(self.customer_app.clone())?;
        customer_app = customer_app.with_tenant_id(self.tenant_id.to_string())?;
        customer_app =
            customer_app.with_locale(locale.unwrap_or(self.default_locale.as_str()).to_string())?;
        if let Some(site_id) = site_id {
            customer_app = customer_app.with_site_id(site_id.to_string())?;
        }
        Ok(customer_app)
    }
}