diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-04-18 05:18:45 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-04-18 05:18:45 -0400 |
commit | 50ab8aabcec26d07f4b4d9652dbceac1b89a6b22 (patch) | |
tree | eadd12b800d47e721f0941d39a6826dbdb4d6353 /src/api.rs | |
parent | 017f1b04df0701e21e7cb44e3e4f6fc505d95638 (diff) | |
download | rbw-50ab8aabcec26d07f4b4d9652dbceac1b89a6b22.tar.gz rbw-50ab8aabcec26d07f4b4d9652dbceac1b89a6b22.zip |
implement edit command
Diffstat (limited to 'src/api.rs')
-rw-r--r-- | src/api.rs | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -87,6 +87,21 @@ struct CiphersPostReqLogin { totp: Option<String>, } +#[derive(serde::Serialize, Debug)] +struct CiphersPutReq { + #[serde(rename = "type")] + ty: u32, // XXX what are the valid types? + name: String, + notes: Option<String>, + login: CiphersPutReqLogin, +} + +#[derive(serde::Serialize, Debug)] +struct CiphersPutReqLogin { + username: Option<String>, + password: Option<String>, +} + #[derive(serde::Deserialize, Debug)] struct CiphersRes { #[serde(rename = "FolderId")] @@ -311,6 +326,45 @@ impl Client { } } + pub fn edit( + &self, + access_token: &str, + id: &str, + name: &str, + username: Option<&str>, + password: Option<&str>, + notes: Option<&str>, + ) -> Result<()> { + let req = CiphersPutReq { + ty: 1, + name: name.to_string(), + notes: notes.map(std::string::ToString::to_string), + login: CiphersPutReqLogin { + username: username.map(std::string::ToString::to_string), + password: password.map(std::string::ToString::to_string), + }, + }; + let client = reqwest::blocking::Client::new(); + let res = client + .put(&self.api_url(&format!("/ciphers/{}", id))) + .header("Authorization", format!("Bearer {}", access_token)) + .json(&req) + .send() + .context(crate::error::Reqwest)?; + match res.status() { + reqwest::StatusCode::OK => Ok(()), + reqwest::StatusCode::UNAUTHORIZED => { + Err(Error::RequestUnauthorized) + } + _ => { + let code = res.status().as_u16(); + let text = res.text().unwrap(); + eprintln!("error: {}", text); + Err(Error::RequestFailed { status: code }) + } + } + } + pub fn remove(&self, access_token: &str, id: &str) -> Result<()> { let client = reqwest::blocking::Client::new(); let res = client |