use serde_json::Value;
use super::{types::DispatchError, McpServer};
impl McpServer {
pub(super) async fn get(&self, path: &str) -> Result<Value, DispatchError> {
let url = format!("{}{}", self.base_url, path);
let resp = self
.http
.get(&url)
.send()
.await
.map_err(|e| DispatchError::Transport(format!("GET {url}: {e}")))?;
let status = resp.status();
let text = resp
.text()
.await
.map_err(|e| DispatchError::Transport(format!("read {url}: {e}")))?;
if !status.is_success() {
return Err(DispatchError::Transport(format!(
"GET {url} returned {status}: {text}"
)));
}
let body: Value = serde_json::from_str(&text)
.map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
Ok(body)
}
pub(super) async fn get_query(
&self,
path: &str,
query: &[(&str, String)],
) -> Result<Value, DispatchError> {
let url = format!("{}{}", self.base_url, path);
let resp = self
.http
.get(&url)
.query(query)
.send()
.await
.map_err(|e| DispatchError::Transport(format!("GET {url}: {e}")))?;
let status = resp.status();
let text = resp
.text()
.await
.map_err(|e| DispatchError::Transport(format!("read {url}: {e}")))?;
if !status.is_success() {
return Err(DispatchError::Transport(format!(
"GET {url} returned {status}: {text}"
)));
}
let body: Value = serde_json::from_str(&text)
.map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
Ok(body)
}
pub(super) async fn get_text(
&self,
path: &str,
query: &[(&str, String)],
) -> Result<String, DispatchError> {
let url = format!("{}{}", self.base_url, path);
let resp = self
.http
.get(&url)
.query(query)
.send()
.await
.map_err(|e| DispatchError::Transport(format!("GET {url}: {e}")))?;
let status = resp.status();
let body = resp
.text()
.await
.map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
if !status.is_success() {
if status == reqwest::StatusCode::BAD_REQUEST {
return Err(DispatchError::InvalidParams(body));
}
return Err(DispatchError::Transport(format!(
"GET {url} returned {status}: {body}"
)));
}
Ok(body)
}
pub(super) async fn post(&self, path: &str, body: &Value) -> Result<Value, DispatchError> {
let url = format!("{}{}", self.base_url, path);
let resp = self
.http
.post(&url)
.json(body)
.send()
.await
.map_err(|e| DispatchError::Transport(format!("POST {url}: {e}")))?;
let status = resp.status();
let body: Value = resp
.json()
.await
.map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
if !status.is_success() {
if status == reqwest::StatusCode::BAD_REQUEST {
let msg = body
.get("error")
.and_then(Value::as_str)
.unwrap_or("bad request")
.to_owned();
return Err(DispatchError::InvalidParams(msg));
}
return Err(DispatchError::Transport(format!(
"POST {url} returned {status}: {body}"
)));
}
Ok(body)
}
pub(super) async fn delete(&self, path: &str) -> Result<Value, DispatchError> {
let url = format!("{}{}", self.base_url, path);
let resp = self
.http
.delete(&url)
.send()
.await
.map_err(|e| DispatchError::Transport(format!("DELETE {url}: {e}")))?;
let status = resp.status();
let text = resp
.text()
.await
.map_err(|e| DispatchError::Transport(format!("read {url}: {e}")))?;
if !status.is_success() {
return Err(DispatchError::Transport(format!(
"DELETE {url} returned {status}: {text}"
)));
}
let body: Value = serde_json::from_str(&text)
.map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
Ok(body)
}
}