mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 19:57:25 +01:00
Add HTTP client and login support
This commit is contained in:
parent
d20d80d1ac
commit
ea3bd35ffd
16 changed files with 832 additions and 25 deletions
31
src/backend/client.rs
Normal file
31
src/backend/client.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use super::Backend;
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use isahc::http::StatusCode;
|
||||
use isahc::prelude::*;
|
||||
|
||||
impl Backend {
|
||||
/// Try to login a user with the provided credentials and return, wether the login suceeded.
|
||||
pub async fn login(&self) -> Result<bool> {
|
||||
let server_url = self.get_server_url().ok_or(anyhow!("No server URL set!"))?;
|
||||
let data = self.get_login_data().ok_or(anyhow!("No login data set!"))?;
|
||||
|
||||
let request = Request::post(format!("{}/login", server_url))
|
||||
.header("Content-Type", "application/json")
|
||||
.body(serde_json::to_string(&data)?)?;
|
||||
|
||||
let mut response = isahc::send_async(request).await?;
|
||||
|
||||
let success = match response.status() {
|
||||
StatusCode::OK => {
|
||||
let token = response.text_async().await?;
|
||||
self.set_token(&token);
|
||||
println!("{}", &token);
|
||||
true
|
||||
}
|
||||
StatusCode::UNAUTHORIZED => false,
|
||||
_ => bail!("Unexpected response status!"),
|
||||
};
|
||||
|
||||
Ok(success)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue