Added auth
This commit is contained in:
@@ -55,6 +55,49 @@ async fn delete(url: &str) -> Result<(), ApiError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// --- Auth ---
|
||||
pub async fn check_setup_required() -> Result<SetupRequired, ApiError> {
|
||||
get_json(&format!("{BASE}/auth/setup-required")).await
|
||||
}
|
||||
|
||||
pub async fn setup(username: &str, password: &str) -> Result<UserInfo, ApiError> {
|
||||
let body = serde_json::json!({"username": username, "password": password}).to_string();
|
||||
post_json(&format!("{BASE}/auth/setup"), &body).await
|
||||
}
|
||||
|
||||
pub async fn login(username: &str, password: &str) -> Result<UserInfo, ApiError> {
|
||||
let body = serde_json::json!({"username": username, "password": password}).to_string();
|
||||
post_json(&format!("{BASE}/auth/login"), &body).await
|
||||
}
|
||||
|
||||
pub async fn logout() -> Result<(), ApiError> {
|
||||
let resp = Request::post(&format!("{BASE}/auth/logout"))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| ApiError(e.to_string()))?;
|
||||
if !resp.ok() {
|
||||
return Err(ApiError(format!("HTTP {}", resp.status())));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_me() -> Result<UserInfo, ApiError> {
|
||||
get_json(&format!("{BASE}/auth/me")).await
|
||||
}
|
||||
|
||||
pub async fn list_users() -> Result<Vec<UserInfo>, ApiError> {
|
||||
get_json(&format!("{BASE}/auth/users")).await
|
||||
}
|
||||
|
||||
pub async fn create_user(username: &str, password: &str) -> Result<UserInfo, ApiError> {
|
||||
let body = serde_json::json!({"username": username, "password": password}).to_string();
|
||||
post_json(&format!("{BASE}/auth/users"), &body).await
|
||||
}
|
||||
|
||||
pub async fn delete_user(id: i32) -> Result<(), ApiError> {
|
||||
delete(&format!("{BASE}/auth/users/{id}")).await
|
||||
}
|
||||
|
||||
// --- Status ---
|
||||
pub async fn get_status() -> Result<Status, ApiError> {
|
||||
get_json(&format!("{BASE}/status")).await
|
||||
|
||||
Reference in New Issue
Block a user