use super::{ApiLinks, ApiMeta};
use super::{HasPagination, HasResponse, HasValue};
use crate::method::{Create, Delete, Get, List};
use crate::request::DomainRequest;
use crate::request::Request;
use crate::{ROOT_URL, STATIC_URL_ERROR};
use getset::{Getters, Setters};
use serde::Serialize;
use std::fmt::Display;
use std::net::IpAddr;
use url::Url;
const DOMAINS_SEGMENT: &str = "domains";
#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
#[get = "pub"]
pub struct Domain {
name: String,
ttl: Option<usize>,
zone_file: Option<String>
}
impl Domain {
pub fn create<N, I>(name: N, ip_address: I) -> DomainRequest<Create, Domain>
where N: AsRef<str> + Serialize + Display,
I: Into<IpAddr> + Serialize + Display {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DOMAINS_SEGMENT);
let mut req = Request::new(url);
req.set_body(json!({
"name": name,
"ip_address": ip_address,
}));
req
}
pub fn list() -> DomainRequest<List, Vec<Domain>> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DOMAINS_SEGMENT);
Request::new(url)
}
pub fn get<N: AsRef<str> + Display>(name: N) -> DomainRequest<Get, Domain> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DOMAINS_SEGMENT)
.push(name.as_ref());
Request::new(url)
}
pub fn delete<N: AsRef<str> + Display>(name: N) -> DomainRequest<Delete, ()> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DOMAINS_SEGMENT)
.push(name.as_ref());
Request::new(url)
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct DomainResponse {
domain: Domain
}
impl HasResponse for Domain {
type Response = DomainResponse;
}
impl HasValue for DomainResponse {
type Value = Domain;
fn value(self) -> Domain {
self.domain
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct DomainListResponse {
domains: Vec<Domain>,
links: ApiLinks,
meta: ApiMeta
}
impl HasResponse for Vec<Domain> {
type Response = DomainListResponse;
}
impl HasPagination for DomainListResponse {
fn next_page(&self) -> Option<Url> {
self.links.next()
}
}
impl HasValue for DomainListResponse {
type Value = Vec<Domain>;
fn value(self) -> Vec<Domain> {
self.domains
}
}