langgraph-api 0.1.1

Rust Client API of LangGraph
Documentation
/*
 * LangSmith Deployment
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * The version of the OpenAPI document: 0.1.0
 *
 * Generated by: https://openapi-generator.tech
 */

use super::{ContentType, Error, UploadFile, configuration};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};

/// struct for typed errors of method [`delete_mcp`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteMcpError {
    Status404(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_mcp`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetMcpError {
    Status405(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`post_mcp`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PostMcpError {
    Status400(),
    Status405(),
    Status500(),
    UnknownValue(serde_json::Value),
}

/// Implemented according to the Streamable HTTP Transport specification. Terminate an MCP session. The server implementation is stateless, so this is a no-op.  
pub fn delete_mcp_request_builder(
    configuration: &configuration::Configuration,
) -> Result<reqwest::RequestBuilder, Error<serde_json::Error>> {
    let uri_str = format!("{}/mcp/", configuration.base_path);
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::DELETE, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    Ok(req_builder)
}

pub async fn delete_mcp(
    configuration: &configuration::Configuration,
) -> Result<(), Error<DeleteMcpError>> {
    let req_builder =
        delete_mcp_request_builder(configuration).map_err(super::map_request_builder_error)?;
    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();

    if !status.is_client_error() && !status.is_server_error() {
        Ok(())
    } else {
        let content = resp.text().await?;
        let entity: Option<DeleteMcpError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Implemented according to the Streamable HTTP Transport specification.
pub fn get_mcp_request_builder(
    configuration: &configuration::Configuration,
) -> Result<reqwest::RequestBuilder, Error<serde_json::Error>> {
    let uri_str = format!("{}/mcp/", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    Ok(req_builder)
}

pub async fn get_mcp(
    configuration: &configuration::Configuration,
) -> Result<(), Error<GetMcpError>> {
    let req_builder =
        get_mcp_request_builder(configuration).map_err(super::map_request_builder_error)?;
    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();

    if !status.is_client_error() && !status.is_server_error() {
        Ok(())
    } else {
        let content = resp.text().await?;
        let entity: Option<GetMcpError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Implemented according to the Streamable HTTP Transport specification. Sends a JSON-RPC 2.0 message to the server.  - **Request**: Provide an object with `jsonrpc`, `id`, `method`, and optional `params`. - **Response**: Returns a JSON-RPC response or acknowledgment.  **Notes:** - Stateless: Sessions are not persisted across requests.
pub fn post_mcp_request_builder(
    configuration: &configuration::Configuration,
    accept: &str,
    body: serde_json::Value,
) -> Result<reqwest::RequestBuilder, Error<serde_json::Error>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_header_accept = accept;
    let p_body_body = body;

    let uri_str = format!("{}/mcp/", configuration.base_path);
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    req_builder = req_builder.header("Accept", p_header_accept.to_string());
    req_builder = req_builder.json(&p_body_body);

    Ok(req_builder)
}

pub async fn post_mcp(
    configuration: &configuration::Configuration,
    accept: &str,
    body: serde_json::Value,
) -> Result<serde_json::Value, Error<PostMcpError>> {
    let req_builder = post_mcp_request_builder(configuration, accept, body)
        .map_err(super::map_request_builder_error)?;
    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => Err(Error::from(serde_json::Error::custom(
                "Received `text/plain` content type response that cannot be converted to `serde_json::Value`",
            ))),
            ContentType::Unsupported(unknown_type) => {
                Err(Error::from(serde_json::Error::custom(format!(
                    "Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`"
                ))))
            }
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<PostMcpError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}