diff --git a/src/albums.rs b/src/albums.rs index edeed0d..438befc 100644 --- a/src/albums.rs +++ b/src/albums.rs @@ -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>, 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(); } diff --git a/src/main.rs b/src/main.rs index e0c0fda..bc3a3f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { .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> { } 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(); }