uptrakit-openapi-client 0.0.4

Typed HTTP client for the Uptrakit web API
Documentation
use crate::Result;
use crate::UptrakitClient;
use crate::types_impl::surfaces::{
    InvokeSurfaceInteractionRequest, SurfaceProviderInfo, SurfaceReadResponse, SurfaceResponse,
};

impl UptrakitClient {
    /// List registered surfaces. Optional `slot` and `page` filters map to query params.
    pub async fn list_surfaces(
        &self,
        slot: Option<&str>,
        page: Option<&str>,
    ) -> Result<Vec<SurfaceResponse>> {
        let url = format!("{}{}", self.base_url, crate::paths::surfaces::BASE);
        let mut req = self.http.get(&url).bearer_auth(self.token_or_err()?);
        let mut query_params: Vec<(&str, &str)> = Vec::new();
        if let Some(slot) = slot {
            query_params.push(("slot", slot));
        }
        if let Some(page) = page {
            query_params.push(("page", page));
        }
        if !query_params.is_empty() {
            req = req.query(&query_params);
        }
        let resp = self.send_with_retry(req).await?;
        self.handle_response(resp).await
    }

    /// List targeted providers for a surface.
    pub async fn list_surface_providers(
        &self,
        surface_id: &str,
    ) -> Result<Vec<SurfaceProviderInfo>> {
        self.get(&crate::paths::surfaces::providers(surface_id))
            .await
    }

    /// Read a single surface with its interactions and data sources.
    pub async fn read_surface(&self, surface_id: &str) -> Result<SurfaceReadResponse> {
        self.get(&crate::paths::surfaces::read(surface_id)).await
    }

    /// Read a surface interaction via `GET`. `query` pairs are forwarded verbatim
    /// as query-string parameters (reserved keys included).
    pub async fn read_surface_interaction(
        &self,
        surface_id: &str,
        interaction_id: &str,
        query: &[(&str, String)],
    ) -> Result<serde_json::Value> {
        self.get_with_query(
            &crate::paths::surfaces::interaction(surface_id, interaction_id),
            &query,
        )
        .await
    }

    /// Invoke a surface interaction.
    pub async fn invoke_surface_interaction(
        &self,
        surface_id: &str,
        interaction_id: &str,
        request: &InvokeSurfaceInteractionRequest,
    ) -> Result<serde_json::Value> {
        self.post_json(
            &crate::paths::surfaces::interaction(surface_id, interaction_id),
            request,
        )
        .await
    }

    /// Update a surface interaction via `PUT`.
    pub async fn update_surface_interaction(
        &self,
        surface_id: &str,
        interaction_id: &str,
        request: &InvokeSurfaceInteractionRequest,
    ) -> Result<serde_json::Value> {
        self.put_json(
            &crate::paths::surfaces::interaction(surface_id, interaction_id),
            request,
        )
        .await
    }

    /// Delete a surface interaction via `DELETE`.
    pub async fn delete_surface_interaction(
        &self,
        surface_id: &str,
        interaction_id: &str,
        request: &InvokeSurfaceInteractionRequest,
    ) -> Result<serde_json::Value> {
        self.delete_json_body(
            &crate::paths::surfaces::interaction(surface_id, interaction_id),
            request,
        )
        .await
    }

    /// Read a surface interaction targeting a specific item via `GET .../{item_id}`.
    pub async fn read_surface_interaction_item(
        &self,
        surface_id: &str,
        interaction_id: &str,
        item_id: &str,
        query: &[(&str, String)],
    ) -> Result<serde_json::Value> {
        self.get_with_query(
            &crate::paths::surfaces::interaction_item(surface_id, interaction_id, item_id),
            &query,
        )
        .await
    }

    /// Update a surface interaction targeting a specific item via `PUT .../{item_id}`.
    pub async fn update_surface_interaction_item(
        &self,
        surface_id: &str,
        interaction_id: &str,
        item_id: &str,
        request: &InvokeSurfaceInteractionRequest,
    ) -> Result<serde_json::Value> {
        self.put_json(
            &crate::paths::surfaces::interaction_item(surface_id, interaction_id, item_id),
            request,
        )
        .await
    }

    /// Delete a surface interaction targeting a specific item via `DELETE .../{item_id}`.
    pub async fn delete_surface_interaction_item(
        &self,
        surface_id: &str,
        interaction_id: &str,
        item_id: &str,
        request: &InvokeSurfaceInteractionRequest,
    ) -> Result<serde_json::Value> {
        self.delete_json_body(
            &crate::paths::surfaces::interaction_item(surface_id, interaction_id, item_id),
            request,
        )
        .await
    }
}