From cb21f24a86a9835fb227fbf5e65c8ac8ac00943c Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Wed, 30 Aug 2023 13:35:12 +0100 Subject: [PATCH] Add clap aliases, address impl, api get fn --- CHANGELOG.md | 3 +++ src/account.rs | 8 ++++++ src/address.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++--- src/lib.rs | 31 +++++++++++++++++++++++ 4 files changed, 105 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da3e0d8..fc2ab42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Address input parameter for fn `process` in `impl`s +- `Address` implementation for subcommands +- API `get` and `get_auth` functions in lib root +- Clap derived subcommand short aliases, using `visible_aliases` - Derive `Debug` for subcommands in `/src` ### Changed diff --git a/src/account.rs b/src/account.rs index b1f7a0a..58349c3 100644 --- a/src/account.rs +++ b/src/account.rs @@ -3,21 +3,25 @@ use clap::Subcommand; #[derive(Debug, Subcommand)] pub enum Account { /// Get information about your account + #[clap(visible_alias = "gi")] GetInfo { /// Email of your omg.lol account email: String, }, /// Get all addresses associated with your account + #[clap(visible_alias = "ga")] GetAddresses { /// Email of your omg.lol account email: String, }, /// Get the name associated with your account + #[clap(visible_alias = "gn")] GetName { /// Email of your omg.lol account email: String, }, /// Update the name associated with your account + #[clap(visible_alias = "sn")] SetName { /// Email of your omg.lol account email: String, @@ -25,11 +29,13 @@ pub enum Account { name: String, }, /// Get all sessions associated with your account + #[clap(visible_alias = "gs")] GetSessions { /// Email of your omg.lol account email: String, }, /// Delete a session from your account + #[clap(visible_alias = "rs")] RemoveSession { /// Email of your omg.lol account email: String, @@ -37,11 +43,13 @@ pub enum Account { session_id: String, }, /// Get settings associated with your account + #[clap(visible_alias = "gset")] GetSettings { /// Email of your omg.lol account email: String, }, /// Update settings associated with your account + #[clap(visible_alias = "sset")] SetSettings { /// Email of your omg.lol account email: String, diff --git a/src/address.rs b/src/address.rs index d750897..de1930a 100644 --- a/src/address.rs +++ b/src/address.rs @@ -6,21 +6,25 @@ use crate::{get, get_auth}; #[derive(Debug, Subcommand)] pub enum Address { /// Get information about the availability of an address + #[clap(visible_aliases = &["a", "av"])] IsAvailable { /// Address to get availability of address: String, }, /// Get the expiration date for an address + #[clap(visible_aliases = &["e", "exp"])] GetExpiry { /// Address to get availability of address: String, }, /// Get limited (public) information about an address (no auth required) + #[clap(visible_aliases = &["pi", "pinfo"])] GetPublicInfo { /// Address to get availability of address: String, }, /// Get comprehensive information about an address + #[clap(visible_aliases = &["i", "info"])] GetInfo { /// Address to get availability of address: String, @@ -30,10 +34,65 @@ pub enum Address { impl Address { pub fn process(&self, api_key: &str) -> Result { match self { - Address::IsAvailable => todo!(), - Address::GetExpiry => todo!(), - Address::GetPublicInfo => todo!(), - Address::GetInfo => todo!(), + Address::IsAvailable { address } => Ok(AddressResponse::IsAvailable(get(&format!( + "address/{address}/availability" + ))?)), + Address::GetExpiry { address } => Ok(AddressResponse::GetExpiry(get(&format!( + "address/{address}/expiration" + ))?)), + Address::GetPublicInfo { address } => Ok(AddressResponse::GetPublicInfo(get( + &format!("address/{address}/info"), + )?)), + Address::GetInfo { address } => Ok(AddressResponse::GetInfo(get_auth( + api_key, + &format!("address/{address}/info"), + )?)), } } } + +structstruck::strike! { + #[strikethrough[allow(dead_code)]] + #[strikethrough[derive(Debug, Deserialize)]] + pub enum AddressResponse { + IsAvailable ( struct { + pub response: pub struct IsAvailableResponse { + pub address: String, + pub available: bool, + }, + }), + GetExpiry ( struct { + pub response: pub struct GetExpiryResponse { + pub message: String, + pub expired: bool, + }, + }), + GetPublicInfo ( struct { + pub response: pub struct GetPublicInfoResponse { + pub address: String, + pub message: String, + pub expiration: struct GetPublicInfoExpiration { + pub expired: bool, + }, + pub verification: struct GetPublicInfoVerification { + pub verified: bool, + }, + }, + }), + GetInfo ( struct { + pub response: pub struct GetInfoResponse { + pub address: String, + pub message: String, + pub expiration: struct GetInfoExpiration { + pub expired: bool, + pub will_expire: bool, + pub relative_time: String, + }, + pub verification: struct GetInfoVerification { + pub verified: bool, + }, + pub owner: String, + } + }), + } +} diff --git a/src/lib.rs b/src/lib.rs index 05de81c..172d76d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,15 +23,35 @@ pub use web::Web; pub mod weblog; pub use weblog::Weblog; +fn get(url: &str) -> Result { + reqwest::blocking::Client::new() + .get(format!("https://api.omg.lol/{}", url)) + .send()? + .error_for_status()? + .json::() +} + +fn get_auth(api_key: &str, url: &str) -> Result { + reqwest::blocking::Client::new() + .get(format!("https://api.omg.lol/{}", url)) + .header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}")) + .send()? + .error_for_status()? + .json::() +} + +// TODO: gate clap derives behind crate feature, not needed for TUI/GUI // TODO: allow content fields for some commands to provide filepaths, using the content of the file instead #[derive(Subcommand)] pub enum Commands { /// Get information and make changes to your account + #[clap(visible_aliases = &["ac"])] Account { #[clap(subcommand)] command: Account, }, /// Get information and make changes to your addresses + #[clap(visible_aliases = &["a"])] Address { #[clap(subcommand)] command: Address, @@ -42,28 +62,34 @@ pub enum Commands { name: String, }, /// Get the address directory, consisting of addresses that have opted-in to be listed + #[clap(visible_aliases = &["dir"])] Directory, /// Adjust the switchboard / DNS records for your omg.lol address + #[clap(visible_aliases = &["d"])] Dns { #[clap(subcommand)] command: Dns, }, /// Manage the email configuration for an omg.lol address + #[clap(visible_aliases = &["e"])] Email { #[clap(subcommand)] command: Email, }, /// Manage your /now page + #[clap(visible_aliases = &["n"])] Now { #[clap(subcommand)] command: Now, }, /// Manage the pastebin for an omg.lol address + #[clap(visible_aliases = &["p"])] Pastebin { #[clap(subcommand)] command: Pastebin, }, /// Manage preferences for omg.lol accounts, addresses and objects + #[clap(visible_aliases = &["pr"])] Preferences { /// Account to change settings for owner: String, @@ -73,6 +99,7 @@ pub enum Commands { value: String, }, /// Manage PURLs (Persistent URLs) for your omg.lol address + #[clap(visible_aliases = &["u"])] Purl { #[clap(subcommand)] command: Purl, @@ -80,6 +107,7 @@ pub enum Commands { /// Get service information about omg.lol Service, /// Manage the statuslog for an omg.lol address + #[clap(visible_aliases = &["s"])] Status { #[clap(subcommand)] command: Status, @@ -90,16 +118,19 @@ pub enum Commands { address: Option, }, /// Manage omg.lol profile themes + #[clap(visible_aliases = &["t"])] Theme { #[clap(subcommand)] command: Theme, }, /// Manage profile page and web stuff for an omg.lol address + #[clap(visible_aliases = &["w"])] Web { #[clap(subcommand)] command: Web, }, /// Manage the weblog for an omg.lol address + #[clap(visible_aliases = &["b"])] Weblog { #[clap(subcommand)] command: Weblog,