Using tabled to show the values from get_albums

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-29 09:45:57 +02:00
parent b587f8e7fb
commit 7f8312d0c2
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
4 changed files with 84 additions and 4 deletions

31
Cargo.lock generated
View File

@ -448,6 +448,7 @@ dependencies = [
"mockito",
"serde",
"serde_json",
"tabled",
"tokio",
"tokio-test",
"toml",
@ -581,6 +582,15 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "papergrid"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fe93a33fc67c8a9d21f3fefa54c4a991e08d2eba5f2fd6f327ca520862eddfb"
dependencies = [
"unicode-width",
]
[[package]]
name = "parking_lot"
version = "0.11.2"
@ -969,6 +979,27 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "tabled"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "465c45048a4b177eee43527a3741866dd66839f15c3223b5047dbc43d8a5076e"
dependencies = [
"papergrid",
"tabled_derive",
]
[[package]]
name = "tabled_derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b366a04a152b687a209b2459f29da3650b74bc859390308949305211e28af4b9"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tempfile"
version = "3.2.0"

View File

@ -16,6 +16,7 @@ cookie = "0.15.1"
toml = "0.5.8"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tabled = "0.3.0"
[dev-dependencies]
mockito = "0.30.0"

View File

@ -1,9 +1,54 @@
use hyper::{Body, Method, Request, header::{AUTHORIZATION, COOKIE}};
use serde_json::{Value, json};
use serde_json::{Map, Value, from_str};
use serde::Deserialize;
use tabled::Tabled;
use crate::{LycheeClient, body_to_str};
pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> Value {
fn display_option_string(o: &Option<String>) -> String {
match o {
Some(s) => format!("{}", s),
None => format!("N/A")
}
}
#[derive(Deserialize, Debug)]
pub struct AllAlbums {
pub smartalbums: Map<String, Value>,
pub albums: Vec<Album>,
pub shared_albums: Vec<Album>
}
#[derive(Deserialize, Debug, Tabled)]
pub struct Album {
pub id: String,
pub title: String,
pub public: String,
pub full_photo: String,
pub visible: String,
pub nsfw: String,
pub parent_id: String,
pub cover_id: String,
pub description: String,
pub downloadable: String,
pub share_button_visible: String,
pub created_at: String,
pub updated_at: String,
#[field(display_with="display_option_string")]
pub min_taken_at: Option<String>,
#[field(display_with="display_option_string")]
pub max_taken_at: Option<String>,
pub password: String,
pub license: String,
pub sorting_col: String,
pub sorting_order: String,
#[field(display_with="display_option_string")]
pub thumb: Option<String>,
pub has_albums: String,
pub owner: String
}
pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> AllAlbums {
let req = Request::builder()
.method(Method::POST)
.uri(c.endpoint.to_string() + "/api/Albums::get")
@ -12,5 +57,6 @@ pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> Value {
.body(Body::empty())
.expect("Cannot request /api/Albums::get.");
let res = c.client.request(req).await.unwrap();
return json!(body_to_str(res.into_body()).await);
let v: AllAlbums = from_str(body_to_str(res.into_body()).await.as_str()).unwrap();
return v;
}

View File

@ -4,6 +4,7 @@ use hyper::{Client, client::HttpConnector};
use clap::{App, Arg, SubCommand, crate_version};
use session::logout;
use cookie::Cookie;
use tabled::Table;
mod albums;
mod session;
@ -76,7 +77,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
logout(&client, &lychee_session_cookie).await;
} else if let Some(matches) = matches.subcommand_matches("albums") {
if matches.is_present("get") {
println!("{}", get_albums(&client, &lychee_session_cookie).await);
let a = get_albums(&client, &lychee_session_cookie).await.albums;
println!("{}", Table::new(a).to_string());
} else {
App::print_long_help(&mut app).unwrap();
}