use serde_json::json;
use crate::model::*;
use crate::RecurlyClient;
pub struct CancelSubscriptionRequest<'a> {
pub(crate) http_client: &'a RecurlyClient,
pub subscription_id: String,
pub timeframe: Option<String>,
}
impl<'a> CancelSubscriptionRequest<'a> {
pub async fn send(self) -> anyhow::Result<Subscription> {
let mut r = self
.http_client
.client
.put(
&format!(
"/subscriptions/{subscription_id}/cancel", subscription_id = self
.subscription_id
),
);
if let Some(ref unwrapped) = self.timeframe {
r = r.push_json(json!({ "timeframe" : unwrapped }));
}
r = self.http_client.authenticate(r);
let res = r.send().await.unwrap().error_for_status();
match res {
Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
Err(res) => {
let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
Err(anyhow::anyhow!("{:?}", text))
}
}
}
pub fn timeframe(mut self, timeframe: &str) -> Self {
self.timeframe = Some(timeframe.to_owned());
self
}
}