Skip to content

Commit

Permalink
Merge pull request #1219 from tjkirch/reboot-update
Browse files Browse the repository at this point in the history
apiclient: add high-level update subcommands
  • Loading branch information
tjkirch authored Nov 20, 2020
2 parents 0c93e9a + dd1f879 commit 7e79d42
Show file tree
Hide file tree
Showing 7 changed files with 714 additions and 31 deletions.
3 changes: 3 additions & 0 deletions sources/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions sources/api/apiclient/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ exclude = ["README.md"]
http = "0.2"
hyper = { version = "0.13", default-features = false }
hyper-unix-connector = "0.1"
log = "0.4"
serde_json = "1.0"
simplelog = "0.8"
snafu = "0.6"
tokio = { version = "0.2", default-features = false, features = ["macros", "rt-threaded"] }
tokio = { version = "0.2", default-features = false, features = ["macros", "rt-threaded", "time"] }
# When hyper updates to tokio 0.3:
#tokio = { version = "0.3", default-features = false, features = ["macros", "rt-multi-thread"] }
#tokio = { version = "0.3", default-features = false, features = ["macros", "rt-multi-thread", "time"] }
unindent = "0.1"

[build-dependencies]
Expand Down
45 changes: 33 additions & 12 deletions sources/api/apiclient/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use hyper_unix_connector::{UnixClient, Uri};
use snafu::{ensure, ResultExt};
use std::path::Path;

pub mod update;

mod error {
use snafu::Snafu;

Expand All @@ -34,7 +36,7 @@ mod error {
ResponseStatus {
method: String,
code: http::StatusCode,
uri: http::uri::Uri,
uri: String,
body: String,
},

Expand Down Expand Up @@ -69,6 +71,36 @@ pub async fn raw_request<P, S1, S2>(
method: S2,
data: Option<String>,
) -> Result<(http::StatusCode, String)>
where
P: AsRef<Path>,
S1: AsRef<str>,
S2: AsRef<str>,
{
let (status, body) = raw_request_unchecked(&socket_path, &uri, &method, data).await?;

// Error if the response status is in not in the 2xx range.
ensure!(
status.is_success(),
error::ResponseStatus {
method: method.as_ref(),
code: status,
uri: uri.as_ref(),
body,
}
);

Ok((status, body))
}

/// Works exactly like raw_request in making an HTTP request over a Unix-domain socket, but doesn't
/// check that the returned status code represents success. This can be useful if you have to
/// handle specific error codes, rather than inspecting the Error type of raw_request.
pub async fn raw_request_unchecked<P, S1, S2>(
socket_path: P,
uri: S1,
method: S2,
data: Option<String>,
) -> Result<(http::StatusCode, String)>
where
P: AsRef<Path>,
S1: AsRef<str>,
Expand Down Expand Up @@ -103,16 +135,5 @@ where
.context(error::ResponseBodyRead)?;
let body = String::from_utf8(body_bytes.to_vec()).context(error::NonUtf8Response)?;

// Error if the response status is in not in the 2xx range.
ensure!(
status.is_success(),
error::ResponseStatus {
method,
code: status,
uri,
body,
}
);

Ok((status, body))
}
Loading

0 comments on commit 7e79d42

Please sign in to comment.