roboat 0.39.0

A high performance interface for the Roblox 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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use crate::{Client, Limit, RoboatError, User};
use reqwest::header;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

mod request_types;

const TRADES_API: &str = "https://trades.roblox.com/v1/trades/";
const TRADE_DETAILS_API: &str = "https://trades.roblox.com/v1/trades/{trade_id}";
const DECLINE_TRADE_API: &str = "https://trades.roblox.com/v1/trades/{trade_id}/decline";
const SEND_TRADE_API: &str = "https://trades.roblox.com/v1/trades/send";
const ACCEPT_TRADE_API: &str = "https://trades.roblox.com/v1/trades/{trade_id}/accept";
const TRADE_COUNT_API: &str = "https://trades.roblox.com/v1/trades/inbound/count";

/// For requests related to trades, we use Descending as the sort order.
/// This is because there is hardly any use case for using a reverse sort order for trades.
const SORT_ORDER: &str = "Desc";

/// The type of the trade you want to request (Inbound, Outbound, Completed, Inactive).
#[derive(
    Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize, Copy,
)]
#[allow(missing_docs)]
pub enum TradeType {
    Inbound,
    Outbound,
    Completed,
    #[default]
    Inactive,
}

/// The details of a Roblox trade.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize)]
pub struct Trade {
    /// The id of the trade. Used for accepting, declining, ... trades.
    pub trade_id: u64,
    /// The details of the person you're trading with.
    pub partner: User,
    /// Whether one of the parties can still act on the trade.
    pub is_active: bool,
    /// The status of the trade.
    pub status: TradeStatus,
}

/// The status of a Roblox trade. [`Self::Open`] is the status for both
/// inbound and outbound trades.
#[derive(
    Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize, Copy,
)]
#[allow(missing_docs)]
pub enum TradeStatus {
    Open,
    Completed,
    Declined,
    #[default]
    Expired,
    RejectedDueToError,
}

impl std::str::FromStr for TradeStatus {
    type Err = RoboatError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Open" => Ok(Self::Open),
            "Completed" => Ok(Self::Completed),
            "Declined" => Ok(Self::Declined),
            "Expired" => Ok(Self::Expired),
            "RejectedDueToError" => Ok(Self::RejectedDueToError),
            _ => Err(RoboatError::MalformedResponse),
        }
    }
}

/// The details of a trade.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize)]
pub struct TradeDetails {
    /// Your partner in the trade deal.
    pub partner: User,
    /// The items you're offering.
    pub your_items: Vec<TradeItem>,
    /// The amount of robux you're offering.
    pub your_robux: u64,
    /// The items your partner is offering.
    pub partner_items: Vec<TradeItem>,
    /// The amount of robux your partner is offering.
    pub partner_robux: u64,
    /// The creation time of the trade in ISO 8601 format.
    pub created: String,
    /// The expiration time of the trade in ISO 8601 format.
    pub expiration: Option<String>,
    /// Whether one of the parties can still act on the trade.
    pub is_active: bool,
    /// The status of the trade.
    pub status: TradeStatus,
}

/// The details of an item in a trade. This is separate from other item structs
#[allow(missing_docs)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize)]
pub struct TradeItem {
    pub item_id: u64,
    /// The serial number of the item. Only exists for limited Us.
    pub serial_number: Option<u64>,
    /// The unique asset id of the item. This is the only item with this uaid.
    pub uaid: u64,
    pub name: String,
    /// The recent average price of the item.
    pub rap: u64,
}

impl Client {
    /// Returns a list of trades using the endpoint <https://trades.roblox.com/v1/{trade_type}>.
    ///
    /// # Notes
    /// * Requires a valid roblosecurity.
    /// * Trades are ordered newest to oldest.
    ///
    /// # Errors
    /// * All errors under [Standard Errors](#standard-errors).
    /// * All errors under [Auth Required Errors](#auth-required-errors).
    ///
    /// # Example
    /// ```no_run
    /// use roboat::ClientBuilder;
    /// use roboat::trades::TradeType;
    /// use roboat::Limit;
    ///
    /// const ROBLOSECURITY: &str = "roblosecurity";
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = ClientBuilder::new().roblosecurity(ROBLOSECURITY.to_string()).build();
    ///
    /// let trade_type = TradeType::Inbound;
    /// let limit = Limit::Ten;
    /// let cursor = None;
    ///
    /// let (trades, next_cursor) = client.trades(trade_type, limit, cursor).await?;
    ///
    /// println!("Inbound Trade #1 Partner: {}", trades[0].partner.username);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn trades(
        &self,
        trade_type: TradeType,
        limit: Limit,
        cursor: Option<String>,
    ) -> Result<(Vec<Trade>, Option<String>), RoboatError> {
        let limit = limit.to_u64();
        let cursor = cursor.unwrap_or_default();

        let cookie_string = self.cookie_string()?;

        let trade_type_str = match trade_type {
            TradeType::Inbound => "inbound",
            TradeType::Outbound => "outbound",
            TradeType::Completed => "completed",
            TradeType::Inactive => "inactive",
        };

        let formatted_url = format!(
            "{}{}?sortOrder={}&cursor={}&limit={}",
            TRADES_API, trade_type_str, SORT_ORDER, cursor, limit
        );

        let request_result = self
            .reqwest_client
            .get(&formatted_url)
            .header(header::COOKIE, cookie_string)
            .send()
            .await;

        let response = Self::validate_request_result(request_result).await?;
        let raw = Self::parse_to_raw::<request_types::InboundTradesResponse>(response).await?;

        let next_cursor = raw.next_page_cursor;

        let mut trades = Vec::new();

        for trade in raw.data {
            let partner = User {
                user_id: trade.user.id as u64,
                username: trade.user.name,
                display_name: trade.user.display_name,
            };

            let trade = Trade {
                trade_id: trade.id as u64,
                partner,
                is_active: trade.is_active,
                status: trade.status,
            };

            trades.push(trade);
        }

        Ok((trades, next_cursor))
    }

    /// Returns the details of a trade using <https://trades.roblox.com/v1/trades/{trade_id}>.
    ///
    /// # Notes
    /// * Requires a valid roblosecurity.
    ///
    /// # Errors
    /// * All errors under [Standard Errors](#standard-errors).
    /// * All errors under [Auth Required Errors](#auth-required-errors).
    ///
    /// # Example
    /// ```no_run
    /// use roboat::ClientBuilder;
    ///
    /// const ROBLOSECURITY: &str = "roblosecurity";
    /// const TRADE_ID: u64 = 123456789;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = ClientBuilder::new().roblosecurity(ROBLOSECURITY.to_string()).build();
    ///
    /// let trade_details = client.trade_details(TRADE_ID).await?;
    ///
    /// println!("Trade Details: {:#?}", trade_details);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn trade_details(&self, trade_id: u64) -> Result<TradeDetails, RoboatError> {
        let formatted_url = TRADE_DETAILS_API.replace("{trade_id}", &trade_id.to_string());
        let cookie_string = self.cookie_string()?;

        let response_result = self
            .reqwest_client
            .get(&formatted_url)
            .header(header::COOKIE, cookie_string)
            .send()
            .await;

        let response = Self::validate_request_result(response_result).await?;
        let raw = Self::parse_to_raw::<request_types::TradeDetailsResponse>(response).await?;

        let partner = User {
            user_id: raw.offers[1].user.id as u64,
            username: raw.offers[1].user.name.clone(),
            display_name: raw.offers[1].user.display_name.clone(),
        };

        let mut your_items: Vec<TradeItem> = Vec::new();

        for item in &raw.offers[0].user_assets {
            let trade_item = TradeItem {
                item_id: item.asset_id as u64,
                serial_number: item.serial_number.map(|x| x as u64),
                uaid: item.id as u64,
                name: item.name.clone(),
                rap: item.recent_average_price as u64,
            };

            your_items.push(trade_item);
        }

        let mut partner_items: Vec<TradeItem> = Vec::new();

        for item in &raw.offers[1].user_assets {
            let trade_item = TradeItem {
                item_id: item.asset_id as u64,
                serial_number: item.serial_number.map(|x| x as u64),
                uaid: item.id as u64,
                name: item.name.clone(),
                rap: item.recent_average_price as u64,
            };

            partner_items.push(trade_item);
        }

        let your_robux = raw.offers[0].robux as u64;
        let partner_robux = raw.offers[1].robux as u64;

        let created = raw.created;
        let expiration = raw.expiration;
        let is_active = raw.is_active;

        let trade_status = TradeStatus::from_str(&raw.status)?;

        let trade_details = TradeDetails {
            partner,
            your_items,
            partner_items,
            your_robux,
            partner_robux,
            created,
            expiration,
            is_active,
            status: trade_status,
        };

        Ok(trade_details)
    }

    /// Declines a trade using <https://trades.roblox.com/v1/trades/{trade_id}/decline>.
    ///
    /// # Notes
    /// * Requires a valid roblosecurity.
    /// * Will repeat once if the x-csrf-token is invalid.
    ///
    /// # Errors
    /// * All errors under [Standard Errors](#standard-errors).
    /// * All errors under [Auth Required Errors](#auth-required-errors).
    /// * All errors under [X-CSRF-TOKEN Required Errors](#x-csrf-token-required-errors).
    ///
    /// # Example
    /// ```no_run
    /// use roboat::ClientBuilder;
    ///
    /// const ROBLOSECURITY: &str = "roblosecurity";
    /// const TRADE_ID: u64 = 123456789;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = ClientBuilder::new().roblosecurity(ROBLOSECURITY.to_string()).build();
    ///
    /// client.decline_trade(TRADE_ID).await?;
    ///
    /// println!("Declined trade {}", TRADE_ID);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn decline_trade(&self, trade_id: u64) -> Result<(), RoboatError> {
        match self.decline_trade_internal(trade_id).await {
            Ok(x) => Ok(x),
            Err(e) => match e {
                RoboatError::InvalidXcsrf(new_xcsrf) => {
                    self.set_xcsrf(new_xcsrf).await;

                    self.decline_trade_internal(trade_id).await
                }
                _ => Err(e),
            },
        }
    }

    /// your_robux and partner robux is before tax
    ///
    /// /// Declines a trade using <https://trades.roblox.com/v1/trades/{trade_id}/decline>.
    ///
    /// # Notes
    /// * Requires a valid roblosecurity.
    /// * Will repeat once if the x-csrf-token is invalid.
    ///
    /// # Argument Notes
    /// * `your_robux` and `partner` is before 30% tax.
    /// * Uaids are NOT item/asset ids. They are unique ids for each item.
    ///
    /// # Return Notes
    /// * The value returned on success is the trade id.
    ///
    /// # Errors
    /// * All errors under [Standard Errors](#standard-errors).
    /// * All errors under [Auth Required Errors](#auth-required-errors).
    /// * All errors under [X-CSRF-TOKEN Required Errors](#x-csrf-token-required-errors).
    ///
    /// # Example
    /// ```no_run
    /// use roboat::ClientBuilder;
    ///
    /// const ROBLOSECURITY: &str = "roblosecurity";
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = ClientBuilder::new().roblosecurity(ROBLOSECURITY.to_string()).build();
    ///
    /// let partner_id = 12345;
    /// let your_uaids = vec![123, 456];
    /// let your_robux = 100;
    /// let partner_uaids = vec![321, 654];
    /// let partner_robux = 0;
    ///
    /// let trade_id = client
    ///     .send_trade(
    ///         partner_id,
    ///         your_uaids,
    ///         your_robux,
    ///         partner_uaids,
    ///         partner_robux,
    ///     )
    ///     .await?;
    ///
    /// println!("Sent Trade! Trade ID: {}", trade_id);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send_trade(
        &self,
        partner_id: u64,
        your_item_uaids: Vec<u64>,
        your_robux: u64,
        partner_item_uaids: Vec<u64>,
        partner_robux: u64,
    ) -> Result<u64, RoboatError> {
        match self
            .send_trade_internal(
                partner_id,
                your_item_uaids.clone(),
                your_robux,
                partner_item_uaids.clone(),
                partner_robux,
            )
            .await
        {
            Ok(x) => Ok(x),
            Err(e) => match e {
                RoboatError::InvalidXcsrf(new_xcsrf) => {
                    self.set_xcsrf(new_xcsrf).await;

                    self.send_trade_internal(
                        partner_id,
                        your_item_uaids,
                        your_robux,
                        partner_item_uaids,
                        partner_robux,
                    )
                    .await
                }
                _ => Err(e),
            },
        }
    }

    /* pub async fn send_trade_two_step(
        &self,
        partner_id: u64,
        your_item_uaids: Vec<u64>,
        your_robux: u64,
        partner_item_uaids: Vec<u64>,
        partner_robux: u64,
        two_step_solution: TradeTwoStepSolution,
    ) -> Result<u64, RoboatError> {
        todo!()
    } */

    /// Accepts a trade using <https://trades.roblox.com/v1/trades/{trade_id}/accept>.
    ///
    /// # Notes
    /// * Requires a valid roblosecurity.
    /// * Will repeat once if the x-csrf-token is invalid.
    ///
    /// # Errors
    /// * All errors under [Standard Errors](#standard-errors).
    /// * All errors under [Auth Required Errors](#auth-required-errors).
    /// * All errors under [X-CSRF-TOKEN Required Errors](#x-csrf-token-required-errors).
    ///
    /// # Example
    /// ```no_run
    /// use roboat::ClientBuilder;
    ///
    /// const ROBLOSECURITY: &str = "roblosecurity";
    /// const TRADE_ID: u64 = 123456789;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = ClientBuilder::new().roblosecurity(ROBLOSECURITY.to_string()).build();
    ///
    /// client.accept_trade(TRADE_ID).await?;
    ///
    /// println!("Accepted trade {}", TRADE_ID);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn accept_trade(&self, trade_id: u64) -> Result<(), RoboatError> {
        match self.accept_trade_internal(trade_id).await {
            Ok(x) => Ok(x),
            Err(e) => match e {
                RoboatError::InvalidXcsrf(new_xcsrf) => {
                    self.set_xcsrf(new_xcsrf).await;

                    self.accept_trade_internal(trade_id).await
                }
                _ => Err(e),
            },
        }
    }

    /// Retrieves the count of trades the user using <https://trades.roblox.com/v1/trades/inbound/count>.
    ///
    /// # Notes
    /// * Requires a valid roblosecurity.
    ///
    /// # Errors
    /// * All errors under [Standard Errors](#standard-errors).
    /// * All errors under [Auth Required Errors](#auth-required-errors).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use roboat::ClientBuilder;
    /// use roboat::RoboatError;
    ///
    /// const ROBLOSECURITY: &str = "roblosecurity";
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), RoboatError> {
    /// let client = ClientBuilder::new().roblosecurity(ROBLOSECURITY.to_string()).build();
    ///
    /// let trade_count = client.trade_count().await?;
    ///
    /// println!("Total trades: {}", trade_count);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn trade_count(&self) -> Result<u64, RoboatError> {
        let cookie_string = self.cookie_string()?;

        let response_result = self
            .reqwest_client
            .get(TRADE_COUNT_API)
            .header(header::COOKIE, cookie_string)
            .send()
            .await;

        let response = Self::validate_request_result(response_result).await?;
        let raw = Self::parse_to_raw::<request_types::TradeCountResponse>(response).await?;

        Ok(raw.count)
    }
}

mod internal {
    use super::{request_types, ACCEPT_TRADE_API, DECLINE_TRADE_API, SEND_TRADE_API};
    use crate::{Client, RoboatError, XCSRF_HEADER};
    use reqwest::header;

    impl Client {
        pub(super) async fn decline_trade_internal(
            &self,
            trade_id: u64,
        ) -> Result<(), RoboatError> {
            let formatted_url = DECLINE_TRADE_API.replace("{trade_id}", &trade_id.to_string());
            let cookie_string = self.cookie_string()?;
            let xcsrf = self.xcsrf().await;

            let response_result = self
                .reqwest_client
                .post(&formatted_url)
                .header(header::COOKIE, cookie_string)
                .header(XCSRF_HEADER, xcsrf)
                .send()
                .await;

            Self::validate_request_result(response_result).await?;

            Ok(())
        }

        pub(super) async fn send_trade_internal(
            &self,
            partner_id: u64,
            your_item_uaids: Vec<u64>,
            your_robux: u64,
            partner_item_uaids: Vec<u64>,
            partner_robux: u64,
        ) -> Result<u64, RoboatError> {
            let cookie_string = self.cookie_string()?;
            let xcsrf = self.xcsrf().await;

            let user_id = self.user_id().await?;
            let user_trade_offer = request_types::SendTradeOffer {
                user_id,
                user_asset_ids: your_item_uaids,
                robux: your_robux,
            };

            let partner_trade_offer = request_types::SendTradeOffer {
                user_id: partner_id,
                user_asset_ids: partner_item_uaids,
                robux: partner_robux,
            };

            let body = request_types::SendTradeBody {
                // The partner trade offer always comes first.
                offers: vec![partner_trade_offer, user_trade_offer],
            };

            let response_result = self
                .reqwest_client
                .post(SEND_TRADE_API)
                .header(header::COOKIE, cookie_string)
                .header(XCSRF_HEADER, xcsrf)
                .json(&body)
                .send()
                .await;

            let response = Self::validate_request_result(response_result).await?;
            let raw = Self::parse_to_raw::<request_types::SendTradeResponse>(response).await?;

            Ok(raw.id)
        }

        pub(super) async fn accept_trade_internal(&self, trade_id: u64) -> Result<(), RoboatError> {
            let formatted_url = ACCEPT_TRADE_API.replace("{trade_id}", &trade_id.to_string());
            let cookie_string = self.cookie_string()?;
            let xcsrf = self.xcsrf().await;

            let response_result = self
                .reqwest_client
                .post(&formatted_url)
                .header(header::COOKIE, cookie_string)
                .header(XCSRF_HEADER, xcsrf)
                .send()
                .await;

            Self::validate_request_result(response_result).await?;

            // The response is empty, so we just return Ok(()).
            Ok(())
        }
    }
}