Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apiclient: add high-level update subcommands #1219

Merged
merged 2 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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