Refactored to separate session and albums related functions
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
a949f196c8
commit
567e750f73
16
src/albums.rs
Normal file
16
src/albums.rs
Normal 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();
|
||||||
|
}
|
39
src/main.rs
39
src/main.rs
@ -1,8 +1,11 @@
|
|||||||
use hyper_tls::HttpsConnector;
|
use hyper_tls::HttpsConnector;
|
||||||
use hyper::{Body, Client, Method, Request, Response, client::HttpConnector, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, HeaderValue, SET_COOKIE}};
|
use hyper::{Body, Client, Response};
|
||||||
use json::{JsonValue, object};
|
|
||||||
use dotenv::dotenv;
|
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 {
|
pub async fn body_to_str(res: Response<Body>) -> String {
|
||||||
let body = hyper::body::to_bytes(res).await.unwrap();
|
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();
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
24
src/session.rs
Normal file
24
src/session.rs
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user