1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! User API endpoints.
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;
// === Public endpoints (both authenticated and unauthenticated) ===
impl<S: AuthState> Client<S> {
/// Get a public user profile by slug.
///
/// Returns the public profile information for any user.
///
/// # Example
///
/// ```ignore
/// use wf_market::Client;
///
/// async fn example() -> wf_market::Result<()> {
/// let client = Client::builder().build().await?;
/// let user = client.get_user("some_user").await?;
///
/// println!("User: {} ({})", user.ingame_name, user.platform);
/// println!("Reputation: {}", user.reputation);
/// if user.is_available() {
/// println!("Available for trading!");
/// }
/// Ok(())
/// }
/// ```
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)
}
}
// === Authenticated endpoints ===
impl Client<Authenticated> {
/// Get the current user's private profile.
///
/// Returns the full private profile including settings and account info.
///
/// # Example
///
/// ```ignore
/// use wf_market::{Client, Credentials};
///
/// async fn example() -> wf_market::Result<()> {
/// let client = Client::from_credentials(/* ... */).await?;
///
/// let me = client.me().await?;
/// println!("Logged in as: {} ({})", me.ingame_name, me.email.as_deref().unwrap_or("no email"));
/// println!("Platform: {}, Crossplay: {}", me.platform, me.crossplay);
/// println!("Role: {:?}, Tier: {:?}", me.role, me.tier);
/// Ok(())
/// }
/// # fn main() {}
/// ```
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)
}
/// Update the current user's profile settings.
///
/// Only include the fields you want to change in the [`UpdateProfile`].
///
/// # Example
///
/// ```ignore
/// use wf_market::{Client, Credentials, UpdateProfile, Platform, Theme};
///
/// async fn example() -> wf_market::Result<()> {
/// let client = Client::from_credentials(/* ... */).await?;
///
/// // Update platform and enable crossplay
/// let updated = client.update_me(
/// UpdateProfile::new()
/// .platform(Platform::Pc)
/// .crossplay(true)
/// ).await?;
///
/// println!("Updated! Now on {} with crossplay: {}", updated.platform, updated.crossplay);
///
/// // Update theme
/// let updated = client.update_me(
/// UpdateProfile::new().theme(Theme::Dark)
/// ).await?;
///
/// Ok(())
/// }
/// # fn main() {}
/// ```
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)
}
}