feat(backend): implemented the first api calls, to enable login

This commit is contained in:
antifallobst 2023-09-13 07:24:34 +02:00
parent ba25f98d58
commit f965a4ddd9
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
2 changed files with 28 additions and 8 deletions

View File

@ -10,14 +10,14 @@ struct AuthResponse {
#[derive(Debug, Deserialize)]
struct UserIdResponse {
name: String,
username: String,
id: i64,
}
#[derive(Debug, Deserialize)]
struct UserInfoResponse {
id: i64,
name: String,
username: String,
joined: i64,
is_admin: bool,
}
@ -54,7 +54,12 @@ impl Api {
StatusCode::FAILED_DEPENDENCY => {
return Ok(Err(Error::msg("Account not verified!")))
}
_ => return Ok(Err(Error::msg("Unknown error"))),
_ => {
return Ok(Err(Error::msg(format!(
"Unknown error: {}",
response.status().to_string()
))))
}
}
let response_body: AuthResponse = response.json().await?;
@ -69,7 +74,7 @@ impl Api {
let request = format!("{base_url}/account/id");
let response = Client::new()
.post(request)
.get(request)
.bearer_auth(&token)
.send()
.await?;
@ -81,7 +86,12 @@ impl Api {
StatusCode::FORBIDDEN => {
return Ok(Err(Error::msg("Blocked for security reasons!")))
}
_ => return Ok(Err(Error::msg("Unknown error"))),
_ => {
return Ok(Err(Error::msg(format!(
"Unknown error: {}",
response.status().to_string()
))))
}
}
let response_body: UserIdResponse = response.json().await?;
@ -104,7 +114,12 @@ impl Api {
return Ok(Err(Error::msg("Blocked for security reasons!")))
}
StatusCode::NOT_FOUND => return Ok(Err(Error::msg("User not found"))),
_ => return Ok(Err(Error::msg("Unknown error"))),
_ => {
return Ok(Err(Error::msg(format!(
"Unknown error: {}",
response.status().to_string()
))))
}
}
let response_body: UserInfoResponse = response.json().await?;

View File

@ -25,8 +25,13 @@ async fn main() -> Result<()> {
let stdout = ui::prepare()?;
let base_url = args.base_url.unwrap();
let api = {
let mut login = ui::login::UI::new(stdout, base_url)?;
let api = login.run().await?;
api
};
println!("{:#?}", api);
Ok(())
}