wf-market 0.3.0

A Rust client library for the warframe.market API
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! WebSocket client implementation.

use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;

use futures_util::{SinkExt, StreamExt};
use tokio::sync::{RwLock, mpsc};
use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;

use crate::error::{Error, Result, WsError};
use crate::models::OrderListing;

use super::builder::WebSocketBuilder;
use super::events::{Activity, UserStatus, WsEvent};
use super::message::{ParsedRoute, WsMessage};
use super::subscription::Subscription;
use super::{WS_PROTOCOL, WS_URL};

/// Active WebSocket connection.
///
/// Provides methods for subscribing to events and sending commands.
pub struct WebSocket {
    sender: mpsc::UnboundedSender<WsMessage>,
    subscriptions: Arc<RwLock<HashSet<Subscription>>>,
}

impl WebSocket {
    /// Connect to the WebSocket server.
    pub(crate) async fn connect(builder: WebSocketBuilder<'_>) -> Result<Self> {
        let token = builder.client.token().to_string();
        let event_handler = builder.event_handler;
        let initial_subscriptions = builder.subscriptions;
        let auto_reconnect = builder.auto_reconnect;
        let reconnect_delay = builder.reconnect_delay;
        let user_agent = builder.user_agent;

        let (msg_tx, msg_rx) = mpsc::unbounded_channel::<WsMessage>();
        let subscriptions = Arc::new(RwLock::new(HashSet::new()));
        let subscriptions_clone = subscriptions.clone();

        // Spawn the connection task
        tokio::spawn(async move {
            // Wrap in Option to allow taking/replacing across reconnections
            let mut msg_rx = Some(msg_rx);

            loop {
                // Build request with protocol
                let mut request = WS_URL.into_client_request().expect("Invalid WebSocket URL");
                let headers = request.headers_mut();
                headers.insert("Sec-WebSocket-Protocol", WS_PROTOCOL.parse().unwrap());
                headers.insert("User-Agent", user_agent.parse().unwrap());

                match connect_async(request).await {
                    Ok((ws_stream, _)) => {
                        let (mut write, mut read) = ws_stream.split();

                        // Emit connected event
                        if let Some(ref handler) = event_handler {
                            handler(WsEvent::Connected).await;
                        }

                        // Send authentication
                        let auth_msg = WsMessage::sign_in(&token);
                        let auth_json = serde_json::to_string(&auth_msg).unwrap();
                        if write.send(Message::Text(auth_json.into())).await.is_err() {
                            if let Some(ref handler) = event_handler {
                                handler(WsEvent::Disconnected {
                                    reason: "Failed to send auth".to_string(),
                                })
                                .await;
                            }
                            if !auto_reconnect {
                                break;
                            }
                            tokio::time::sleep(reconnect_delay).await;
                            continue;
                        }

                        // Create a channel for sending messages from this task
                        let (internal_tx, mut internal_rx) = mpsc::unbounded_channel::<WsMessage>();

                        // Take the receiver for the write task
                        let msg_rx_for_write = match msg_rx.take() {
                            Some(rx) => rx,
                            None => {
                                // This should never happen, but handle gracefully
                                if let Some(ref handler) = event_handler {
                                    handler(WsEvent::Disconnected {
                                        reason: "Internal error: message receiver lost".to_string(),
                                    })
                                    .await;
                                }
                                break;
                            }
                        };

                        // Write task - wraps receiver in Option for cancellation recovery
                        let write_task = tokio::spawn(async move {
                            let mut rx = msg_rx_for_write;
                            loop {
                                tokio::select! {
                                    Some(msg) = internal_rx.recv() => {
                                        let json = serde_json::to_string(&msg).unwrap();
                                        if write.send(Message::Text(json.into())).await.is_err() {
                                            break;
                                        }
                                    }
                                    Some(msg) = rx.recv() => {
                                        let json = serde_json::to_string(&msg).unwrap();
                                        if write.send(Message::Text(json.into())).await.is_err() {
                                            break;
                                        }
                                    }
                                    else => break,
                                }
                            }
                            rx
                        });

                        // Read task
                        let event_handler_clone = event_handler.clone();
                        let subscriptions_for_read = subscriptions_clone.clone();
                        let initial_subs = initial_subscriptions.clone();
                        let internal_tx_clone = internal_tx.clone();

                        let read_task = tokio::spawn(async move {
                            let mut authenticated = false;

                            while let Some(msg_result) = read.next().await {
                                match msg_result {
                                    Ok(Message::Text(text)) => {
                                        if let Ok(msg) = serde_json::from_str::<WsMessage>(&text) {
                                            let event = Self::parse_event(&msg);

                                            // Handle authentication success
                                            if matches!(event, WsEvent::Authenticated)
                                                && !authenticated
                                            {
                                                authenticated = true;

                                                // Register initial subscriptions
                                                for sub in &initial_subs {
                                                    let sub_msg = WsMessage::new(
                                                        sub.subscribe_route(),
                                                        sub.to_payload(),
                                                    );
                                                    let _ = internal_tx_clone.send(sub_msg);
                                                    subscriptions_for_read
                                                        .write()
                                                        .await
                                                        .insert(sub.clone());
                                                }
                                            }

                                            // Emit event to handler
                                            if let Some(ref handler) = event_handler_clone {
                                                handler(event).await;
                                            }
                                        }
                                    }
                                    Ok(Message::Close(_)) => {
                                        if let Some(ref handler) = event_handler_clone {
                                            handler(WsEvent::Disconnected {
                                                reason: "Connection closed by server".to_string(),
                                            })
                                            .await;
                                        }
                                        break;
                                    }
                                    Err(e) => {
                                        if let Some(ref handler) = event_handler_clone {
                                            handler(WsEvent::Disconnected {
                                                reason: format!("Read error: {}", e),
                                            })
                                            .await;
                                        }
                                        break;
                                    }
                                    _ => {}
                                }
                            }
                        });

                        // Wait for either task to complete
                        let recovered_rx = tokio::select! {
                            result = write_task => {
                                // Write task finished - get receiver back
                                result.ok()
                            }
                            _ = read_task => {
                                // Read task finished first - receiver is consumed by write task
                                // which is now orphaned. We cannot recover it, so return None.
                                None
                            }
                        };

                        msg_rx = recovered_rx;

                        // If msg_rx is None, the receiver is lost. The connection is broken
                        // and the sender will error on next send attempt.
                        if msg_rx.is_none() {
                            break;
                        }

                        if let Some(ref handler) = event_handler {
                            handler(WsEvent::Disconnected {
                                reason: "Connection lost".to_string(),
                            })
                            .await;
                        }
                    }
                    Err(e) => {
                        if let Some(ref handler) = event_handler {
                            handler(WsEvent::Disconnected {
                                reason: format!("Connection failed: {}", e),
                            })
                            .await;
                        }
                    }
                }

                if !auto_reconnect {
                    break;
                }

                tokio::time::sleep(reconnect_delay).await;
            }
        });

        // Wait a bit for initial connection
        tokio::time::sleep(Duration::from_millis(100)).await;

        Ok(Self {
            sender: msg_tx,
            subscriptions,
        })
    }

    /// Parse a WebSocket message into an event.
    ///
    /// Routes follow the format: `@wfm|type/path` or `@wfm|type/path:parameter`
    /// Examples from the API:
    /// - `@wfm|cmd/auth/signIn:ok` -> Authenticated
    /// - `@wfm|event/reports/online` -> OnlineCount
    /// - `@wfm|event/status/set` -> StatusUpdate
    /// - `@wfm|event/subscriptions/newOrder` -> OrderCreated
    fn parse_event(msg: &WsMessage) -> WsEvent {
        let route = match ParsedRoute::parse(&msg.route) {
            Some(r) => r,
            None => {
                return WsEvent::Unknown {
                    route: msg.route.clone(),
                    payload: msg.payload.clone().unwrap_or_default(),
                };
            }
        };

        // Match on (msg_type, path) from the parsed route
        match (route.msg_type.as_str(), route.path.as_str()) {
            // === Command responses ===

            // Authentication response: @wfm|cmd/auth/signIn:ok or @wfm|cmd/auth/signIn:error
            ("cmd", "auth/signIn") => match route.parameter.as_deref() {
                Some("ok") => WsEvent::Authenticated,
                Some("error") => WsEvent::AuthenticationFailed {
                    error: msg
                        .payload
                        .as_ref()
                        .and_then(|p| p["error"].as_str())
                        .unwrap_or("Unknown error")
                        .to_string(),
                },
                _ => WsEvent::Unknown {
                    route: msg.route.clone(),
                    payload: msg.payload.clone().unwrap_or_default(),
                },
            },

            // === Events ===

            // Online count: @wfm|event/reports/online
            // Payload: {"connections": 79086, "authorizedUsers": 46183}
            ("event", "reports/online") => {
                if let Some(ref payload) = msg.payload {
                    let connections = payload["connections"].as_u64().unwrap_or(0);
                    // Note: API uses "authorizedUsers" not "authorized"
                    let authorized = payload["authorizedUsers"].as_u64().unwrap_or(0);
                    WsEvent::OnlineCount {
                        connections,
                        authorized,
                    }
                } else {
                    WsEvent::OnlineCount {
                        connections: 0,
                        authorized: 0,
                    }
                }
            }

            // Status update: @wfm|event/status/set
            // Payload: {"status": "invisible", "statusSetAt": "2026-01-10T20:16:19Z"}
            ("event", "status/set") => {
                if let Some(ref payload) = msg.payload {
                    let status: UserStatus =
                        serde_json::from_value(payload["status"].clone()).unwrap_or_default();

                    // API sends "statusSetAt" - timestamp when status was set
                    // Note: Field is named status_until for backwards compatibility,
                    // but semantically represents when the status was set, not when it expires
                    let status_until = payload["statusSetAt"]
                        .as_str()
                        .and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
                        .map(|dt| dt.with_timezone(&chrono::Utc));

                    let activity: Option<Activity> =
                        serde_json::from_value(payload["activity"].clone()).ok();

                    WsEvent::StatusUpdate {
                        status,
                        status_until,
                        activity,
                    }
                } else {
                    WsEvent::StatusUpdate {
                        status: UserStatus::default(),
                        status_until: None,
                        activity: None,
                    }
                }
            }

            // New order from subscription: @wfm|event/subscriptions/newOrder
            ("event", "subscriptions/newOrder") => {
                if let Some(ref payload) = msg.payload {
                    if let Ok(order) = serde_json::from_value::<OrderListing>(payload.clone()) {
                        return WsEvent::OrderCreated { order };
                    }
                }
                WsEvent::Unknown {
                    route: msg.route.clone(),
                    payload: msg.payload.clone().unwrap_or_default(),
                }
            }

            // Order updated: @wfm|event/subscriptions/orderUpdated
            ("event", "subscriptions/orderUpdated") => {
                if let Some(ref payload) = msg.payload {
                    if let Ok(order) = serde_json::from_value::<OrderListing>(payload.clone()) {
                        return WsEvent::OrderUpdated { order };
                    }
                }
                WsEvent::Unknown {
                    route: msg.route.clone(),
                    payload: msg.payload.clone().unwrap_or_default(),
                }
            }

            // Order removed: @wfm|event/subscriptions/orderRemoved
            ("event", "subscriptions/orderRemoved") => {
                if let Some(ref payload) = msg.payload {
                    let order_id = payload["id"]
                        .as_str()
                        .or_else(|| payload["orderId"].as_str())
                        .unwrap_or("")
                        .to_string();
                    WsEvent::OrderRemoved { order_id }
                } else {
                    WsEvent::OrderRemoved {
                        order_id: String::new(),
                    }
                }
            }

            // Session revoked: @wfm|event/auth/revoke
            ("event", "auth/revoke") => WsEvent::SessionRevoked,

            // Verification update: @wfm|event/user/verificationUpdate
            ("event", "user/verificationUpdate") => {
                if let Some(ref payload) = msg.payload {
                    WsEvent::VerificationUpdate {
                        ingame_name: payload["ingameName"].as_str().unwrap_or("").to_string(),
                        verified: payload["verified"].as_bool().unwrap_or(false),
                    }
                } else {
                    WsEvent::VerificationUpdate {
                        ingame_name: String::new(),
                        verified: false,
                    }
                }
            }

            // Ban event: @wfm|event/user/banned
            ("event", "user/banned") => {
                if let Some(ref payload) = msg.payload {
                    let until = payload["banUntil"]
                        .as_str()
                        .or_else(|| payload["until"].as_str())
                        .and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
                        .map(|dt| dt.with_timezone(&chrono::Utc))
                        .unwrap_or_else(chrono::Utc::now);

                    WsEvent::Banned {
                        until,
                        message: payload["banMessage"]
                            .as_str()
                            .or_else(|| payload["message"].as_str())
                            .unwrap_or("")
                            .to_string(),
                    }
                } else {
                    WsEvent::Banned {
                        until: chrono::Utc::now(),
                        message: String::new(),
                    }
                }
            }

            // Warning event: @wfm|event/user/warned
            ("event", "user/warned") => WsEvent::Warned {
                message: msg
                    .payload
                    .as_ref()
                    .and_then(|p| p["warnMessage"].as_str().or_else(|| p["message"].as_str()))
                    .unwrap_or("")
                    .to_string(),
            },

            // Ban lifted: @wfm|event/user/banLifted
            ("event", "user/banLifted") => WsEvent::BanLifted,

            // Warning lifted: @wfm|event/user/warningLifted
            ("event", "user/warningLifted") => WsEvent::WarningLifted,

            // Fallback for unknown events
            _ => WsEvent::Unknown {
                route: msg.route.clone(),
                payload: msg.payload.clone().unwrap_or_default(),
            },
        }
    }

    /// Subscribe to real-time updates.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use wf_market::ws::Subscription;
    /// # use wf_market::ws::WebSocket;
    /// # async fn example(ws: &WebSocket) -> wf_market::Result<()> {
    ///
    /// ws.subscribe(Subscription::item("mesa_prime_set")).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn subscribe(&self, subscription: Subscription) -> Result<()> {
        let msg = WsMessage::new(subscription.subscribe_route(), subscription.to_payload());

        self.sender
            .send(msg)
            .map_err(|e| Error::WebSocket(WsError::SendFailed(e.to_string())))?;

        self.subscriptions.write().await.insert(subscription);
        Ok(())
    }

    /// Unsubscribe from updates.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use wf_market::ws::Subscription;
    /// # use wf_market::ws::WebSocket;
    /// # async fn example(ws: &WebSocket) -> wf_market::Result<()> {
    ///
    /// let sub = Subscription::item("mesa_prime_set");
    /// ws.unsubscribe(&sub).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn unsubscribe(&self, subscription: &Subscription) -> Result<()> {
        let msg = WsMessage::new(subscription.unsubscribe_route(), subscription.to_payload());

        self.sender
            .send(msg)
            .map_err(|e| Error::WebSocket(WsError::SendFailed(e.to_string())))?;

        self.subscriptions.write().await.remove(subscription);
        Ok(())
    }

    /// Set user status.
    ///
    /// # Arguments
    ///
    /// * `status` - New status (online, ingame, invisible)
    /// * `duration` - Optional duration in seconds (60-21600)
    /// * `activity` - Optional in-game activity
    ///
    /// # Example
    ///
    /// ```ignore
    /// use wf_market::ws::{UserStatus, Activity, ActivityType};
    /// # use wf_market::ws::WebSocket;
    /// # async fn example(ws: &WebSocket) -> wf_market::Result<()> {
    ///
    /// ws.set_status(
    ///     UserStatus::Ingame,
    ///     Some(3600), // 1 hour
    ///     Some(Activity::new(ActivityType::OnMission).with_details("Index")),
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_status(
        &self,
        status: UserStatus,
        duration: Option<u64>,
        activity: Option<Activity>,
    ) -> Result<()> {
        let status_str = match status {
            UserStatus::Online => "online",
            UserStatus::Ingame => "ingame",
            UserStatus::Invisible => "invisible",
            UserStatus::Offline => "offline",
        };

        let activity_json = activity.map(|a| serde_json::to_value(a).unwrap());
        let msg = WsMessage::set_status(status_str, duration, activity_json);

        self.sender
            .send(msg)
            .map_err(|e| Error::WebSocket(WsError::SendFailed(e.to_string())))?;

        Ok(())
    }

    /// Sign out from the WebSocket (but keep connection open).
    pub async fn sign_out(&self) -> Result<()> {
        let msg = WsMessage::sign_out();

        self.sender
            .send(msg)
            .map_err(|e| Error::WebSocket(WsError::SendFailed(e.to_string())))?;

        Ok(())
    }

    /// Get currently active subscriptions.
    pub async fn subscriptions(&self) -> Vec<Subscription> {
        self.subscriptions.read().await.iter().cloned().collect()
    }

    /// Send a raw message (for advanced/undocumented endpoints).
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use wf_market::ws::WebSocket;
    /// # async fn example(ws: &WebSocket) -> wf_market::Result<()> {
    /// ws.send_raw("@custom/route", serde_json::json!({ "key": "value" }))?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn send_raw(&self, route: &str, payload: serde_json::Value) -> Result<()> {
        let msg = WsMessage::new(route, payload);

        self.sender
            .send(msg)
            .map_err(|e| Error::WebSocket(WsError::SendFailed(e.to_string())))?;

        Ok(())
    }
}