Skip to main content

aperture_cli/cli/
legacy_execute.rs

1//! Legacy clap-based execution adapter.
2//!
3//! This module preserves the `execute_request(...)` API used by older callers
4//! and tests, while delegating to the new domain-based execution pipeline:
5//! `ArgMatches -> OperationCall/ExecutionContext -> execute() -> render`.
6
7use crate::cache::models::CachedSpec;
8use crate::cli::OutputFormat;
9use crate::config::models::GlobalConfig;
10use crate::engine::executor::RetryContext;
11use crate::error::Error;
12use crate::response_cache::CacheConfig;
13use clap::ArgMatches;
14
15/// Legacy wrapper that translates `ArgMatches` into domain types and delegates
16/// to [`crate::engine::executor::execute`]. Retained for backward compatibility.
17///
18/// # Errors
19///
20/// Returns errors for authentication failures, network issues, or JQ filter errors.
21#[allow(clippy::too_many_arguments)]
22#[allow(clippy::missing_panics_doc)]
23pub async fn execute_request(
24    spec: &CachedSpec,
25    matches: &ArgMatches,
26    base_url: Option<&str>,
27    dry_run: bool,
28    idempotency_key: Option<&str>,
29    global_config: Option<&GlobalConfig>,
30    output_format: &OutputFormat,
31    jq_filter: Option<&str>,
32    cache_config: Option<&CacheConfig>,
33    capture_output: bool,
34    retry_context: Option<&RetryContext>,
35) -> Result<Option<String>, Error> {
36    use crate::cli::translate;
37    use crate::invocation::ExecutionContext;
38
39    // Check --show-examples flag (CLI-only concern).
40    // NOTE: The primary path through `execute_api_command` also checks this
41    // flag before reaching here.  This duplicate check is intentional so that
42    // callers of the legacy `execute_request` API (tests, batch) still get
43    // correct behavior without depending on an outer guard.
44    if translate::has_show_examples_flag(matches) {
45        let operation_id = translate::matches_to_operation_id(spec, matches)?;
46        let operation = spec
47            .commands
48            .iter()
49            .find(|cmd| cmd.operation_id == operation_id)
50            .ok_or_else(|| Error::spec_not_found(&spec.name))?;
51        crate::cli::render::render_examples(operation);
52        return Ok(None);
53    }
54
55    // Translate ArgMatches → OperationCall
56    let call = translate::matches_to_operation_call(spec, matches)?;
57
58    // Build ExecutionContext from the individual parameters
59    let ctx = ExecutionContext {
60        dry_run,
61        idempotency_key: idempotency_key.map(String::from),
62        cache_config: cache_config.cloned(),
63        retry_context: retry_context.cloned(),
64        base_url: base_url.map(String::from),
65        global_config: global_config.cloned(),
66        server_var_args: translate::extract_server_var_args(matches),
67        auto_paginate: false,
68    };
69
70    // Execute using the new domain-type API
71    let result = crate::engine::executor::execute(spec, call, ctx).await?;
72
73    // Render to string or stdout based on capture_output
74    if capture_output {
75        crate::cli::render::render_result_to_string(&result, output_format, jq_filter)
76    } else {
77        crate::cli::render::render_result(&result, output_format, jq_filter)?;
78        Ok(None)
79    }
80}