use reqwest::header::HeaderMap;
use reqwest::{RequestBuilder, Response};
use crate::error::Error;
pub(crate) struct RawResponse {
pub(crate) status: u16,
pub(crate) headers: HeaderMap,
pub(crate) body: Vec<u8>,
}
pub(crate) async fn send(request: RequestBuilder) -> Result<RawResponse, Error> {
let response: Response = request
.send()
.await
.map_err(|error| Error::transport(error.to_string()))?;
let status = response.status().as_u16();
let headers = response.headers().clone();
let body = response
.bytes()
.await
.map_err(|error| Error::transport(error.to_string()))?
.to_vec();
Ok(RawResponse {
status,
headers,
body,
})
}
pub(crate) fn cursor_from_url(next: &str) -> Option<String> {
let url = url::Url::parse(next).ok()?;
url.query_pairs()
.find(|(key, _)| key == "cursor")
.map(|(_, value)| value.into_owned())
}
pub(crate) fn encode_segment(segment: &str) -> String {
let mut encoded = String::with_capacity(segment.len());
for byte in segment.bytes() {
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') {
encoded.push(char::from(byte));
} else {
encoded.push_str(&format!("%{byte:02X}"));
}
}
encoded
}
#[cfg(test)]
mod tests {
use super::encode_segment;
#[test]
fn keeps_unreserved_and_encodes_the_rest() {
assert_eq!(encode_segment("invoice.voided"), "invoice.voided");
assert_eq!(encode_segment("a-b_c~d"), "a-b_c~d");
assert_eq!(encode_segment("a/b?c d"), "a%2Fb%3Fc%20d");
assert_eq!(encode_segment("é"), "%C3%A9");
}
}