use serde_json::{Value, json};
use crate::authentification::Credentials;
use crate::error::Result;
use crate::http::{client, ok};
pub async fn subscription(credentials: &Credentials) -> Result<bool> {
let response = client()
.get(format!("{}/newsletter/subscription", crate::get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(json["subscribed"].as_bool().unwrap_or(false))
}
pub async fn set_subscription(credentials: &Credentials, subscribed: bool) -> Result<bool> {
let response = client()
.put(format!("{}/newsletter/subscription", crate::get_url()))
.header("Authorization", format!("Token {}", credentials.token))
.json(&json!({ "subscribed": subscribed }))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(json["subscribed"].as_bool().unwrap_or(false))
}