From 567e750f73c665c1d9477ec82b72371185d67c07 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 19 Oct 2021 11:40:02 +0200 Subject: [PATCH] Refactored to separate session and albums related functions Signed-off-by: Louis Vallat --- src/albums.rs | 16 ++++++++++++++++ src/main.rs | 39 +++++---------------------------------- src/session.rs | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 src/albums.rs create mode 100644 src/session.rs diff --git a/src/albums.rs b/src/albums.rs new file mode 100644 index 0000000..08ae8a3 --- /dev/null +++ b/src/albums.rs @@ -0,0 +1,16 @@ +use hyper_tls::HttpsConnector; +use hyper::{Body, Client, Method, Request, client::HttpConnector, header::{AUTHORIZATION, COOKIE, HeaderValue}}; +use json::JsonValue; +use crate::body_to_str; + +pub async fn get_albums(client: &Client>, lychee_session: HeaderValue) -> JsonValue { + let req = Request::builder() + .method(Method::POST) + .uri(dotenv::var("LYCHEE_ENDPOINT").unwrap() + "/api/Albums::get") + .header(COOKIE, lychee_session) + .header(AUTHORIZATION, dotenv::var("API_KEY").unwrap()) + .body(Body::empty()) + .expect("error"); + let _res = client.request(req).await.unwrap(); + return json::parse(body_to_str(_res).await.as_str()).unwrap(); +} diff --git a/src/main.rs b/src/main.rs index 14b7769..d7bff44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,11 @@ use hyper_tls::HttpsConnector; -use hyper::{Body, Client, Method, Request, Response, client::HttpConnector, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, HeaderValue, SET_COOKIE}}; -use json::{JsonValue, object}; +use hyper::{Body, Client, Response}; use dotenv::dotenv; +use crate::session::login; +use crate::albums::get_albums; +mod session; +mod albums; pub async fn body_to_str(res: Response) -> String { let body = hyper::body::to_bytes(res).await.unwrap(); @@ -10,38 +13,6 @@ pub async fn body_to_str(res: Response) -> String { return body_str.to_string(); } -async fn login(client: &Client>) -> HeaderValue { - let login_data = object! { - "username": dotenv::var("USERNAME").unwrap(), - "password": dotenv::var("PASSWORD").unwrap() - }; - - let req = Request::builder() - .method(Method::POST) - .uri(dotenv::var("LYCHEE_ENDPOINT").unwrap() + "/api/Session::login") - .header(AUTHORIZATION, dotenv::var("API_KEY").unwrap()) - .header(CONTENT_TYPE, "application/json") - .body(Body::from(login_data.dump())) - .expect("error"); - let res = client.request(req).await.unwrap(); - let lychee_session = res.headers().get(SET_COOKIE.as_str()).unwrap().clone(); - assert_eq!(json::parse(body_to_str(res).await.as_str()).unwrap(), true); - - return lychee_session; -} - -async fn get_albums(client: &Client>, lychee_session: HeaderValue) -> JsonValue { - let req = Request::builder() - .method(Method::POST) - .uri(dotenv::var("LYCHEE_ENDPOINT").unwrap() + "/api/Albums::get") - .header(COOKIE, lychee_session) - .header(AUTHORIZATION, dotenv::var("API_KEY").unwrap()) - .body(Body::empty()) - .expect("error"); - let _res = client.request(req).await.unwrap(); - return json::parse(body_to_str(_res).await.as_str()).unwrap(); -} - #[tokio::main] async fn main() -> Result<(), Box> { diff --git a/src/session.rs b/src/session.rs new file mode 100644 index 0000000..b0c1b07 --- /dev/null +++ b/src/session.rs @@ -0,0 +1,24 @@ +use hyper_tls::HttpsConnector; +use hyper::{Body, Client, Method, Request, client::HttpConnector, header::{AUTHORIZATION, CONTENT_TYPE, HeaderValue, SET_COOKIE}}; +use json::object; +use crate::body_to_str; + +pub async fn login(client: &Client>) -> HeaderValue { + let login_data = object! { + "username": dotenv::var("USERNAME").unwrap(), + "password": dotenv::var("PASSWORD").unwrap() + }; + + let req = Request::builder() + .method(Method::POST) + .uri(dotenv::var("LYCHEE_ENDPOINT").unwrap() + "/api/Session::login") + .header(AUTHORIZATION, dotenv::var("API_KEY").unwrap()) + .header(CONTENT_TYPE, "application/json") + .body(Body::from(login_data.dump())) + .expect("error"); + let res = client.request(req).await.unwrap(); + let lychee_session = res.headers().get(SET_COOKIE.as_str()).unwrap().clone(); + assert_eq!(json::parse(body_to_str(res).await.as_str()).unwrap(), true); + + return lychee_session; +}