Skip to main content

soma_openapi/
dispatch.rs

1use crate::error::OpenApiError;
2use crate::registry::OpenApiRegistry;
3
4pub async fn dispatch_openapi_call(
5    registry: &OpenApiRegistry,
6    client: &reqwest::Client,
7    label: &str,
8    operation_id: &str,
9    params: serde_json::Value,
10) -> Result<serde_json::Value, OpenApiError> {
11    let handle = registry.operation(label, operation_id)?;
12    let host = handle.base_url.host_str().unwrap_or_default().to_string();
13    let method = handle.method.clone();
14    let started = std::time::Instant::now();
15    let result = crate::http::execute_operation(client, handle, params).await;
16    log_dispatch(label, operation_id, &host, &method, started, &result);
17    result
18}
19
20#[cfg(test)]
21pub(crate) async fn dispatch_openapi_call_no_ssrf(
22    registry: &OpenApiRegistry,
23    client: &reqwest::Client,
24    label: &str,
25    operation_id: &str,
26    params: serde_json::Value,
27) -> Result<serde_json::Value, OpenApiError> {
28    let handle = registry.operation(label, operation_id)?;
29    let host = handle.base_url.host_str().unwrap_or_default().to_string();
30    let method = handle.method.clone();
31    let started = std::time::Instant::now();
32    let result = crate::http::execute_operation_no_ssrf(client, handle, params).await;
33    log_dispatch(label, operation_id, &host, &method, started, &result);
34    result
35}
36
37fn log_dispatch(
38    label: &str,
39    operation_id: &str,
40    host: &str,
41    method: &reqwest::Method,
42    started: std::time::Instant,
43    result: &Result<serde_json::Value, OpenApiError>,
44) {
45    let elapsed_ms = started.elapsed().as_millis();
46    match result {
47        Ok(_) => tracing::info!(
48            service = "openapi",
49            action = operation_id,
50            label = %label,
51            host = %host,
52            method = %method,
53            status = "ok",
54            elapsed_ms = elapsed_ms as u64,
55            "openapi dispatch complete"
56        ),
57        Err(error) => tracing::warn!(
58            service = "openapi",
59            action = operation_id,
60            label = %label,
61            host = %host,
62            method = %method,
63            status = "error",
64            kind = error.kind(),
65            elapsed_ms = elapsed_ms as u64,
66            "openapi dispatch failed"
67        ),
68    }
69}