Replaced the json crate with serde_json

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-28 15:19:24 +02:00
parent 912908a56d
commit b587f8e7fb
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
5 changed files with 14 additions and 20 deletions

8
Cargo.lock generated
View File

@ -406,12 +406,6 @@ version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "json"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -451,9 +445,9 @@ dependencies = [
"dirs", "dirs",
"hyper", "hyper",
"hyper-tls", "hyper-tls",
"json",
"mockito", "mockito",
"serde", "serde",
"serde_json",
"tokio", "tokio",
"tokio-test", "tokio-test",
"toml", "toml",

View File

@ -10,12 +10,12 @@ hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tokio-test = "0.4.2" tokio-test = "0.4.2"
hyper-tls = "0.5.0" hyper-tls = "0.5.0"
json = "0.12.4"
clap = "2.33.3" clap = "2.33.3"
dirs = "4.0.0" dirs = "4.0.0"
cookie = "0.15.1" cookie = "0.15.1"
toml = "0.5.8" toml = "0.5.8"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1"
[dev-dependencies] [dev-dependencies]
mockito = "0.30.0" mockito = "0.30.0"

View File

@ -1,9 +1,9 @@
use hyper::{Body, Method, Request, header::{AUTHORIZATION, COOKIE}}; use hyper::{Body, Method, Request, header::{AUTHORIZATION, COOKIE}};
use json::JsonValue; use serde_json::{Value, json};
use crate::{LycheeClient, body_to_str}; use crate::{LycheeClient, body_to_str};
pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> JsonValue { pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> Value {
let req = Request::builder() let req = Request::builder()
.method(Method::POST) .method(Method::POST)
.uri(c.endpoint.to_string() + "/api/Albums::get") .uri(c.endpoint.to_string() + "/api/Albums::get")
@ -12,5 +12,5 @@ pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> JsonValue {
.body(Body::empty()) .body(Body::empty())
.expect("Cannot request /api/Albums::get."); .expect("Cannot request /api/Albums::get.");
let res = c.client.request(req).await.unwrap(); let res = c.client.request(req).await.unwrap();
return json::parse(body_to_str(res.into_body()).await.as_str()).unwrap(); return json!(body_to_str(res.into_body()).await);
} }

View File

@ -76,7 +76,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
logout(&client, &lychee_session_cookie).await; logout(&client, &lychee_session_cookie).await;
} else if let Some(matches) = matches.subcommand_matches("albums") { } else if let Some(matches) = matches.subcommand_matches("albums") {
if matches.is_present("get") { if matches.is_present("get") {
println!("{}", get_albums(&client, &lychee_session_cookie).await.pretty(4)); println!("{}", get_albums(&client, &lychee_session_cookie).await);
} else { } else {
App::print_long_help(&mut app).unwrap(); App::print_long_help(&mut app).unwrap();
} }

View File

@ -1,17 +1,17 @@
use hyper::{Body, Method, Request, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, SET_COOKIE}}; use hyper::{Body, Method, Request, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, SET_COOKIE}};
use json::object; use serde_json::json;
use crate::{LycheeClient, body_to_str}; use crate::{LycheeClient, body_to_str};
pub async fn login(client: &LycheeClient, login: &str, password: &str) -> String { pub async fn login(client: &LycheeClient, login: &str, password: &str) -> String {
let login_data = object! { "username": login, "password": password }; let login_data = json!({ "username": login, "password": password });
let req = Request::builder() let req = Request::builder()
.method(Method::POST) .method(Method::POST)
.uri(client.endpoint.clone() + "/api/Session::login") .uri(client.endpoint.clone() + "/api/Session::login")
.header(AUTHORIZATION, client.api_key.clone()) .header(AUTHORIZATION, client.api_key.clone())
.header(CONTENT_TYPE, "application/json") .header(CONTENT_TYPE, "application/json")
.body(Body::from(login_data.dump())) .body(Body::from(login_data.to_string()))
.expect("Cannot request Session::login."); .expect("Cannot request Session::login.");
let res = client.client.request(req).await.unwrap(); let res = client.client.request(req).await.unwrap();
let headers = res.headers().get_all(SET_COOKIE) let headers = res.headers().get_all(SET_COOKIE)
@ -39,7 +39,7 @@ pub async fn logout(client: &LycheeClient, lychee_session: &str) {
mod session_tests { mod session_tests {
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use hyper::Client; use hyper::Client;
use json::object; use serde_json::json;
use mockito::mock; use mockito::mock;
use crate::{LycheeClient, session::{login, logout}}; use crate::{LycheeClient, session::{login, logout}};
@ -143,24 +143,24 @@ mod session_tests {
#[test] #[test]
fn login_check_username_password() { fn login_check_username_password() {
let body1 = object!{ "username": "username", "password": "password" }; let body1 = json!({ "username": "username", "password": "password" });
let client = setup(); let client = setup();
let m1 = mock("POST", "/api/Session::login") let m1 = mock("POST", "/api/Session::login")
.with_header("content-type", "application/json") .with_header("content-type", "application/json")
.with_header("set-cookie", "lychee_session=demo") .with_header("set-cookie", "lychee_session=demo")
.with_body("true") .with_body("true")
.match_body(body1.dump().as_str()) .match_body(body1.to_string().as_str())
.create(); .create();
tokio_test::block_on(login(&client, "username", "password")); tokio_test::block_on(login(&client, "username", "password"));
m1.assert(); m1.assert();
let body2 = object!{ "username": "u", "password": "p" }; let body2 = json!({ "username": "u", "password": "p" });
let client = setup(); let client = setup();
let m2 = mock("POST", "/api/Session::login") let m2 = mock("POST", "/api/Session::login")
.with_header("content-type", "application/json") .with_header("content-type", "application/json")
.with_header("set-cookie", "lychee_session=demo") .with_header("set-cookie", "lychee_session=demo")
.with_body("true") .with_body("true")
.match_body(body2.dump().as_str()) .match_body(body2.to_string().as_str())
.create(); .create();
tokio_test::block_on(login(&client, "u", "p")); tokio_test::block_on(login(&client, "u", "p"));
m2.assert(); m2.assert();