2021-10-15 08:59:12 +02:00
|
|
|
use hyper_tls::HttpsConnector;
|
2021-10-17 23:36:18 +02:00
|
|
|
use hyper::{Body, Client, Method, Request, Response, client::HttpConnector, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, HeaderValue, SET_COOKIE}};
|
|
|
|
use json::{JsonValue, object};
|
2021-10-16 22:20:46 +02:00
|
|
|
use dotenv::dotenv;
|
2021-10-15 08:59:12 +02:00
|
|
|
|
2021-10-16 22:20:46 +02:00
|
|
|
|
2021-10-18 09:36:00 +02:00
|
|
|
async fn body_to_str(res: Response<Body>) -> String {
|
2021-10-17 23:36:18 +02:00
|
|
|
let body = hyper::body::to_bytes(res).await.unwrap();
|
|
|
|
let body_str = std::str::from_utf8(&body).unwrap().clone();
|
|
|
|
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()
|
|
|
|
};
|
|
|
|
|
2021-10-16 22:20:46 +02:00
|
|
|
let req = Request::builder()
|
|
|
|
.method(Method::POST)
|
2021-10-17 23:36:18 +02:00
|
|
|
.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())
|
2021-10-16 22:20:46 +02:00
|
|
|
.body(Body::empty())
|
|
|
|
.expect("error");
|
2021-10-17 23:36:18 +02:00
|
|
|
let _res = client.request(req).await.unwrap();
|
|
|
|
return json::parse(body_to_str(_res).await.as_str()).unwrap();
|
|
|
|
}
|
2021-10-15 08:59:12 +02:00
|
|
|
|
|
|
|
|
2021-10-17 23:36:18 +02:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
|
|
dotenv().ok();
|
2021-10-15 08:59:12 +02:00
|
|
|
|
2021-10-17 23:36:18 +02:00
|
|
|
let https = HttpsConnector::new();
|
|
|
|
let client = Client::builder().build::<_, hyper::Body>(https);
|
|
|
|
let lychee_session = login(&client).await;
|
|
|
|
let albums = get_albums(&client, lychee_session).await;
|
|
|
|
println!("{}", albums["albums"].pretty(4));
|
2021-10-15 08:59:12 +02:00
|
|
|
Ok(())
|
|
|
|
}
|