windmill_api/apis/
mcp_oauth_api.rs

1/*
2 * Windmill API
3 *
4 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5 *
6 * The version of the OpenAPI document: 1.608.0
7 * Contact: contact@windmill.dev
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration};
16
17
18/// struct for typed errors of method [`discover_mcp_o_auth`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum DiscoverMcpOAuthError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`mcp_o_auth_callback`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum McpOAuthCallbackError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`start_mcp_o_auth_popup`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum StartMcpOAuthPopupError {
36    UnknownValue(serde_json::Value),
37}
38
39
40pub async fn discover_mcp_o_auth(configuration: &configuration::Configuration, discover_mcp_o_auth_request: models::DiscoverMcpOAuthRequest) -> Result<models::DiscoverMcpOAuth200Response, Error<DiscoverMcpOAuthError>> {
41    let local_var_configuration = configuration;
42
43    let local_var_client = &local_var_configuration.client;
44
45    let local_var_uri_str = format!("{}/mcp/oauth/discover", local_var_configuration.base_path);
46    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
47
48    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
49        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
50    }
51    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
52        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
53    };
54    local_var_req_builder = local_var_req_builder.json(&discover_mcp_o_auth_request);
55
56    let local_var_req = local_var_req_builder.build()?;
57    let local_var_resp = local_var_client.execute(local_var_req).await?;
58
59    let local_var_status = local_var_resp.status();
60    let local_var_content = local_var_resp.text().await?;
61
62    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
63        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
64    } else {
65        let local_var_entity: Option<DiscoverMcpOAuthError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
66        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
67        Err(Error::ResponseError(local_var_error))
68    }
69}
70
71/// Handles OAuth callback, exchanges code for tokens, returns HTML that posts message to opener
72pub async fn mcp_o_auth_callback(configuration: &configuration::Configuration, code: &str, state: &str) -> Result<String, Error<McpOAuthCallbackError>> {
73    let local_var_configuration = configuration;
74
75    let local_var_client = &local_var_configuration.client;
76
77    let local_var_uri_str = format!("{}/mcp/oauth/callback", local_var_configuration.base_path);
78    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
79
80    local_var_req_builder = local_var_req_builder.query(&[("code", &code.to_string())]);
81    local_var_req_builder = local_var_req_builder.query(&[("state", &state.to_string())]);
82    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
83        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
84    }
85
86    let local_var_req = local_var_req_builder.build()?;
87    let local_var_resp = local_var_client.execute(local_var_req).await?;
88
89    let local_var_status = local_var_resp.status();
90    let local_var_content = local_var_resp.text().await?;
91
92    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
93        crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).map_err(Error::from)
94    } else {
95        let local_var_entity: Option<McpOAuthCallbackError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
96        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
97        Err(Error::ResponseError(local_var_error))
98    }
99}
100
101/// Opens in a popup, discovers OAuth metadata, registers client, and redirects to OAuth provider
102pub async fn start_mcp_o_auth_popup(configuration: &configuration::Configuration, mcp_server_url: &str, scopes: Option<&str>) -> Result<(), Error<StartMcpOAuthPopupError>> {
103    let local_var_configuration = configuration;
104
105    let local_var_client = &local_var_configuration.client;
106
107    let local_var_uri_str = format!("{}/mcp/oauth/start", local_var_configuration.base_path);
108    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
109
110    local_var_req_builder = local_var_req_builder.query(&[("mcp_server_url", &mcp_server_url.to_string())]);
111    if let Some(ref local_var_str) = scopes {
112        local_var_req_builder = local_var_req_builder.query(&[("scopes", &local_var_str.to_string())]);
113    }
114    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
115        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
116    }
117    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
118        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
119    };
120
121    let local_var_req = local_var_req_builder.build()?;
122    let local_var_resp = local_var_client.execute(local_var_req).await?;
123
124    let local_var_status = local_var_resp.status();
125    let local_var_content = local_var_resp.text().await?;
126
127    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
128        Ok(())
129    } else {
130        let local_var_entity: Option<StartMcpOAuthPopupError> = crate::from_str_patched/* Externally injected from /build.nu */(&local_var_content).ok();
131        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
132        Err(Error::ResponseError(local_var_error))
133    }
134}
135