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
177
178
179
180
181
182
183
184
//! connect-1password is a Rust SDK for 1Password Connect.
//!
//! # High-level features
//!
//! - Based on [`tokio`], [`hyper`] and [`hyper_rustls`] by default.
//! - [`hyper`] can be replaced using the [`HTTPClient`](client::HTTPClient) interface.
//!
//! # Examples
//!
//! ## Create a Login item
//!
//! To create a Login item, make sure to use the Trait [`LoginItem`](models::item::LoginItem), so as to be able to call
//! respective methods (enforced by the interface) on [`ItemBuilder`](models::item::ItemBuilder).
//!
//! ```
//! use connect_1password::{
//! error::Error,
//! client::{Client, HTTPClient},
//! models::{
//! item::{LoginItem, FullItem, ItemBuilder, ItemCategory},
//! },
//! vaults,
//! items,
//! };
//!
//! const SLEEP_DELAY: u64 = 4; // seconds
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Error> {
//! let client = Client::default();
//!
//! let (vaults, _) = vaults::all(&client).await?;
//! assert!(!vaults.is_empty());
//!
//! let item: FullItem = ItemBuilder::new(&vaults[0].id, ItemCategory::Login)
//! .title("Secure server login")
//! .username("Bob")
//! .password("")
//! .build()
//! .unwrap();
//!
//! let (new_item, _) = items::add(&client, item).await?;
//! assert_eq!(new_item.title, "Secure server login");
//!
//! // Just as a clean up measure, we remove the item created in the this example
//! tokio::time::sleep(std::time::Duration::new(SLEEP_DELAY, 0)).await;
//!
//! items::remove(&client, &vaults[0].id, &new_item.id)
//! .await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Create an API Credential item
//!
//! This is ideally used for programmatic access, and potentially the main interface required for
//! this entire API wrapper.
//!
//! In the example below, since we have not provided a specific API key value, one is generated for
//! us by the Connect API.
//!
//! ```
//! use connect_1password::{
//! error::Error,
//! client::{Client, HTTPClient},
//! models::{
//! item::{ApiCredentialItem, FullItem, ItemBuilder, ItemCategory, FieldObject},
//! },
//! vaults,
//! items,
//! };
//!
//! const SLEEP_DELAY: u64 = 4; // seconds
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Error> {
//! let client = Client::default();
//!
//! let (vaults, _) = vaults::all(&client).await?;
//! assert!(!vaults.is_empty());
//!
//! let item: FullItem = ItemBuilder::new(&vaults[0].id, ItemCategory::ApiCredential)
//! .api_key("", "Dell XYZ")
//! .build()
//! .unwrap();
//!
//! let client = Client::default();
//! let (new_item, _) = items::add(&client, item).await?;
//! assert_eq!(new_item.title, "Dell XYZ");
//!
//! tokio::time::sleep(std::time::Duration::new(SLEEP_DELAY, 0)).await;
//!
//! let (item, _) = items::get(&client, &vaults[0].id, &new_item.id).await?;
//! let fields: Vec<_> = item.fields.into_iter().filter(|r| r.value.is_some()).collect();
//! assert_eq!(fields.len(), 1);
//! dbg!(&fields);
//!
//! let default_value = "".to_string();
//! let api_value = fields[0].value.as_ref().unwrap_or(&default_value);
//! let field_type = fields[0].r#type.as_ref().unwrap_or(&default_value);
//! assert_eq!(field_type, "CONCEALED");
//! assert!(!api_value.is_empty());
//!
//! // Just as a clean up measure, we remove the item created in the this example
//! tokio::time::sleep(std::time::Duration::new(SLEEP_DELAY, 0)).await;
//!
//! items::remove(&client, &vaults[0].id, &new_item.id)
//! .await?;
//!
//! Ok(())
//! }
//! ```
//!
//! However, if we provide a specific key, this is the value persisted into 1Password.
//!
//! ```
//! use connect_1password::{
//! error::Error,
//! client::{Client, HTTPClient},
//! models::{
//! item::{ApiCredentialItem, FullItem, ItemBuilder, ItemCategory, FieldObject},
//! },
//! vaults,
//! items,
//! };
//!
//! const SLEEP_DELAY: u64 = 4; // seconds
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Error> {
//! let client = Client::default();
//!
//! let (vaults, _) = vaults::all(&client).await?;
//! assert!(!vaults.is_empty());
//!
//! let item: FullItem = ItemBuilder::new(&vaults[0].id, ItemCategory::ApiCredential)
//! .api_key("smelly-socks", "Dell XYZ")
//! .build()
//! .unwrap();
//!
//! let (new_item, _) = items::add(&client, item).await?;
//! assert_eq!(new_item.title, "Dell XYZ");
//!
//! tokio::time::sleep(std::time::Duration::new(SLEEP_DELAY, 0)).await;
//!
//! let client = Client::default();
//! let (item, _) = items::get(&client, &vaults[0].id, &new_item.id).await?;
//! let fields: Vec<_> = item.fields.into_iter().filter(|r| r.value.is_some()).collect();
//! assert_eq!(fields.len(), 1);
//! dbg!(&fields);
//!
//! let default_value = "".to_string();
//! let api_value = fields[0].value.as_ref().unwrap_or(&default_value);
//! let field_type = fields[0].r#type.as_ref().unwrap_or(&default_value);
//! assert_eq!(field_type, "CONCEALED");
//! assert_eq!(api_value, "smelly-socks");
//!
//! // Just as a clean up measure, we remove the item created in the this example
//! tokio::time::sleep(std::time::Duration::new(SLEEP_DELAY, 0)).await;
//!
//! items::remove(&client, &vaults[0].id, &new_item.id)
//! .await?;
//!
//! Ok(())
//! }
//! ```