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
- 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

View file

@ -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,

View file

@ -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<AddressResponse, reqwest::Error> {
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,
}
}),
}
}

View file

@ -23,15 +23,35 @@ pub use web::Web;
pub mod 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
#[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<String>,
},
/// 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,