wellfriend-document-sdk 1.0.0

Official async Rust SDK for the Wellfriend document API: typed document tools, pipelines, streaming uploads and downloads, retries and webhook verification.
Documentation
//! TOOL embed sessions: one tool workspace in your page.

use reqwest::Method;
use serde::Serialize;

use crate::client::{json_body, Request};
use crate::error::Result;
use crate::generated::resources::EmbedSessions;
use crate::Object;

/// The fields of a TOOL embed session: one tool workspace in your page, charged like the API.
///
/// ```
/// let session = wellfriend::ToolEmbedSession::new("compress-pdf", "https://app.example.com");
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolEmbedSession {
    /// The tool the page shows, for example `compress-pdf`.
    pub tool_id: String,
    /// The origin of the page that embeds the tool.
    pub parent_origin: String,
    /// Option values the workspace opens with.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub preset: Option<Object>,
    /// Options the user cannot change.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub locked_parameters: Vec<String>,
    /// A stored document the workspace opens with (`documentId`, optional `versionId`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<Object>,
    /// Your own labels and colours.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branding: Option<Object>,
    /// Your own reference, returned with the session.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub host_reference: Option<String>,
    /// How long the session lives, in seconds.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ttl_seconds: Option<u32>,
}

impl ToolEmbedSession {
    /// A session for `tool_id` embedded in the page at `parent_origin`.
    pub fn new(tool_id: impl Into<String>, parent_origin: impl Into<String>) -> Self {
        ToolEmbedSession {
            tool_id: tool_id.into(),
            parent_origin: parent_origin.into(),
            ..Self::default()
        }
    }
}

#[derive(Serialize)]
struct ToolEmbedSessionBody<'a> {
    surface: &'static str,
    #[serde(flatten)]
    session: &'a ToolEmbedSession,
}

impl EmbedSessions {
    /// Creates a TOOL embed session (`POST /api/v2/embed-sessions` with `surface: "TOOL"`).
    /// Send its token and frame URL to your page.
    pub async fn create_tool(&self, session: ToolEmbedSession) -> Result<Object> {
        let body = ToolEmbedSessionBody {
            surface: "TOOL",
            session: &session,
        };
        let request =
            Request::new(Method::POST, ["embed-sessions".to_owned()]).json(json_body(&body, None)?);
        self.client.json(&self.options, request).await
    }
}