Get list of all recipes from server
This commit is contained in:
parent
cde6c7fe9f
commit
432327338f
3 changed files with 264 additions and 15 deletions
62
src/main.rs
62
src/main.rs
|
@ -1,3 +1,5 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
// SPDX-FileCopyrightText: 2022 Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
|
@ -8,6 +10,7 @@ use {
|
|||
self::config::Config,
|
||||
anyhow::anyhow,
|
||||
base64::write::EncoderWriter as Base64Encoder,
|
||||
chrono::{DateTime, Utc},
|
||||
clap::{arg, command, ArgMatches, Command},
|
||||
reqwest::Url,
|
||||
std::io::Write,
|
||||
|
@ -28,9 +31,18 @@ fn parse_args() -> ArgMatches {
|
|||
.subcommand(
|
||||
Command::new("import")
|
||||
.about("Import the given URLs into NextCloud's cookbook")
|
||||
.arg(server_arg)
|
||||
.arg(server_arg.clone())
|
||||
.arg(arg!(<url> ... "One or more URLs each pointing to page with a recipe to import in NextCloud")),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("schedule")
|
||||
.about("")
|
||||
.arg(server_arg.clone())
|
||||
.arg(arg!(-d --days <days> "")
|
||||
.value_parser(clap::builder::RangedU64ValueParser::<u32>::new().range(1..))
|
||||
.required(false)
|
||||
.default_value("7"))
|
||||
)
|
||||
.get_matches()
|
||||
}
|
||||
|
||||
|
@ -53,18 +65,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
})?;
|
||||
}
|
||||
Some(("import", sub_matches)) => {
|
||||
let server_name = sub_matches
|
||||
.get_one::<String>("server")
|
||||
.unwrap_or_else(|| {
|
||||
let servers = &configuration.credentials.servers;
|
||||
match servers.len() {
|
||||
0 => panic!("No NextCloud server set up yet, use '{} init' first", env!("CARGO_BIN_NAME")),
|
||||
1 => servers.iter().next().unwrap().0,
|
||||
_ => panic!("More than one NextCloud server set up, use the '--server' option to specify which one to use. Known servers: {:#?}", servers.keys().collect::<Vec<_>>()),
|
||||
}
|
||||
});
|
||||
|
||||
let api_client = ApiClient::new(&server_name, &configuration)?;
|
||||
let api_client = get_api_client(&sub_matches, &configuration)?;
|
||||
for url in sub_matches
|
||||
.get_many::<String>("url")
|
||||
.expect("At least one url is required")
|
||||
|
@ -80,12 +81,47 @@ async fn main() -> anyhow::Result<()> {
|
|||
println!("{:#?}", response); // TODO
|
||||
}
|
||||
}
|
||||
Some(("schedule", sub_matches)) => {
|
||||
let api_client = get_api_client(&sub_matches, &configuration)?;
|
||||
let recipes = api_client
|
||||
.client
|
||||
.get(api_client.base_url.join("apps/cookbook/api/recipes")?)
|
||||
.send()
|
||||
.await?;
|
||||
println!("{:#?}", recipes.json::<Vec<Recipe>>().await?); // TODO
|
||||
}
|
||||
_ => unreachable!("Exhausted list of subcommands and subcommand_required prevents `None`"),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Recipe {
|
||||
#[serde(rename = "recipe_id")]
|
||||
recipe_id: u32,
|
||||
name: String,
|
||||
keywords: String,
|
||||
date_created: DateTime<Utc>,
|
||||
date_modified: DateTime<Utc>,
|
||||
}
|
||||
|
||||
fn get_api_client(sub_matches: &ArgMatches, configuration: &Config) -> anyhow::Result<ApiClient> {
|
||||
let server_name = sub_matches
|
||||
.get_one::<String>("server")
|
||||
.unwrap_or_else(|| {
|
||||
let servers = &configuration.credentials.servers;
|
||||
match servers.len() {
|
||||
0 => panic!("No NextCloud server set up yet, use '{} init' first", env!("CARGO_BIN_NAME")),
|
||||
1 => servers.iter().next().unwrap().0,
|
||||
_ => panic!("More than one NextCloud server set up, use the '--server' option to specify which one to use. Known servers: {:#?}", servers.keys().collect::<Vec<_>>()),
|
||||
}
|
||||
});
|
||||
|
||||
ApiClient::new(&server_name, &configuration)
|
||||
}
|
||||
|
||||
struct ApiClient {
|
||||
pub base_url: Url,
|
||||
pub client: reqwest::Client,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue