From 028adbff93045997d0559272512865a6cd5a8bd9 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Sun, 24 Oct 2021 00:01:36 +0200 Subject: [PATCH] Added a structure to hold all of our recurrent and needed data that can be passed to all functions Signed-off-by: Louis Vallat --- src/main.rs | 17 +++++++++++++--- src/session.rs | 55 ++++++++++++++++++-------------------------------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/main.rs b/src/main.rs index b3d5345..3f6df21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use hyper_tls::HttpsConnector; -use hyper::{Body, Client, Response}; +use hyper::{Body, Client, Response, client::HttpConnector}; use clap::{Arg, App, SubCommand}; use crate::session::login; @@ -7,6 +7,12 @@ use dotenv::dotenv; mod session; mod albums; +pub struct LycheeClient { + client: Client>, + endpoint: String, + api_key: String +} + pub async fn body_to_str(res: Response) -> String { let body = hyper::body::to_bytes(res).await.unwrap(); let body_str = std::str::from_utf8(&body).unwrap().clone(); @@ -39,8 +45,13 @@ async fn main() -> Result<(), Box> { .get_matches(); dotenv().ok(); - let https = HttpsConnector::new(); - let client = Client::builder().build::<_, hyper::Body>(https); + + let client = LycheeClient { + client: Client::builder().build::<_, hyper::Body>(HttpsConnector::new()), + endpoint: std::env::var("LYCHEE_ENDPOINT").unwrap(), + api_key: std::env::var("LYCHEE_API_KEY").unwrap() + }; + if let Some(matches) = matches.subcommand_matches("login") { let username = matches.value_of("username").unwrap(); diff --git a/src/session.rs b/src/session.rs index 4a1c245..bf31c76 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,52 +1,51 @@ -use hyper_tls::HttpsConnector; -use hyper::{Body, Client, Method, Request, client::HttpConnector, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, HeaderValue, SET_COOKIE}}; +use hyper::{Body, Method, Request, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, HeaderValue, SET_COOKIE}}; use json::object; -use crate::body_to_str; +use crate::{LycheeClient, body_to_str}; -pub async fn login(client: &Client>, login: &str, password: &str) -> HeaderValue { +pub async fn login(client: &LycheeClient, login: &str, password: &str) -> HeaderValue { let login_data = object! { "username": login, "password": password }; let req = Request::builder() .method(Method::POST) - .uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Session::login") - .header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap()) + .uri(client.endpoint.clone() + "/api/Session::login") + .header(AUTHORIZATION, client.api_key.clone()) .header(CONTENT_TYPE, "application/json") .body(Body::from(login_data.dump())) .expect("error"); - let res = client.request(req).await.unwrap(); + let res = client.client.request(req).await.unwrap(); let lychee_session = res.headers().get(SET_COOKIE.as_str()).unwrap().clone(); - assert_eq!(res.headers().get(CONTENT_TYPE.as_str()).unwrap(), "application/json"); assert!(json::parse(body_to_str(res).await.as_str()).unwrap().as_bool().unwrap()); return lychee_session; } -pub async fn logout(client: &Client>, lychee_session: &HeaderValue) { +pub async fn logout(client: &LycheeClient, lychee_session: &HeaderValue) { let req = Request::builder() .method(Method::POST) - .uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Session::logout") + .uri(client.endpoint.clone() + "/api/Session::logout") .header(COOKIE.as_str(), lychee_session) - .header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap()) + .header(AUTHORIZATION, client.api_key.clone()) .body(Body::empty()) .expect("error"); - let res = client.request(req).await.unwrap(); + let res = client.client.request(req).await.unwrap(); assert!(json::parse(body_to_str(res).await.as_str()).unwrap().as_bool().unwrap()); } #[cfg(test)] mod session_tests { use hyper_tls::HttpsConnector; - use hyper::{Client, client::HttpConnector, header::HeaderValue}; + use hyper::{Client, header::HeaderValue}; use json::object; use mockito::mock; - use crate::session::{login, logout}; + use crate::{LycheeClient, session::{login, logout}}; - fn setup() -> Client> { + fn setup() -> LycheeClient { let https = HttpsConnector::new(); - let client = Client::builder().build::<_, hyper::Body>(https); - std::env::set_var("LYCHEE_ENDPOINT", mockito::server_url()); - std::env::set_var("LYCHEE_API_KEY", "value"); - return client; + return LycheeClient { + client: Client::builder().build::<_, hyper::Body>(https), + endpoint: mockito::server_url(), + api_key: "value".to_string() + }; } #[test] @@ -115,23 +114,9 @@ mod session_tests { m.assert(); } - #[test] - #[should_panic] - fn login_html_returned() { - let client = setup(); - let m = mock("POST", "/api/Session::login") - .with_header("content-type", "text/html") - .with_header("set-cookie", "demo") - .with_body("true") - .with_status(200) - .create(); - tokio_test::block_on(login(&client, "u", "p")); - m.assert(); - } - #[test] fn login_check_api_key() { - let client = setup(); + let mut client = setup(); let m1 = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") .with_header("set-cookie", "demo") @@ -141,7 +126,7 @@ mod session_tests { tokio_test::block_on(login(&client, "u", "p")); m1.assert(); - std::env::set_var("LYCHEE_API_KEY", "othervalue"); + client.api_key = "othervalue".to_string(); let m2 = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") .with_header("set-cookie", "demo")