use crate::client::AuthenticatedClient;
use crate::client::DeviceClient;
use crate::error::Error;
use serde::Serialize;
use std::collections::HashMap;
use url::Url;
#[derive(Serialize)]
pub(crate) struct SaveSettingsRequest {
pub(crate) set: HashMap<String, String>,
pub(crate) remove: Vec<String>,
}
pub trait SaveAccountSettings {
fn save_account_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
) -> Result<HashMap<String, String>, Error>;
}
pub trait SaveDeviceSettings {
fn save_device_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
) -> Result<HashMap<String, String>, Error>;
}
pub trait SavePodcastSettings {
fn save_podcast_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
podcast: Url,
) -> Result<HashMap<String, String>, Error>;
}
pub trait SaveEpisodeSettings {
fn save_episode_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
podcast: Url,
episode: Url,
) -> Result<HashMap<String, String>, Error>;
}
pub trait GetAccountSettings {
fn get_account_settings(&self) -> Result<HashMap<String, String>, Error>;
}
pub trait GetDeviceSettings {
fn get_device_settings(&self) -> Result<HashMap<String, String>, Error>;
}
pub trait GetPodcastSettings {
fn get_podcast_settings(&self, podcast: Url) -> Result<HashMap<String, String>, Error>;
}
pub trait GetEpisodeSettings {
fn get_episode_settings(
&self,
podcast: Url,
episode: Url,
) -> Result<HashMap<String, String>, Error>;
}
impl SaveAccountSettings for AuthenticatedClient {
fn save_account_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
) -> Result<HashMap<String, String>, Error> {
Ok(self
.post(
&format!(
"https://gpodder.net/api/2/settings/{}/account.json",
self.username
),
&SaveSettingsRequest { set, remove },
)?
.json()?)
}
}
impl SaveAccountSettings for DeviceClient {
fn save_account_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
) -> Result<HashMap<String, String>, Error> {
self.authenticated_client.save_account_settings(set, remove)
}
}
impl SaveDeviceSettings for DeviceClient {
fn save_device_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
) -> Result<HashMap<String, String>, Error> {
Ok(self
.post_with_query(
&format!(
"https://gpodder.net/api/2/settings/{}/device.json",
self.authenticated_client.username
),
&SaveSettingsRequest { set, remove },
&[&("device", self.device_id.as_str())],
)?
.json()?)
}
}
impl SavePodcastSettings for AuthenticatedClient {
fn save_podcast_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
podcast: Url,
) -> Result<HashMap<String, String>, Error> {
Ok(self
.post_with_query(
&format!(
"https://gpodder.net/api/2/settings/{}/podcast.json",
self.username
),
&SaveSettingsRequest { set, remove },
&[&("podcast", podcast.as_str())],
)?
.json()?)
}
}
impl SavePodcastSettings for DeviceClient {
fn save_podcast_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
podcast: Url,
) -> Result<HashMap<String, String>, Error> {
self.authenticated_client
.save_podcast_settings(set, remove, podcast)
}
}
impl SaveEpisodeSettings for AuthenticatedClient {
fn save_episode_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
podcast: Url,
episode: Url,
) -> Result<HashMap<String, String>, Error> {
Ok(self
.post_with_query(
&format!(
"https://gpodder.net/api/2/settings/{}/episode.json",
self.username
),
&SaveSettingsRequest { set, remove },
&[
&("podcast", podcast.as_str()),
&("episode", episode.as_str()),
],
)?
.json()?)
}
}
impl SaveEpisodeSettings for DeviceClient {
fn save_episode_settings(
&self,
set: HashMap<String, String>,
remove: Vec<String>,
podcast: Url,
episode: Url,
) -> Result<HashMap<String, String>, Error> {
self.authenticated_client
.save_episode_settings(set, remove, podcast, episode)
}
}
impl GetAccountSettings for AuthenticatedClient {
fn get_account_settings(&self) -> Result<HashMap<String, String>, Error> {
Ok(self
.get(&format!(
"https://gpodder.net/api/2/settings/{}/account.json",
self.username
))?
.json()?)
}
}
impl GetAccountSettings for DeviceClient {
fn get_account_settings(&self) -> Result<HashMap<String, String>, Error> {
self.authenticated_client.get_account_settings()
}
}
impl GetDeviceSettings for DeviceClient {
fn get_device_settings(&self) -> Result<HashMap<String, String>, Error> {
Ok(self
.get_with_query(
&format!(
"https://gpodder.net/api/2/settings/{}/device.json",
self.authenticated_client.username
),
&[&("device", self.device_id.as_str())],
)?
.json()?)
}
}
impl GetPodcastSettings for AuthenticatedClient {
fn get_podcast_settings(&self, podcast: Url) -> Result<HashMap<String, String>, Error> {
Ok(self
.get_with_query(
&format!(
"https://gpodder.net/api/2/settings/{}/podcast.json",
self.username
),
&[&("podcast", podcast.as_str())],
)?
.json()?)
}
}
impl GetPodcastSettings for DeviceClient {
fn get_podcast_settings(&self, podcast: Url) -> Result<HashMap<String, String>, Error> {
self.authenticated_client.get_podcast_settings(podcast)
}
}
impl GetEpisodeSettings for AuthenticatedClient {
fn get_episode_settings(
&self,
podcast: Url,
episode: Url,
) -> Result<HashMap<String, String>, Error> {
Ok(self
.get_with_query(
&format!(
"https://gpodder.net/api/2/settings/{}/episode.json",
self.username
),
&[
&("podcast", podcast.as_str()),
&("episode", episode.as_str()),
],
)?
.json()?)
}
}
impl GetEpisodeSettings for DeviceClient {
fn get_episode_settings(
&self,
podcast: Url,
episode: Url,
) -> Result<HashMap<String, String>, Error> {
self.authenticated_client
.get_episode_settings(podcast, episode)
}
}