use anyhow::bail;
use crate::action::{ControlFlow, State};
use crate::parser::BuiltinCommand;
pub async fn run_append(
mut cmd: BuiltinCommand,
state: &State,
) -> Result<ControlFlow, anyhow::Error> {
let database = cmd
.args
.opt_string("database")
.unwrap_or_else(|| "materialize".to_string());
let schema = cmd
.args
.opt_string("schema")
.unwrap_or_else(|| "public".to_string());
let name = cmd.args.string("name")?;
let status_code = cmd.args.opt_parse::<u16>("status")?;
let headers: Vec<(String, String)> = cmd.args.into_iter().collect();
let body = cmd.input.join("\n");
println!("$ webhook-append {database}.{schema}.{name}\n{body}\n{headers:?}");
let client = if state.materialize.use_https {
reqwest::Client::builder()
.danger_accept_invalid_certs(true)
.build()
.unwrap()
} else {
reqwest::Client::new()
};
let url = format!(
"{}://{}/api/webhook/{database}/{schema}/{name}",
if state.materialize.use_https {
"https"
} else {
"http"
},
state.materialize.http_addr
);
let mut builder = client.post(url).body(body);
for (name, value) in headers {
builder = builder.header(name, value);
}
let response = builder.send().await?;
let status = response.status();
println!("{}\n{}", status, response.text().await?);
let expected_status = status_code.unwrap_or(200);
if status.as_u16() == expected_status {
Ok(ControlFlow::Continue)
} else {
bail!("webhook append returned unexpected status: {}", status);
}
}