use std::sync::Arc;
use crate::core::encoding::encode_segment;
use crate::core::error::Result;
use crate::core::models::mandate::MandateResponse;
use crate::http::client::Inner;
use crate::http::transport::HttpMethod;
#[derive(Debug, Clone)]
pub struct Mandates {
inner: Arc<Inner>,
}
impl Mandates {
pub(crate) fn new(inner: Arc<Inner>) -> Self {
Self { inner }
}
pub async fn retrieve(&self, mandate_id: &str) -> Result<MandateResponse> {
let path = format!("/mandates/{}", encode_segment(mandate_id));
self.inner
.request(HttpMethod::Get, &path, None, &[], None)
.await
}
pub async fn revoke(&self, mandate_id: &str) -> Result<MandateResponse> {
let path = format!("/mandates/revoke/{}", encode_segment(mandate_id));
self.inner
.request(HttpMethod::Post, &path, None, &[], None)
.await
}
}
#[cfg(test)]
mod tests {
use crate::http::mock::{Step, client_with};
#[tokio::test]
async fn revoke_posts_to_the_revoke_path() {
let (client, mock) = client_with(vec![Step::ok(
r#"{"mandate_id":"man/1","status":"revoked"}"#,
)]);
client.mandates().revoke("man/1").await.unwrap();
let seen = mock.seen.lock().unwrap();
assert_eq!(seen[0].method, "POST");
assert_eq!(seen[0].url, "https://api.test/mandates/revoke/man%2F1");
}
}