Refactored to separate session and albums related functions

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-19 11:40:02 +02:00
parent a949f196c8
commit 567e750f73
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
3 changed files with 45 additions and 34 deletions

16
src/albums.rs Normal file
View File

@ -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<HttpsConnector<HttpConnector>>, 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();
}

View File

@ -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<Body>) -> String {
let body = hyper::body::to_bytes(res).await.unwrap();
@ -10,38 +13,6 @@ pub async fn body_to_str(res: Response<Body>) -> String {
return body_str.to_string();
}
async fn login(client: &Client<HttpsConnector<HttpConnector>>) -> 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<HttpsConnector<HttpConnector>>, 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<dyn std::error::Error + Send + Sync>> {

24
src/session.rs Normal file
View File

@ -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<HttpsConnector<HttpConnector>>) -> 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;
}