Updated REST principal API

This commit is contained in:
mdecimus
2024-02-18 16:48:44 +01:00
parent afe10e6d81
commit 8027f135bc
13 changed files with 224 additions and 150 deletions

View File

@@ -61,7 +61,7 @@ impl AccountCommands {
..Default::default()
};
let account_id = client
.http_request::<u32, _>(Method::POST, "/admin/principal", Some(principal))
.http_request::<u32, _>(Method::POST, "/api/principal", Some(principal))
.await;
eprintln!("Successfully created account {name:?} with id {account_id}.");
}
@@ -131,7 +131,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(changes),
)
.await;
@@ -144,7 +144,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
addresses
.into_iter()
@@ -164,7 +164,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
addresses
.into_iter()
@@ -184,7 +184,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
member_of
.into_iter()
@@ -204,7 +204,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
member_of
.into_iter()
@@ -224,7 +224,7 @@ impl AccountCommands {
client
.http_request::<Value, String>(
Method::DELETE,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
None,
)
.await;
@@ -233,9 +233,13 @@ impl AccountCommands {
AccountCommands::Display { name } => {
client.display_principal(&name).await;
}
AccountCommands::List { from, limit } => {
AccountCommands::List {
filter,
limit,
page,
} => {
client
.list_principals("individual", "Account", from, limit)
.list_principals("individual", "Account", filter, page, limit)
.await;
}
}
@@ -245,11 +249,7 @@ impl AccountCommands {
impl Client {
pub async fn display_principal(&self, name: &str) {
let principal = self
.http_request::<Principal, String>(
Method::GET,
&format!("/admin/principal/{name}"),
None,
)
.http_request::<Principal, String>(Method::GET, &format!("/api/principal/{name}"), None)
.await;
let mut table = Table::new();
if let Some(name) = principal.name {
@@ -318,31 +318,35 @@ impl Client {
&self,
record_type: &str,
record_name: &str,
from: Option<String>,
filter: Option<String>,
page: Option<usize>,
limit: Option<usize>,
) {
let mut query = form_urlencoded::Serializer::new("/admin/principal?".to_string());
let mut query = form_urlencoded::Serializer::new("/api/principal?".to_string());
query.append_pair("type", record_type);
if let Some(from) = &from {
query.append_pair("from", from);
if let Some(filter) = &filter {
query.append_pair("filter", filter);
}
if let Some(limit) = limit {
query.append_pair("limit", &limit.to_string());
}
if let Some(page) = page {
query.append_pair("page", &page.to_string());
}
let results = self
.http_request::<Vec<String>, String>(Method::GET, &query.finish(), None)
.http_request::<ListResponse, String>(Method::GET, &query.finish(), None)
.await;
if !results.is_empty() {
if !results.items.is_empty() {
let mut table = Table::new();
table.add_row(Row::new(vec![
Cell::new(&format!("{record_name} Name")).with_style(Attr::Bold)
]));
for domain in &results {
table.add_row(Row::new(vec![Cell::new(domain)]));
for item in &results.items {
table.add_row(Row::new(vec![Cell::new(item)]));
}
eprintln!();
@@ -352,13 +356,19 @@ impl Client {
eprintln!(
"\n\n{} {}{} found.\n",
results.len(),
results.total,
record_name.to_ascii_lowercase(),
if results.len() == 1 { "" } else { "s" }
if results.total == 1 { "" } else { "s" }
);
}
}
#[derive(Debug, serde::Deserialize)]
struct ListResponse {
pub total: usize,
pub items: Vec<String>,
}
impl Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {

View File

@@ -190,10 +190,12 @@ pub enum AccountCommands {
/// List all user accounts
List {
/// Starting point for listing accounts
from: Option<String>,
/// Filter accounts by keywords
filter: Option<String>,
/// Maximum number of accounts to list
limit: Option<usize>,
/// Page number
page: Option<usize>,
},
}
@@ -255,10 +257,12 @@ pub enum ListCommands {
/// List all mailing lists
List {
/// Starting point for listing mailing lists
from: Option<String>,
/// Filter mailing lists by keywords
filter: Option<String>,
/// Maximum number of mailing lists to list
limit: Option<usize>,
/// Page number
page: Option<usize>,
},
}
@@ -320,10 +324,12 @@ pub enum GroupCommands {
/// List all groups
List {
/// Starting point for listing groups
from: Option<String>,
/// Filter groups by keywords
filter: Option<String>,
/// Maximum number of groups to list
limit: Option<usize>,
/// Page number
page: Option<usize>,
},
}

View File

@@ -32,19 +32,19 @@ impl ServerCommands {
match self {
ServerCommands::DatabaseMaintenance {} => {
client
.http_request::<Value, String>(Method::GET, "/admin/store/maintenance", None)
.http_request::<Value, String>(Method::GET, "/api/store/maintenance", None)
.await;
eprintln!("Success.");
}
ServerCommands::ReloadCertificates {} => {
client
.http_request::<Value, String>(Method::GET, "/admin/reload/certificates", None)
.http_request::<Value, String>(Method::GET, "/api/reload/certificates", None)
.await;
eprintln!("Success.");
}
ServerCommands::ReloadConfig {} => {
client
.http_request::<Value, String>(Method::GET, "/admin/reload/config", None)
.http_request::<Value, String>(Method::GET, "/api/reload/config", None)
.await;
eprintln!("Success.");
}
@@ -52,7 +52,7 @@ impl ServerCommands {
client
.http_request::<Value, _>(
Method::POST,
"/admin/config",
"/api/config",
Some(vec![(key.clone(), value.unwrap_or_default())]),
)
.await;
@@ -62,7 +62,7 @@ impl ServerCommands {
client
.http_request::<Value, String>(
Method::DELETE,
&format!("/admin/config/{key}"),
&format!("/api/config/{key}"),
None,
)
.await;
@@ -72,7 +72,7 @@ impl ServerCommands {
let results = client
.http_request::<Vec<(String, String)>, String>(
Method::GET,
&format!("/admin/config/{}", prefix.unwrap_or_default()),
&format!("/api/config/{}", prefix.unwrap_or_default()),
None,
)
.await;

View File

@@ -36,7 +36,7 @@ impl DomainCommands {
client
.http_request::<Value, String>(
Method::POST,
&format!("/admin/domain/{name}"),
&format!("/api/domain/{name}"),
None,
)
.await;
@@ -46,7 +46,7 @@ impl DomainCommands {
client
.http_request::<Value, String>(
Method::DELETE,
&format!("/admin/domain/{name}"),
&format!("/api/domain/{name}"),
None,
)
.await;
@@ -54,9 +54,9 @@ impl DomainCommands {
}
DomainCommands::List { from, limit } => {
let query = if from.is_none() && limit.is_none() {
Cow::Borrowed("/admin/domain")
Cow::Borrowed("/api/domain")
} else {
let mut query = "/admin/domain?".to_string();
let mut query = "/api/domain?".to_string();
if let Some(from) = &from {
query.push_str(&format!("from={from}"));
}

View File

@@ -50,13 +50,13 @@ impl GroupCommands {
..Default::default()
};
let account_id = client
.http_request::<u32, _>(Method::POST, "/admin/principal", Some(principal))
.http_request::<u32, _>(Method::POST, "/api/principal", Some(principal))
.await;
if let Some(members) = members {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(vec![PrincipalUpdate::set(
PrincipalField::Members,
PrincipalValue::StringList(members),
@@ -103,7 +103,7 @@ impl GroupCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(changes),
)
.await;
@@ -116,7 +116,7 @@ impl GroupCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
members
.into_iter()
@@ -136,7 +136,7 @@ impl GroupCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
members
.into_iter()
@@ -155,8 +155,14 @@ impl GroupCommands {
GroupCommands::Display { name } => {
client.display_principal(&name).await;
}
GroupCommands::List { from, limit } => {
client.list_principals("group", "Group", from, limit).await;
GroupCommands::List {
filter,
limit,
page,
} => {
client
.list_principals("group", "Group", filter, page, limit)
.await;
}
}
}

View File

@@ -50,13 +50,13 @@ impl ListCommands {
..Default::default()
};
let account_id = client
.http_request::<u32, _>(Method::POST, "/admin/principal", Some(principal))
.http_request::<u32, _>(Method::POST, "/api/principal", Some(principal))
.await;
if let Some(members) = members {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(vec![PrincipalUpdate::set(
PrincipalField::Members,
PrincipalValue::StringList(members),
@@ -103,7 +103,7 @@ impl ListCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(changes),
)
.await;
@@ -116,7 +116,7 @@ impl ListCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
members
.into_iter()
@@ -136,7 +136,7 @@ impl ListCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
members
.into_iter()
@@ -155,9 +155,13 @@ impl ListCommands {
ListCommands::Display { name } => {
client.display_principal(&name).await;
}
ListCommands::List { from, limit } => {
ListCommands::List {
filter,
limit,
page,
} => {
client
.list_principals("list", "Mailing List", from, limit)
.list_principals("list", "Mailing List", filter, page, limit)
.await;
}
}

View File

@@ -102,7 +102,7 @@ impl QueueCommands {
for (message, id) in client
.http_request::<Vec<Option<Message>>, String>(
Method::GET,
&build_query("/admin/queue/status?ids=", chunk),
&build_query("/api/queue/status?ids=", chunk),
None,
)
.await
@@ -176,7 +176,7 @@ impl QueueCommands {
for (message, id) in client
.http_request::<Vec<Option<Message>>, String>(
Method::GET,
&build_query("/admin/queue/status?ids=", &parse_ids(&ids)),
&build_query("/api/queue/status?ids=", &parse_ids(&ids)),
None,
)
.await
@@ -316,7 +316,7 @@ impl QueueCommands {
std::process::exit(1);
}
let mut query = form_urlencoded::Serializer::new("/admin/queue/retry?".to_string());
let mut query = form_urlencoded::Serializer::new("/api/queue/retry?".to_string());
if let Some(filter) = &domain {
query.append_pair("filter", filter);
@@ -371,8 +371,7 @@ impl QueueCommands {
std::process::exit(1);
}
let mut query =
form_urlencoded::Serializer::new("/admin/queue/cancel?".to_string());
let mut query = form_urlencoded::Serializer::new("/api/queue/cancel?".to_string());
if let Some(filter) = &rcpt {
query.append_pair("filter", filter);
@@ -414,7 +413,7 @@ impl Client {
before: &Option<DateTime>,
after: &Option<DateTime>,
) -> Vec<u64> {
let mut query = form_urlencoded::Serializer::new("/admin/queue/list?".to_string());
let mut query = form_urlencoded::Serializer::new("/api/queue/list?".to_string());
if let Some(sender) = from {
query.append_pair("from", sender);

View File

@@ -51,7 +51,7 @@ impl ReportCommands {
page_size,
} => {
let stdout = Term::buffered_stdout();
let mut query = form_urlencoded::Serializer::new("/admin/report/list?".to_string());
let mut query = form_urlencoded::Serializer::new("/api/report/list?".to_string());
if let Some(domain) = &domain {
query.append_pair("domain", domain);
@@ -78,7 +78,7 @@ impl ReportCommands {
for (report, id) in client
.http_request::<Vec<Option<Report>>, String>(
Method::GET,
&format!("/admin/report/status?ids={}", chunk.join(",")),
&format!("/api/report/status?ids={}", chunk.join(",")),
None,
)
.await
@@ -117,7 +117,7 @@ impl ReportCommands {
for (report, id) in client
.http_request::<Vec<Option<Report>>, String>(
Method::GET,
&format!("/admin/report/status?ids={}", ids.join(",")),
&format!("/api/report/status?ids={}", ids.join(",")),
None,
)
.await
@@ -173,7 +173,7 @@ impl ReportCommands {
for (success, id) in client
.http_request::<Vec<bool>, String>(
Method::GET,
&format!("/admin/report/cancel?ids={}", ids.join(",")),
&format!("/api/report/cancel?ids={}", ids.join(",")),
None,
)
.await