digitalocean_api/api/
domain.rs1use super::{ApiLinks, ApiMeta};
2use super::{HasPagination, HasResponse, HasValue};
3use crate::method::{Create, Delete, Get, List};
4use crate::request::DomainRequest;
5use crate::request::Request;
6use crate::{ROOT_URL, STATIC_URL_ERROR};
7use getset::{Getters, Setters};
8use serde::Deserialize;
9use serde::Serialize;
10use std::fmt::Display;
11use std::net::IpAddr;
12use url::Url;
13
14const DOMAINS_SEGMENT: &str = "domains";
15
16#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
21#[get = "pub"]
22pub struct Domain {
23 name: String,
26
27 ttl: Option<usize>,
31
32 zone_file: Option<String>,
38}
39
40impl Domain {
41 pub fn create<N, I>(name: N, ip_address: I) -> DomainRequest<Create, Domain>
43 where
44 N: AsRef<str> + Serialize + Display,
45 I: Into<IpAddr> + Serialize + Display,
46 {
47 let mut url = ROOT_URL.clone();
48 url.path_segments_mut()
49 .expect(STATIC_URL_ERROR)
50 .push(DOMAINS_SEGMENT);
51
52 let mut req = Request::new(url);
53 req.set_body(json!({
54 "name": name,
55 "ip_address": ip_address,
56 }));
57 req
58 }
59
60 pub fn list() -> DomainRequest<List, Vec<Domain>> {
62 let mut url = ROOT_URL.clone();
63 url.path_segments_mut()
64 .expect(STATIC_URL_ERROR)
65 .push(DOMAINS_SEGMENT);
66
67 Request::new(url)
68 }
69
70 pub fn get<N: AsRef<str> + Display>(name: N) -> DomainRequest<Get, Domain> {
72 let mut url = ROOT_URL.clone();
73 url.path_segments_mut()
74 .expect(STATIC_URL_ERROR)
75 .push(DOMAINS_SEGMENT)
76 .push(name.as_ref());
77
78 Request::new(url)
79 }
80
81 pub fn delete<N: AsRef<str> + Display>(name: N) -> DomainRequest<Delete, ()> {
83 let mut url = ROOT_URL.clone();
84 url.path_segments_mut()
85 .expect(STATIC_URL_ERROR)
86 .push(DOMAINS_SEGMENT)
87 .push(name.as_ref());
88
89 Request::new(url)
90 }
91}
92
93#[derive(Deserialize, Serialize, Debug, Clone)]
95pub struct DomainResponse {
96 domain: Domain,
97}
98
99impl HasResponse for Domain {
100 type Response = DomainResponse;
101}
102
103impl HasValue for DomainResponse {
104 type Value = Domain;
105
106 fn value(self) -> Domain {
107 self.domain
108 }
109}
110
111#[derive(Deserialize, Serialize, Debug, Clone)]
113pub struct DomainListResponse {
114 domains: Vec<Domain>,
115 links: ApiLinks,
116 meta: ApiMeta,
117}
118
119impl HasResponse for Vec<Domain> {
120 type Response = DomainListResponse;
121}
122
123impl HasPagination for DomainListResponse {
124 fn next_page(&self) -> Option<Url> {
125 self.links.next()
126 }
127}
128
129impl HasValue for DomainListResponse {
130 type Value = Vec<Domain>;
131
132 fn value(self) -> Vec<Domain> {
133 self.domains
134 }
135}