digitalocean/api/
ssh_key.rs1use super::{ApiLinks, ApiMeta};
2use super::{HasPagination, HasResponse, HasValue};
3use crate::method::{Create, Delete, Get, List, Update};
4use crate::request::Request;
5use crate::request::SshKeyRequest;
6use crate::{ROOT_URL, STATIC_URL_ERROR};
7use getset::{Getters, Setters};
8use serde::Serialize;
9use std::fmt::Display;
10use url::Url;
11
12const ACCOUNT_SEGMENT: &str = "account";
13const KEYS_SEGMENT: &str = "keys";
14
15#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
21#[get = "pub"]
22pub struct SshKey {
23 id: usize,
30
31 fingerprint: String,
35
36 public_key: String,
40
41 name: String,
44}
45
46impl SshKey {
47 pub fn create<N>(name: N, public_key: N) -> SshKeyRequest<Create, SshKey>
49 where N: AsRef<str> + Serialize + Display, {
50 let mut url = ROOT_URL.clone();
51 url.path_segments_mut()
52 .expect(STATIC_URL_ERROR)
53 .push(ACCOUNT_SEGMENT)
54 .push(KEYS_SEGMENT);
55
56 let mut req = Request::new(url);
57 req.set_body(json!({
58 "name": name,
59 "public_key": public_key,
60 }));
61 req
62 }
63
64 pub fn list() -> SshKeyRequest<List, Vec<SshKey>> {
66 let mut url = ROOT_URL.clone();
67 url.path_segments_mut()
68 .expect(STATIC_URL_ERROR)
69 .push(ACCOUNT_SEGMENT)
70 .push(KEYS_SEGMENT);
71
72 Request::new(url)
73 }
74
75 pub fn get<S: Serialize + Display>(id: S) -> SshKeyRequest<Get, SshKey> {
77 let mut url = ROOT_URL.clone();
78 url.path_segments_mut()
79 .expect(STATIC_URL_ERROR)
80 .push(ACCOUNT_SEGMENT)
81 .push(KEYS_SEGMENT)
82 .push(&format!("{}", id));
83
84 Request::new(url)
85 }
86
87 pub fn update<S: Serialize + Display>(id: S) -> SshKeyRequest<Update, SshKey> {
89 let mut url = ROOT_URL.clone();
90 url.path_segments_mut()
91 .expect(STATIC_URL_ERROR)
92 .push(ACCOUNT_SEGMENT)
93 .push(KEYS_SEGMENT)
94 .push(&format!("{}", id));
95
96 Request::new(url)
97 }
98
99 pub fn delete<S: Serialize + Display>(id: S) -> SshKeyRequest<Delete, ()> {
101 let mut url = ROOT_URL.clone();
102 url.path_segments_mut()
103 .expect(STATIC_URL_ERROR)
104 .push(ACCOUNT_SEGMENT)
105 .push(KEYS_SEGMENT)
106 .push(&format!("{}", id));
107
108 Request::new(url)
109 }
110}
111
112impl SshKeyRequest<Update, SshKey> {
113 pub fn name<S: AsRef<str> + Display + Serialize>(mut self, val: S) -> Self {
117 self.body_mut()["name"] = json!(val);
118 self
119 }
120}
121
122#[derive(Deserialize, Serialize, Debug, Clone)]
124pub struct SshKeyListResponse {
125 ssh_keys: Vec<SshKey>,
126 links: ApiLinks,
127 meta: ApiMeta
128}
129
130impl HasResponse for Vec<SshKey> {
131 type Response = SshKeyListResponse;
132}
133
134impl HasPagination for SshKeyListResponse {
135 fn next_page(&self) -> Option<Url> {
136 self.links.next()
137 }
138}
139
140impl HasValue for SshKeyListResponse {
141 type Value = Vec<SshKey>;
142
143 fn value(self) -> Vec<SshKey> {
144 self.ssh_keys
145 }
146}
147
148#[derive(Deserialize, Serialize, Debug, Clone)]
150pub struct SshKeyResponse {
151 ssh_key: SshKey
152}
153
154impl HasResponse for SshKey {
155 type Response = SshKeyResponse;
156}
157
158impl HasValue for SshKeyResponse {
159 type Value = SshKey;
160
161 fn value(self) -> SshKey {
162 self.ssh_key
163 }
164}