use crate::connection::MDRSConnection; use crate::models::user::User as ModelUser; use serde::Deserialize; use anyhow::{bail}; /// Full API response shape from GET v3/users/current/ #[derive(Debug, Deserialize)] struct UsersApiCurrentResponse { id: u32, username: String, laboratories: Vec, is_reviewer: bool, } #[derive(Debug, Deserialize)] struct UsersCurrentResponseLaboratory { id: u32, } #[derive(Deserialize)] struct TokenRefreshResponse { access: String, } impl MDRSConnection { /// Fetch current user and return the slim 4-field model matching the Python cache format. pub async fn get_current_user(&self) -> Result { let resp = self.get("v3/users/current/").await?; let obj = resp.json::().await?; let laboratory_ids = obj.laboratories.into_iter().map(|l| l.id).collect(); Ok(ModelUser { id: obj.id, username: obj.username, laboratory_ids, is_reviewer: obj.is_reviewer, }) } /// Refresh the access token using the refresh token. /// POST v3/users/token/refresh/ {refresh: ...} -> {access: new_access} pub async fn token_refresh( &self, refresh_token: &str, ) -> Result { let body = serde_json::json!({ "refresh": refresh_token }); let resp = self .client .post(self.build_url("v3/users/token/refresh/")) .json(&body) .send() .await?; if !resp.status().is_success() { bail!("Token refresh failed: {}", resp.status()); } let r: TokenRefreshResponse = resp.json().await?; Ok(r.access) } }