use crate::auth::auth_manager::AuthManager;
use crate::core::config_schema::Config;
use crate::data::store::EndpointRecord;
use crate::services::api_client::ApiClient;
use crate::validation::validator::{validate_input, validate_output};
pub async fn call_operation(
endpoint: &EndpointRecord,
config: &Config,
auth_manager: &mut AuthManager,
operation_id: &str,
args: serde_json::Value,
) -> anyhow::Result<serde_json::Value> {
validate_input(&config.api_version, operation_id, &args)?;
let client = ApiClient::new(config.clone());
let response = client.execute(endpoint, &args, auth_manager).await?;
if let Err(err) = validate_output(&config.api_version, operation_id, &response) {
tracing::warn!(
operation_id,
error = %err,
"response did not match the documented schema; returning it as-is"
);
}
Ok(response)
}
#[cfg(test)]
mod tests {
use super::*;
fn test_config() -> Config {
Config {
url: "localhost".to_string(),
auth_method: crate::core::config_schema::AuthMethod::SqlServer,
sql_port: 1433,
pool_max_size: 10,
trust_server_cert: true,
api_version: "2025".to_string(),
log_level: "info".to_string(),
rate_limit: 100,
timeout_ms: 30_000,
cache_size: 500,
retry_attempts: 3,
transport: crate::core::config_schema::Transport::Stdio,
host: "127.0.0.1".to_string(),
port: 3000,
cors_allow: None,
default_database: None,
}
}
fn dummy_endpoint(operation_id: &str) -> EndpointRecord {
EndpointRecord {
operation_id: operation_id.to_string(),
path: "/dbo/sp_MailItemResultSets".to_string(),
method: "POST".to_string(),
summary: None,
description: None,
input_schema: serde_json::Value::Null,
output_schema: serde_json::Value::Null,
auth_scheme_ref: None,
}
}
#[tokio::test]
async fn call_operation_rejects_invalid_input_before_touching_the_network() {
let operation_id = "dbo_sp_MailItemResultSets";
let endpoint = dummy_endpoint(operation_id);
let config = test_config();
let mut auth_manager = AuthManager::new(config.auth_method);
let result = call_operation(
&endpoint,
&config,
&mut auth_manager,
operation_id,
serde_json::json!({"body": {}}),
)
.await;
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("invalid input"));
}
}