digitalocean_api/api/
certificate.rs1use super::{ApiLinks, ApiMeta};
2use super::{HasPagination, HasResponse, HasValue};
3use crate::method::{Create, Delete, Get, List};
4use crate::request::CertificateRequest;
5use crate::request::Request;
6use crate::{ROOT_URL, STATIC_URL_ERROR};
7use chrono::{DateTime, Utc};
8use getset::{Getters, Setters};
9use serde::Deserialize;
10use serde::Serialize;
11use std::fmt::Display;
12use url::Url;
13
14const CERTIFICATES_SEGMENT: &str = "certificates";
15
16#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
22#[get = "pub"]
23pub struct Certificate {
24 id: String,
26
27 name: String,
29
30 not_after: DateTime<Utc>,
33
34 sha1_fingerprint: String,
37
38 created_at: DateTime<Utc>,
41}
42
43impl Certificate {
44 pub fn create<S>(
46 name: S,
47 private_key: S,
48 leaf_certificate: S,
49 ) -> CertificateRequest<Create, Certificate>
50 where
51 S: AsRef<str> + Serialize + Display,
52 {
53 let mut url = ROOT_URL.clone();
54 url.path_segments_mut()
55 .expect(STATIC_URL_ERROR)
56 .push(CERTIFICATES_SEGMENT);
57
58 let mut req = Request::new(url);
59
60 req.set_body(json!({
61 "name": name,
62 "private_key": private_key,
63 "leaf_certificate": leaf_certificate,
64 }));
65
66 req
67 }
68
69 pub fn list() -> CertificateRequest<List, Vec<Certificate>> {
71 let mut url = ROOT_URL.clone();
72 url.path_segments_mut()
73 .expect(STATIC_URL_ERROR)
74 .push(CERTIFICATES_SEGMENT);
75
76 Request::new(url)
77 }
78
79 pub fn get<N: AsRef<str> + Display>(id: N) -> CertificateRequest<Get, Certificate> {
81 let mut url = ROOT_URL.clone();
82 url.path_segments_mut()
83 .expect(STATIC_URL_ERROR)
84 .push(CERTIFICATES_SEGMENT)
85 .push(id.as_ref());
86
87 Request::new(url)
88 }
89
90 pub fn delete<N: AsRef<str> + Display>(id: N) -> CertificateRequest<Delete, ()> {
92 let mut url = ROOT_URL.clone();
93 url.path_segments_mut()
94 .expect(STATIC_URL_ERROR)
95 .push(CERTIFICATES_SEGMENT)
96 .push(id.as_ref());
97
98 Request::new(url)
99 }
100}
101
102impl CertificateRequest<Create, Certificate> {
103 pub fn certificate_chain<S: AsRef<str> + Serialize + Display>(mut self, val: S) -> Self {
108 self.body_mut()["certificate_chain"] = json!(val);
109 self
110 }
111}
112
113#[derive(Deserialize, Serialize, Debug, Clone)]
115pub struct CertificateResponse {
116 certificate: Certificate,
117}
118
119impl HasResponse for Certificate {
120 type Response = CertificateResponse;
121}
122
123impl HasValue for CertificateResponse {
124 type Value = Certificate;
125
126 fn value(self) -> Certificate {
127 self.certificate
128 }
129}
130
131#[derive(Deserialize, Serialize, Debug, Clone)]
133pub struct CertificateListResponse {
134 certificates: Vec<Certificate>,
135 links: ApiLinks,
136 meta: ApiMeta,
137}
138
139impl HasResponse for Vec<Certificate> {
140 type Response = CertificateListResponse;
141}
142
143impl HasPagination for CertificateListResponse {
144 fn next_page(&self) -> Option<Url> {
145 self.links.next()
146 }
147}
148
149impl HasValue for CertificateListResponse {
150 type Value = Vec<Certificate>;
151
152 fn value(self) -> Vec<Certificate> {
153 self.certificates
154 }
155}