1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
910use anyhow::bail;
11use mz_ore::collections::HashSet;
12use reqwest::Method;
13use reqwest::header::CONTENT_TYPE;
1415use crate::action::{ControlFlow, State};
16use crate::parser::BuiltinCommand;
1718pub async fn run_request(
19mut cmd: BuiltinCommand,
20_: &mut State,
21) -> Result<ControlFlow, anyhow::Error> {
22let url = cmd.args.string("url")?;
23let method: Method = cmd.args.parse("method")?;
24let content_type = cmd.args.opt_string("content-type");
25let further_accepted_status_codes = match cmd.args.opt_string("accept-additional-status-codes")
26 {
27Some(s) => s
28 .split(',')
29 .map(|s| s.parse::<u16>().unwrap())
30 .collect::<HashSet<u16>>(),
31None => HashSet::new(),
32 };
33let body = cmd.input.join("\n");
3435println!("$ http-request {} {}\n{}", method, url, body);
3637let client = reqwest::Client::new();
3839let mut request = client.request(method, &url).body(body);
4041if let Some(value) = &content_type {
42 request = request.header(CONTENT_TYPE, value);
43 }
4445let response = request.send().await?;
46let status = response.status();
4748println!("{}\n{}", status, response.text().await?);
4950if status.is_success() || further_accepted_status_codes.contains(&status.as_u16()) {
51Ok(ControlFlow::Continue)
52 } else {
53bail!("http request returned failing status: {}", status)
54 }
55}