mod bindings;
mod channels;
mod connections;
mod consumers;
mod definitions;
mod exchanges;
mod health;
mod nodes;
mod overview;
mod parameters;
mod permissions;
mod policies;
mod queues;
mod users;
mod vhosts;
use crate::{Error, Result};
pub(crate) fn encode_segment(s: &str) -> String {
url::form_urlencoded::byte_serialize(s.as_bytes())
.collect::<String>()
.replace('+', "%20")
}
pub(crate) async fn handle_response(resp: reqwest::Response) -> Result<String> {
let status = resp.status();
let body = resp.text().await?;
if status.is_success() {
Ok(body)
} else if status.as_u16() == 404 {
Err(Error::NotFound(body))
} else {
Err(Error::Api {
status: status.as_u16(),
reason: body,
})
}
}
pub(crate) fn nf(e: Error, ctx: &str) -> Error {
match e {
Error::NotFound(body) => Error::NotFound(format!("{ctx}: {body}")),
other => other,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encode_segment_encodes_slash() {
assert_eq!(encode_segment("/"), "%2F");
}
#[test]
fn encode_segment_encodes_space_as_percent20_not_plus() {
assert_eq!(encode_segment("my queue"), "my%20queue");
}
#[test]
fn encode_segment_at_sign() {
assert_eq!(encode_segment("rabbit@host"), "rabbit%40host");
}
}