feat: allow expecting status codes
This commit is contained in:
parent
147186f8dc
commit
23abb7706e
3 changed files with 14 additions and 2 deletions
|
@ -11,6 +11,10 @@ url = "https://api.alemi.dev/"
|
|||
url = "https://api.alemi.dev/look/into/the/void"
|
||||
extract = { type = "discard" }
|
||||
|
||||
[route.notfound]
|
||||
url = "https://api.alemi.dev/not-found"
|
||||
expect = 404
|
||||
|
||||
[route.debug]
|
||||
url = "https://api.alemi.dev/debug"
|
||||
method = "PUT"
|
||||
|
|
|
@ -37,6 +37,9 @@ pub enum PostWomanError {
|
|||
#[error("invalid regex: {0:?}")]
|
||||
InvalidRegex(#[from] regex::Error),
|
||||
|
||||
#[error("request didn't match expected status code: {0:?}")]
|
||||
UnexpectedStatusCode(reqwest::Response),
|
||||
|
||||
#[error("invalid Json Query: {0}")]
|
||||
JQError(String),
|
||||
|
||||
|
|
|
@ -21,6 +21,8 @@ pub struct Endpoint {
|
|||
pub headers: Option<Vec<String>>,
|
||||
/// body, optional string
|
||||
pub body: Option<StringOr<toml::Table>>,
|
||||
/// expected error code, will fail if different
|
||||
pub expect: Option<u16>,
|
||||
/// response extractor
|
||||
pub extract: Option<StringOr<Extractor>>,
|
||||
}
|
||||
|
@ -135,8 +137,11 @@ impl Endpoint {
|
|||
.headers(headers)
|
||||
.body(body)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
.await?;
|
||||
|
||||
if res.status().as_u16() != self.expect.unwrap_or(200) {
|
||||
return Err(PostWomanError::UnexpectedStatusCode(res));
|
||||
}
|
||||
|
||||
Ok(match self.extract.unwrap_or_default() {
|
||||
StringOr::T(Extractor::Discard) => "".to_string(),
|
||||
|
|
Loading…
Reference in a new issue