Added the album subcommand and the get arg, to query all albums

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-26 16:53:49 +02:00
parent f32becf5f5
commit 997c7b15bf
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
2 changed files with 24 additions and 9 deletions

View File

@ -1,17 +1,16 @@
use hyper_tls::HttpsConnector;
use hyper::{Body, Client, Method, Request, client::HttpConnector, header::{AUTHORIZATION, COOKIE, HeaderValue}};
use hyper::{Body, Method, Request, header::{AUTHORIZATION, COOKIE}};
use json::JsonValue;
use crate::body_to_str;
use crate::{LycheeClient, body_to_str};
pub async fn get_albums(client: &Client<HttpsConnector<HttpConnector>>, lychee_session: &HeaderValue) -> JsonValue {
pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> JsonValue {
let req = Request::builder()
.method(Method::POST)
.uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Albums::get")
.uri(c.endpoint.to_string() + "/api/Albums::get")
.header(COOKIE, lychee_session)
.header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap())
.header(AUTHORIZATION, c.api_key.to_string())
.body(Body::empty())
.expect("error");
let res = client.request(req).await.unwrap();
.expect("Cannot request /api/Albums::get.");
let res = c.client.request(req).await.unwrap();
return json::parse(body_to_str(res.into_body()).await.as_str()).unwrap();
}

View File

@ -4,7 +4,7 @@ use hyper_tls::HttpsConnector;
use hyper::{Body, Client, body, client::HttpConnector};
use clap::{Arg, App, SubCommand};
use session::logout;
use crate::session::login;
use crate::{albums::get_albums, session::login};
use dotenv::dotenv;
mod session;
@ -65,6 +65,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.subcommand(
SubCommand::with_name("logout")
.about("Logout using the stored session cookie.")
)
.subcommand(
SubCommand::with_name("albums")
.about("Manage the albums.")
.arg(Arg::with_name("get")
.long("get")
.short("g")
.required(false)
.help("List the albums on the server."))
);
let matches = app.clone().get_matches();
@ -88,6 +97,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
} else if let Some(_) = matches.subcommand_matches("logout") {
let lychee_session = read_from_file(session_path.to_str().unwrap());
logout(&client, lychee_session.as_str()).await;
} else if let Some(matches) = matches.subcommand_matches("albums") {
if matches.is_present("get") {
let lychee_session = read_from_file(session_path.to_str().unwrap());
println!("{}", get_albums(&client, lychee_session.as_str()).await.pretty(4));
} else {
App::print_long_help(&mut app).unwrap();
}
} else {
App::print_long_help(&mut app).unwrap();
}