Skip to main content

wellfriend/
embed.rs

1//! TOOL embed sessions: one tool workspace in your page.
2
3use reqwest::Method;
4use serde::Serialize;
5
6use crate::client::{json_body, Request};
7use crate::error::Result;
8use crate::generated::resources::EmbedSessions;
9use crate::Object;
10
11/// The fields of a TOOL embed session: one tool workspace in your page, charged like the API.
12///
13/// ```
14/// let session = wellfriend::ToolEmbedSession::new("compress-pdf", "https://app.example.com");
15/// ```
16#[derive(Debug, Clone, Default, PartialEq, Serialize)]
17#[serde(rename_all = "camelCase")]
18pub struct ToolEmbedSession {
19    /// The tool the page shows, for example `compress-pdf`.
20    pub tool_id: String,
21    /// The origin of the page that embeds the tool.
22    pub parent_origin: String,
23    /// Option values the workspace opens with.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub preset: Option<Object>,
26    /// Options the user cannot change.
27    #[serde(skip_serializing_if = "Vec::is_empty")]
28    pub locked_parameters: Vec<String>,
29    /// A stored document the workspace opens with (`documentId`, optional `versionId`).
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub source: Option<Object>,
32    /// Your own labels and colours.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub branding: Option<Object>,
35    /// Your own reference, returned with the session.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub host_reference: Option<String>,
38    /// How long the session lives, in seconds.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub ttl_seconds: Option<u32>,
41}
42
43impl ToolEmbedSession {
44    /// A session for `tool_id` embedded in the page at `parent_origin`.
45    pub fn new(tool_id: impl Into<String>, parent_origin: impl Into<String>) -> Self {
46        ToolEmbedSession {
47            tool_id: tool_id.into(),
48            parent_origin: parent_origin.into(),
49            ..Self::default()
50        }
51    }
52}
53
54#[derive(Serialize)]
55struct ToolEmbedSessionBody<'a> {
56    surface: &'static str,
57    #[serde(flatten)]
58    session: &'a ToolEmbedSession,
59}
60
61impl EmbedSessions {
62    /// Creates a TOOL embed session (`POST /api/v2/embed-sessions` with `surface: "TOOL"`).
63    /// Send its token and frame URL to your page.
64    pub async fn create_tool(&self, session: ToolEmbedSession) -> Result<Object> {
65        let body = ToolEmbedSessionBody {
66            surface: "TOOL",
67            session: &session,
68        };
69        let request =
70            Request::new(Method::POST, ["embed-sessions".to_owned()]).json(json_body(&body, None)?);
71        self.client.json(&self.options, request).await
72    }
73}