lychee_session => &lychee_session in logout

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-21 17:10:17 +02:00
parent 14d801d907
commit 8e8dcece2d
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
3 changed files with 9 additions and 8 deletions

View File

@ -4,7 +4,7 @@ use json::JsonValue;
use crate::body_to_str; use crate::body_to_str;
pub async fn get_albums(client: &Client<HttpsConnector<HttpConnector>>, lychee_session: HeaderValue) -> JsonValue { pub async fn get_albums(client: &Client<HttpsConnector<HttpConnector>>, lychee_session: &HeaderValue) -> JsonValue {
let req = Request::builder() let req = Request::builder()
.method(Method::POST) .method(Method::POST)
.uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Albums::get") .uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Albums::get")

View File

@ -1,6 +1,6 @@
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use hyper::{Body, Client, Response}; use hyper::{Body, Client, Response};
use crate::session::login; use crate::session::{login, logout};
use crate::albums::get_albums; use crate::albums::get_albums;
use dotenv::dotenv; use dotenv::dotenv;
@ -21,7 +21,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let https = HttpsConnector::new(); let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https); let client = Client::builder().build::<_, hyper::Body>(https);
let lychee_session = login(&client).await; let lychee_session = login(&client).await;
let albums = get_albums(&client, lychee_session).await; let albums = get_albums(&client, &lychee_session).await;
logout(&client, &lychee_session).await;
println!("{}", albums["albums"].pretty(4)); println!("{}", albums["albums"].pretty(4));
Ok(()) Ok(())
} }

View File

@ -24,11 +24,11 @@ pub async fn login(client: &Client<HttpsConnector<HttpConnector>>) -> HeaderValu
return lychee_session; return lychee_session;
} }
pub async fn logout(client: &Client<HttpsConnector<HttpConnector>>, cookie: String) { pub async fn logout(client: &Client<HttpsConnector<HttpConnector>>, lychee_session: &HeaderValue) {
let req = Request::builder() let req = Request::builder()
.method(Method::POST) .method(Method::POST)
.uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Session::logout") .uri(std::env::var("LYCHEE_ENDPOINT").unwrap() + "/api/Session::logout")
.header(COOKIE.as_str(), cookie) .header(COOKIE.as_str(), lychee_session)
.header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap()) .header(AUTHORIZATION, std::env::var("LYCHEE_API_KEY").unwrap())
.body(Body::empty()) .body(Body::empty())
.expect("error"); .expect("error");
@ -39,7 +39,7 @@ pub async fn logout(client: &Client<HttpsConnector<HttpConnector>>, cookie: Stri
#[cfg(test)] #[cfg(test)]
mod session_tests { mod session_tests {
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use hyper::{Client, client::HttpConnector}; use hyper::{Client, client::HttpConnector, header::HeaderValue};
use json::object; use json::object;
use mockito::mock; use mockito::mock;
use crate::session::{login, logout}; use crate::session::{login, logout};
@ -64,7 +64,7 @@ mod session_tests {
.match_header("cookie", "cookie value") .match_header("cookie", "cookie value")
.create(); .create();
tokio_test::block_on(logout(&client, "cookie value".to_string())); tokio_test::block_on(logout(&client, &HeaderValue::from_str("cookie value").unwrap()));
m.assert(); m.assert();
} }
@ -77,7 +77,7 @@ mod session_tests {
.match_header("cookie", "cookie value") .match_header("cookie", "cookie value")
.create(); .create();
tokio_test::block_on(logout(&client, "cookie value".to_string())); tokio_test::block_on(logout(&client, &HeaderValue::from_str("cookie value").unwrap()));
m.assert(); m.assert();
} }