Add clap aliases, address impl, api get fn

This commit is contained in:
Aadi Desai 2023-08-30 13:35:12 +01:00
parent e2c5de4a0a
commit cb21f24a86
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
4 changed files with 105 additions and 4 deletions

View file

@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Address input parameter for fn `process` in `impl`s - 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` - Derive `Debug` for subcommands in `/src`
### Changed ### Changed

View file

@ -3,21 +3,25 @@ use clap::Subcommand;
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum Account { pub enum Account {
/// Get information about your account /// Get information about your account
#[clap(visible_alias = "gi")]
GetInfo { GetInfo {
/// Email of your omg.lol account /// Email of your omg.lol account
email: String, email: String,
}, },
/// Get all addresses associated with your account /// Get all addresses associated with your account
#[clap(visible_alias = "ga")]
GetAddresses { GetAddresses {
/// Email of your omg.lol account /// Email of your omg.lol account
email: String, email: String,
}, },
/// Get the name associated with your account /// Get the name associated with your account
#[clap(visible_alias = "gn")]
GetName { GetName {
/// Email of your omg.lol account /// Email of your omg.lol account
email: String, email: String,
}, },
/// Update the name associated with your account /// Update the name associated with your account
#[clap(visible_alias = "sn")]
SetName { SetName {
/// Email of your omg.lol account /// Email of your omg.lol account
email: String, email: String,
@ -25,11 +29,13 @@ pub enum Account {
name: String, name: String,
}, },
/// Get all sessions associated with your account /// Get all sessions associated with your account
#[clap(visible_alias = "gs")]
GetSessions { GetSessions {
/// Email of your omg.lol account /// Email of your omg.lol account
email: String, email: String,
}, },
/// Delete a session from your account /// Delete a session from your account
#[clap(visible_alias = "rs")]
RemoveSession { RemoveSession {
/// Email of your omg.lol account /// Email of your omg.lol account
email: String, email: String,
@ -37,11 +43,13 @@ pub enum Account {
session_id: String, session_id: String,
}, },
/// Get settings associated with your account /// Get settings associated with your account
#[clap(visible_alias = "gset")]
GetSettings { GetSettings {
/// Email of your omg.lol account /// Email of your omg.lol account
email: String, email: String,
}, },
/// Update settings associated with your account /// Update settings associated with your account
#[clap(visible_alias = "sset")]
SetSettings { SetSettings {
/// Email of your omg.lol account /// Email of your omg.lol account
email: String, email: String,

View file

@ -6,21 +6,25 @@ use crate::{get, get_auth};
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum Address { pub enum Address {
/// Get information about the availability of an address /// Get information about the availability of an address
#[clap(visible_aliases = &["a", "av"])]
IsAvailable { IsAvailable {
/// Address to get availability of /// Address to get availability of
address: String, address: String,
}, },
/// Get the expiration date for an address /// Get the expiration date for an address
#[clap(visible_aliases = &["e", "exp"])]
GetExpiry { GetExpiry {
/// Address to get availability of /// Address to get availability of
address: String, address: String,
}, },
/// Get limited (public) information about an address (no auth required) /// Get limited (public) information about an address (no auth required)
#[clap(visible_aliases = &["pi", "pinfo"])]
GetPublicInfo { GetPublicInfo {
/// Address to get availability of /// Address to get availability of
address: String, address: String,
}, },
/// Get comprehensive information about an address /// Get comprehensive information about an address
#[clap(visible_aliases = &["i", "info"])]
GetInfo { GetInfo {
/// Address to get availability of /// Address to get availability of
address: String, address: String,
@ -30,10 +34,65 @@ pub enum Address {
impl Address { impl Address {
pub fn process(&self, api_key: &str) -> Result<AddressResponse, reqwest::Error> { pub fn process(&self, api_key: &str) -> Result<AddressResponse, reqwest::Error> {
match self { match self {
Address::IsAvailable => todo!(), Address::IsAvailable { address } => Ok(AddressResponse::IsAvailable(get(&format!(
Address::GetExpiry => todo!(), "address/{address}/availability"
Address::GetPublicInfo => todo!(), ))?)),
Address::GetInfo => todo!(), 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,
}
}),
}
}

View file

@ -23,15 +23,35 @@ pub use web::Web;
pub mod weblog; pub mod weblog;
pub use weblog::Weblog; pub use weblog::Weblog;
fn get<T: serde::de::DeserializeOwned>(url: &str) -> Result<T, reqwest::Error> {
reqwest::blocking::Client::new()
.get(format!("https://api.omg.lol/{}", url))
.send()?
.error_for_status()?
.json::<T>()
}
fn get_auth<T: serde::de::DeserializeOwned>(api_key: &str, url: &str) -> Result<T, reqwest::Error> {
reqwest::blocking::Client::new()
.get(format!("https://api.omg.lol/{}", url))
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"))
.send()?
.error_for_status()?
.json::<T>()
}
// 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 // TODO: allow content fields for some commands to provide filepaths, using the content of the file instead
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum Commands { pub enum Commands {
/// Get information and make changes to your account /// Get information and make changes to your account
#[clap(visible_aliases = &["ac"])]
Account { Account {
#[clap(subcommand)] #[clap(subcommand)]
command: Account, command: Account,
}, },
/// Get information and make changes to your addresses /// Get information and make changes to your addresses
#[clap(visible_aliases = &["a"])]
Address { Address {
#[clap(subcommand)] #[clap(subcommand)]
command: Address, command: Address,
@ -42,28 +62,34 @@ pub enum Commands {
name: String, name: String,
}, },
/// Get the address directory, consisting of addresses that have opted-in to be listed /// Get the address directory, consisting of addresses that have opted-in to be listed
#[clap(visible_aliases = &["dir"])]
Directory, Directory,
/// Adjust the switchboard / DNS records for your omg.lol address /// Adjust the switchboard / DNS records for your omg.lol address
#[clap(visible_aliases = &["d"])]
Dns { Dns {
#[clap(subcommand)] #[clap(subcommand)]
command: Dns, command: Dns,
}, },
/// Manage the email configuration for an omg.lol address /// Manage the email configuration for an omg.lol address
#[clap(visible_aliases = &["e"])]
Email { Email {
#[clap(subcommand)] #[clap(subcommand)]
command: Email, command: Email,
}, },
/// Manage your /now page /// Manage your /now page
#[clap(visible_aliases = &["n"])]
Now { Now {
#[clap(subcommand)] #[clap(subcommand)]
command: Now, command: Now,
}, },
/// Manage the pastebin for an omg.lol address /// Manage the pastebin for an omg.lol address
#[clap(visible_aliases = &["p"])]
Pastebin { Pastebin {
#[clap(subcommand)] #[clap(subcommand)]
command: Pastebin, command: Pastebin,
}, },
/// Manage preferences for omg.lol accounts, addresses and objects /// Manage preferences for omg.lol accounts, addresses and objects
#[clap(visible_aliases = &["pr"])]
Preferences { Preferences {
/// Account to change settings for /// Account to change settings for
owner: String, owner: String,
@ -73,6 +99,7 @@ pub enum Commands {
value: String, value: String,
}, },
/// Manage PURLs (Persistent URLs) for your omg.lol address /// Manage PURLs (Persistent URLs) for your omg.lol address
#[clap(visible_aliases = &["u"])]
Purl { Purl {
#[clap(subcommand)] #[clap(subcommand)]
command: Purl, command: Purl,
@ -80,6 +107,7 @@ pub enum Commands {
/// Get service information about omg.lol /// Get service information about omg.lol
Service, Service,
/// Manage the statuslog for an omg.lol address /// Manage the statuslog for an omg.lol address
#[clap(visible_aliases = &["s"])]
Status { Status {
#[clap(subcommand)] #[clap(subcommand)]
command: Status, command: Status,
@ -90,16 +118,19 @@ pub enum Commands {
address: Option<String>, address: Option<String>,
}, },
/// Manage omg.lol profile themes /// Manage omg.lol profile themes
#[clap(visible_aliases = &["t"])]
Theme { Theme {
#[clap(subcommand)] #[clap(subcommand)]
command: Theme, command: Theme,
}, },
/// Manage profile page and web stuff for an omg.lol address /// Manage profile page and web stuff for an omg.lol address
#[clap(visible_aliases = &["w"])]
Web { Web {
#[clap(subcommand)] #[clap(subcommand)]
command: Web, command: Web,
}, },
/// Manage the weblog for an omg.lol address /// Manage the weblog for an omg.lol address
#[clap(visible_aliases = &["b"])]
Weblog { Weblog {
#[clap(subcommand)] #[clap(subcommand)]
command: Weblog, command: Weblog,