use crate::client::{AuthState, Authenticated, Client};
use crate::error::{ApiErrorResponse, Error, Result};
use crate::internal::BASE_URL;
use crate::models::{UpdateProfile, UserPrivate, UserProfile};
use super::ApiResponse;
impl<S: AuthState> Client<S> {
pub async fn get_user(&self, slug: &str) -> Result<UserProfile> {
self.wait_for_rate_limit().await;
let response = self
.http
.get(format!("{}/user/{}", BASE_URL, slug))
.send()
.await
.map_err(Error::Network)?;
let status = response.status();
if status == reqwest::StatusCode::NOT_FOUND {
return Err(Error::not_found(format!("User not found: {}", slug)));
}
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
if let Ok(error_response) = serde_json::from_str::<ApiErrorResponse>(&body) {
return Err(Error::api_with_response(
status,
format!("Failed to fetch user: {}", slug),
error_response,
));
}
return Err(Error::api(
status,
format!("Failed to fetch user {}: {}", slug, body),
));
}
let body = response.text().await.map_err(Error::Network)?;
let api_response: ApiResponse<UserProfile> =
serde_json::from_str(&body).map_err(|e| Error::parse_with_body(e.to_string(), body))?;
Ok(api_response.data)
}
}
impl Client<Authenticated> {
pub async fn me(&self) -> Result<UserPrivate> {
self.wait_for_rate_limit().await;
let response = self
.http
.get(format!("{}/me", BASE_URL))
.send()
.await
.map_err(Error::Network)?;
let status = response.status();
if status == reqwest::StatusCode::UNAUTHORIZED {
return Err(Error::auth("Session expired or invalid"));
}
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
if let Ok(error_response) = serde_json::from_str::<ApiErrorResponse>(&body) {
return Err(Error::api_with_response(
status,
"Failed to fetch current user profile",
error_response,
));
}
return Err(Error::api(
status,
format!("Failed to fetch current user: {}", body),
));
}
let body = response.text().await.map_err(Error::Network)?;
let api_response: ApiResponse<UserPrivate> =
serde_json::from_str(&body).map_err(|e| Error::parse_with_body(e.to_string(), body))?;
Ok(api_response.data)
}
pub async fn update_me(&self, update: UpdateProfile) -> Result<UserPrivate> {
self.wait_for_rate_limit().await;
let response = self
.http
.patch(format!("{}/me", BASE_URL))
.json(&update)
.send()
.await
.map_err(Error::Network)?;
let status = response.status();
if status == reqwest::StatusCode::UNAUTHORIZED {
return Err(Error::auth("Session expired or invalid"));
}
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
if let Ok(error_response) = serde_json::from_str::<ApiErrorResponse>(&body) {
return Err(Error::api_with_response(
status,
"Failed to update profile",
error_response,
));
}
return Err(Error::api(
status,
format!("Failed to update profile: {}", body),
));
}
let body = response.text().await.map_err(Error::Network)?;
let api_response: ApiResponse<UserPrivate> =
serde_json::from_str(&body).map_err(|e| Error::parse_with_body(e.to_string(), body))?;
Ok(api_response.data)
}
}