first commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
use crate::connection::MDRSConnection;
|
||||
use crate::models::user::User as ModelUser;
|
||||
use serde::Deserialize;
|
||||
|
||||
/// Full API response shape from GET v3/users/current/
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct UsersApiCurrentResponse {
|
||||
id: u32,
|
||||
username: String,
|
||||
laboratories: Vec<UsersCurrentResponseLaboratory>,
|
||||
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<ModelUser, reqwest::Error> {
|
||||
let resp = self.get("v3/users/current/").await?;
|
||||
let obj = resp.json::<UsersApiCurrentResponse>().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<String, Box<dyn std::error::Error>> {
|
||||
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() {
|
||||
return Err(format!("Token refresh failed: {}", resp.status()).into());
|
||||
}
|
||||
let r: TokenRefreshResponse = resp.json().await?;
|
||||
Ok(r.access)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user