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