v0.12.0-preety_code

This commit is contained in:
Mateusz Gruszczyński
2026-09-03 15:33:37 +02:00
parent be67932b47
commit 07be4b85d9
87 changed files with 11691 additions and 3172 deletions
+19 -6
View File
@@ -3,7 +3,9 @@ struct CreateAccessTokenRequest {
name: Option<String>,
}
async fn list_access_tokens(State(state): State<AppState>) -> Result<Json<Vec<ApiTokenInfo>>, AppError> {
async fn list_access_tokens(
State(state): State<AppState>,
) -> Result<Json<Vec<ApiTokenInfo>>, AppError> {
Ok(Json(state.db.list_api_tokens()?))
}
@@ -11,9 +13,15 @@ async fn create_access_token(
State(state): State<AppState>,
Json(input): Json<CreateAccessTokenRequest>,
) -> Result<(StatusCode, Json<Value>), AppError> {
let name = input.name.unwrap_or_else(|| "Home Assistant".into()).trim().to_string();
let name = input
.name
.unwrap_or_else(|| "Home Assistant".into())
.trim()
.to_string();
if name.is_empty() || name.len() > 80 {
return Err(AppError::BadRequest("token name must contain 1 to 80 characters".into()));
return Err(AppError::BadRequest(
"token name must contain 1 to 80 characters".into(),
));
}
let secret = generate_access_token();
@@ -30,10 +38,16 @@ async fn create_access_token(
"Created a Home Assistant access token",
json!({"token_id": item.id.clone(), "name": item.name.clone()}),
);
Ok((StatusCode::CREATED, Json(json!({"token": secret, "item": item}))))
Ok((
StatusCode::CREATED,
Json(json!({"token": secret, "item": item})),
))
}
async fn delete_access_token(State(state): State<AppState>, Path(id): Path<String>) -> Result<StatusCode, AppError> {
async fn delete_access_token(
State(state): State<AppState>,
Path(id): Path<String>,
) -> Result<StatusCode, AppError> {
if !state.db.delete_api_token(&id)? {
return Err(AppError::NotFound(format!("access token {id}")));
}
@@ -45,4 +59,3 @@ async fn delete_access_token(State(state): State<AppState>, Path(id): Path<Strin
);
Ok(StatusCode::NO_CONTENT)
}