Skip to main content

taskfast_cli/cmd/events/
ack.rs

1// SPDX-License-Identifier: MIT
2//! `taskfast events ack <id>` — advance server-side event cursor.
3//!
4//! Written as a raw reqwest POST rather than a progenitor-generated call
5//! because the ack endpoint lives outside the OpenAPI surface (the live
6//! event stream is specified via AsyncAPI — see [`super::schema`]). The
7//! request is still routed through the shared [`taskfast_client::TaskFastClient`]'s
8//! inner reqwest instance so the `X-API-Key` header, timeouts, and
9//! non-2xx classification stay identical to every other call.
10
11use clap::Parser;
12
13use super::super::{CmdError, CmdResult, Ctx};
14use crate::envelope::Envelope;
15
16use taskfast_client::ClientInfo;
17use taskfast_client::Error as ClientError;
18
19#[derive(Debug, Parser)]
20pub struct AckArgs {
21    /// Event id from the live stream (UUID).
22    pub event_id: String,
23}
24
25pub async fn run(ctx: &Ctx, args: AckArgs) -> CmdResult {
26    let client = ctx.client()?;
27    let url = format!(
28        "{}/agents/me/events/{}/ack",
29        client.inner().baseurl(),
30        args.event_id
31    );
32    let resp = client
33        .inner()
34        .client()
35        .post(&url)
36        .send()
37        .await
38        .map_err(|e| CmdError::from(ClientError::from(e)))?;
39
40    let status = resp.status();
41    let body = resp
42        .text()
43        .await
44        .map_err(|e| CmdError::Network(e.to_string()))?;
45
46    if !status.is_success() {
47        return Err(classify_ack_error(status.as_u16(), &body));
48    }
49
50    let value: serde_json::Value = serde_json::from_str(&body)
51        .map_err(|e| CmdError::Decode(format!("ack response not JSON: {e}")))?;
52
53    Ok(Envelope::success(ctx.environment, ctx.dry_run, value))
54}
55
56fn classify_ack_error(code: u16, body: &str) -> CmdError {
57    let parsed: serde_json::Value = serde_json::from_str(body).unwrap_or(serde_json::Value::Null);
58    let err_code = parsed
59        .get("error")
60        .and_then(|v| v.as_str())
61        .unwrap_or("")
62        .to_string();
63    let msg = parsed
64        .get("message")
65        .and_then(|v| v.as_str())
66        .unwrap_or(body)
67        .to_string();
68
69    match code {
70        401 | 403 => CmdError::Auth(format!("HTTP {code}: {msg}")),
71        422 => CmdError::Validation {
72            code: or_default(&err_code, "validation_error"),
73            message: msg,
74        },
75        404 => CmdError::Validation {
76            code: or_default(&err_code, "not_found"),
77            message: msg,
78        },
79        400..=499 => CmdError::Validation {
80            code: or_default(&err_code, &format!("http_{code}")),
81            message: msg,
82        },
83        500..=599 => CmdError::Server(format!("HTTP {code}: {msg}")),
84        _ => CmdError::Server(format!("unexpected status {code}: {msg}")),
85    }
86}
87
88fn or_default(s: &str, fallback: &str) -> String {
89    if s.is_empty() {
90        fallback.to_string()
91    } else {
92        s.to_string()
93    }
94}