use crate::error::Error;
use crate::seed::client::SeedClient;
use crate::seed::config::CallOptions;
use crate::seed::models::{PairCreate, PairCreateResponse, PairStatus};
pub struct PairResource<'c> {
pub(crate) client: &'c SeedClient,
}
impl<'c> PairResource<'c> {
pub async fn status(&self) -> Result<PairStatus, Error> {
self.client.request_get("/pair/status").await
}
pub async fn status_with(&self, opts: CallOptions) -> Result<PairStatus, Error> {
self.client.request_get_opts("/pair/status", &opts).await
}
pub async fn create(&self, req: PairCreate) -> Result<PairCreateResponse, Error> {
self.client.request_post("/pair", &req, false).await
}
pub async fn create_with(
&self,
req: PairCreate,
opts: CallOptions,
) -> Result<PairCreateResponse, Error> {
self.client
.request_post_opts("/pair", &req, false, &opts)
.await
}
pub async fn delete(&self, client_name: &str) -> Result<(), Error> {
let encoded = urlencoding_light(client_name);
let path = format!("/pair/{encoded}");
let _: serde_json::Value = self.client.request_delete(&path).await?;
Ok(())
}
}
fn urlencoding_light(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for ch in s.chars() {
match ch {
'%' => out.push_str("%25"),
'/' => out.push_str("%2F"),
'?' => out.push_str("%3F"),
'#' => out.push_str("%23"),
' ' => out.push_str("%20"),
c if c.is_ascii_alphanumeric() => out.push(c),
c if matches!(c, '-' | '_' | '.' | '~') => out.push(c),
c => {
let mut buf = [0u8; 4];
for byte in c.encode_utf8(&mut buf).as_bytes() {
out.push_str(&format!("%{byte:02X}"));
}
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn urlencoding_light_handles_safe_chars() {
assert_eq!(urlencoding_light("abc-123_XYZ.~"), "abc-123_XYZ.~");
}
#[test]
fn urlencoding_light_encodes_special() {
assert_eq!(urlencoding_light("foo/bar"), "foo%2Fbar");
assert_eq!(urlencoding_light("a b"), "a%20b");
}
}