use std::fmt::Debug;
#[cfg(feature = "logging")]
use log::{Level, debug, log_enabled};
use oauth10a::client::{ClientError, RestClient};
#[cfg(feature = "jsonschemas")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::Client;
pub const TAG_APPLICATION: &str = "for:applications";
pub const TAG_HDS: &str = "certification:hds";
#[cfg_attr(feature = "jsonschemas", derive(JsonSchema))]
#[derive(Serialize, Deserialize, PartialEq, PartialOrd, Clone, Debug)]
pub struct Zone {
#[serde(rename = "id")]
pub id: Uuid,
#[serde(rename = "city")]
pub city: String,
#[serde(rename = "country")]
pub country: String,
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "countryCode")]
pub coutry_code: String,
#[serde(rename = "lat")]
pub latitude: f64,
#[serde(rename = "lon")]
pub longitude: f64,
#[serde(rename = "tags")]
pub tags: Vec<String>,
}
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("failed to list available zones, {0}")]
List(ClientError),
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub async fn list(client: &Client) -> Result<Vec<Zone>, Error> {
let path = format!("{}/v4/products/zones", client.endpoint);
#[cfg(feature = "logging")]
if log_enabled!(Level::Debug) {
debug!("execute a request to list zones, path: '{}'", &path);
}
client.get(&path).await.map_err(Error::List)
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub async fn applications(client: &Client) -> Result<Vec<Zone>, Error> {
Ok(list(client)
.await?
.iter()
.filter(|zone| zone.tags.contains(&TAG_APPLICATION.to_string()))
.map(ToOwned::to_owned)
.collect())
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub async fn hds(client: &Client) -> Result<Vec<Zone>, Error> {
Ok(list(client)
.await?
.iter()
.filter(|zone| zone.tags.contains(&TAG_APPLICATION.to_string()))
.filter(|zone| zone.tags.contains(&TAG_HDS.to_string()))
.map(ToOwned::to_owned)
.collect())
}