gw2api_rs/v2/
commerce.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::{Authentication, ClientExecutor, RequestBuilder};
5
6/// The coins and items currently waiting in trading post delivery.
7#[derive(Clone, Debug, Serialize, Deserialize)]
8pub struct Delivery {
9    pub coins: u64,
10    pub items: Vec<DeliveryItem>,
11}
12
13/// An item in the delivery box.
14#[derive(Clone, Debug, Serialize, Deserialize)]
15pub struct DeliveryItem {
16    pub id: u64,
17    pub count: u64,
18}
19
20impl Delivery {
21    /// Returns the delivery box for the currently authenticated account.
22    ///
23    /// # Authentication
24    ///
25    /// This endpoint requires authentication and returns an [`Error`] if no access token is set.
26    /// When authenticated it returns the delivery box for the account of the current access token.
27    ///
28    /// # Examples
29    ///
30    /// ```no_run
31    /// # use gw2api_rs::{Client, Result};
32    /// # use gw2api_rs::v2::commerce::Delivery;
33    /// #
34    /// # async fn run() -> Result<()> {
35    /// # let token = "";
36    /// let client: Client = Client::builder().access_token(token).into();
37    /// let delivery = Delivery::get(&client).await?;
38    /// println!("You have {} coins and {} items waiting for pickup.", delivery.coins, delivery.items.len());
39    /// # Ok(())
40    /// # }
41    /// ```
42    ///
43    /// Using the [`blocking`] client:
44    /// ```no_run
45    /// # use gw2api_rs::Result;
46    /// # use gw2api_rs::blocking::Client;
47    /// # use gw2api_rs::v2::commerce::Delivery;
48    /// #
49    /// # fn run() -> Result<()> {
50    /// # let token = "";
51    /// let client: Client = Client::builder().access_token(token).into();
52    /// let delivery = Delivery::get(&client)?;
53    /// println!("You have {} coins and {} items waiting for pickup.", delivery.coins, delivery.items.len());
54    /// # Ok(())
55    /// # }
56    /// ```
57    ///
58    /// [`Error`]: struct@crate::Error
59    /// [`blocking`]: crate::blocking
60    pub fn get<C>(client: &C) -> C::Result
61    where
62        C: ClientExecutor<Self>,
63    {
64        let uri = "/v2/commerce/delivery";
65        client.send(crate::RequestBuilder::new(uri).authenticated(Authentication::Required))
66    }
67}
68
69/// The current gems to coins exchange rate.
70///
71/// `Exchange` handles both the coins to gems and gems to coins exchange rates.
72#[derive(Clone, Debug, Serialize, Deserialize)]
73pub struct Exchange {
74    /// The current coins to gem exchange rate.
75    pub coins_per_gem: u64,
76    /// The amount of coins/gems you get for the requested amount of coins/gems.
77    pub quantity: u64,
78}
79
80impl Exchange {
81    /// Returns the current coins to gems exchange rate for the provided amount of `coins`.
82    ///
83    /// The `quanity` field contains the amount of gems for the provided amount of coins.
84    ///
85    /// # Examples
86    ///
87    /// ```no_run
88    /// # use gw2api_rs::{Client, Result};
89    /// # use gw2api_rs::v2::commerce::Exchange;
90    /// #
91    /// # async fn run() -> Result<()> {
92    /// let client = Client::new();
93    /// let exchange = Exchange::coins(&client, 100).await?;
94    /// println!("You get {} gems for 100 coins.", exchange.quantity);
95    /// # Ok(())
96    /// # }
97    /// ```
98    ///
99    /// Using the [`blocking`] client:
100    /// ```no_run
101    /// # use gw2api_rs::Result;
102    /// # use gw2api_rs::blocking::Client;
103    /// # use gw2api_rs::v2::commerce::Exchange;
104    /// #
105    /// # fn run() -> Result<()> {
106    /// let client = Client::new();
107    /// let exchange = Exchange::coins(&client, 100)?;
108    /// println!("You get {} gems for 100 coins.", exchange.quantity);
109    /// # Ok(())
110    /// # }
111    /// ```
112    ///
113    /// [`blocking`]: crate::blocking
114    pub fn coins<C>(client: &C, coins: u64) -> C::Result
115    where
116        C: ClientExecutor<Self>,
117    {
118        let uri = format!("/v2/commerce/exchange/coins?quantity={}", coins);
119        client.send(RequestBuilder::new(uri))
120    }
121
122    /// Returns the current gems to coins exchange rate for the provided amount of `gems`.
123    ///
124    /// The `quantity` field contains the amount of coins for the provided amount of gems.
125    ///
126    /// # Examples
127    ///
128    /// ```no_run
129    /// # use gw2api_rs::{Client, Result};
130    /// # use gw2api_rs::v2::commerce::Exchange;
131    /// #
132    /// # async fn run() -> Result<()> {
133    /// let client = Client::new();
134    /// let exchange = Exchange::gems(&client, 100).await?;
135    /// println!("You get {} coins for 100 gems.", exchange.quantity);
136    /// # Ok(())
137    /// # }
138    /// ```
139    ///
140    /// Using the [`blocking`] client:
141    /// ```no_run
142    /// # use gw2api_rs::Result;
143    /// # use gw2api_rs::blocking::Client;
144    /// # use gw2api_rs::v2::commerce::Exchange;
145    /// #
146    /// # fn run() -> Result<()> {
147    /// let client = Client::new();
148    /// let exchange = Exchange::gems(&client, 100)?;
149    /// println!("You get {} coins for 100 gems.", exchange.quantity);
150    /// # Ok(())
151    /// # }
152    /// ```
153    ///
154    /// [`blocking`]: crate::blocking
155    pub fn gems<C>(client: &C, gems: u64) -> C::Result
156    where
157        C: ClientExecutor<Self>,
158    {
159        let uri = format!("/v2/commerce/exchange/gems?quantity={}", gems);
160        client.send(RequestBuilder::new(uri))
161    }
162}
163
164#[derive(Clone, Debug, Serialize, Deserialize)]
165pub struct Listings {
166    pub id: u64,
167    pub buys: Listing,
168}
169
170#[derive(Clone, Debug, Serialize, Deserialize)]
171pub struct Listing {
172    pub listings: u64,
173    pub unit_price: u64,
174    pub quantity: u64,
175}
176
177impl Listings {
178    const URI: &'static str = "/v2/commerce/listings";
179
180    pub fn get<C>(client: &C, id: u64) -> C::Result
181    where
182        C: ClientExecutor<Self>,
183    {
184        let uri = format!("{}?id={}", Self::URI, id);
185        client.send(RequestBuilder::new(uri))
186    }
187
188    /// Returns a list of all items avaliable on the trading post.
189    ///
190    /// # Examples
191    ///
192    /// ```no_run
193    /// # use gw2api_rs::{Client, Result};
194    /// # use gw2api_rs::v2::commerce::Listings;
195    /// #
196    /// # async fn run() -> Result<()> {
197    /// let client = Client::new();
198    /// let ids = Listings::ids(&client).await?;
199    /// println!("There are {} items avaliable through the trading post.", ids.len());
200    /// # Ok(())
201    /// # }
202    /// ```
203    ///
204    /// Using the [`blocking`] client:
205    /// ```no_run
206    /// # use gw2api_rs::Result;
207    /// # use gw2api_rs::blocking::Client;
208    /// # use gw2api_rs::v2::commerce::Listings;
209    /// #
210    /// # fn run() -> Result<()> {
211    /// let client = Client::new();
212    /// let ids = Listings::ids(&client)?;
213    /// println!("There are {} items avaliable through the trading post.", ids.len());
214    /// # Ok(())
215    /// # }
216    /// ```
217    ///
218    /// [`blocking`]: crate::blocking
219    pub fn ids<C>(client: &C) -> C::Result
220    where
221        C: ClientExecutor<Vec<u64>>,
222    {
223        client.send(RequestBuilder::new(Self::URI))
224    }
225}
226
227#[derive(Clone, Debug, Serialize, Deserialize)]
228pub struct Prices {
229    pub id: u64,
230    /// Whether free to play accounts are allowed to buy/sell this item.
231    pub whilelisted: bool,
232    pub buys: Price,
233    pub sells: Price,
234}
235
236#[derive(Clone, Debug, Serialize, Deserialize)]
237pub struct Price {
238    pub unit_price: u64,
239    pub quantity: u64,
240}
241
242impl Prices {
243    const URI: &'static str = "/v2/commerce/prices";
244
245    /// Returns a list of all items avaliable on the trading post.
246    ///
247    /// # Examples
248    ///
249    /// ```no_run
250    /// # use gw2api_rs::{Client, Result};
251    /// # use gw2api_rs::v2::commerce::Listings;
252    /// #
253    /// # async fn run() -> Result<()> {
254    /// let client = Client::new();
255    /// let ids = Listings::ids(&client).await?;
256    /// println!("There are {} items avaliable through the trading post.", ids.len());
257    /// # Ok(())
258    /// # }
259    /// ```
260    ///
261    /// Using the [`blocking`] client:
262    /// ```no_run
263    /// # use gw2api_rs::Result;
264    /// # use gw2api_rs::blocking::Client;
265    /// # use gw2api_rs::v2::commerce::Listings;
266    /// #
267    /// # fn run() -> Result<()> {
268    /// let client = Client::new();
269    /// let ids = Listings::ids(&client)?;
270    /// println!("There are {} items avaliable through the trading post.", ids.len());
271    /// # Ok(())
272    /// # }
273    /// ```
274    ///
275    /// [`blocking`]: crate::blocking
276    pub fn ids<C>(client: &C) -> C::Result
277    where
278        C: ClientExecutor<Vec<u64>>,
279    {
280        client.send(RequestBuilder::new(Self::URI))
281    }
282}
283
284#[derive(Clone, Debug, Serialize, Deserialize)]
285#[serde(transparent)]
286pub struct CurrentTransactions {
287    pub transactions: Vec<CurrentTransaction>,
288}
289
290#[derive(Clone, Debug, Serialize, Deserialize)]
291pub struct CurrentTransaction {
292    pub id: u64,
293    pub item_id: u64,
294    pub price: u64,
295    pub quantity: u64,
296    pub created: DateTime<Utc>,
297}
298
299impl CurrentTransactions {
300    const URI: &'static str = "/v2/commerce/transactions/current";
301
302    /// Returns all outstanding *buy* transactions for the currently authenticated account.
303    ///
304    /// # Authentication
305    ///
306    /// This endpoint requires authentication and returns an [`Error`] if no access token is set.
307    /// When authenticated it returns the outstanding *buy* transactions for the account of the
308    /// current access token.
309    ///
310    /// # Examples
311    ///
312    /// ```no_run
313    /// # use gw2api_rs::{Client, Result};
314    /// # use gw2api_rs::v2::commerce::CurrentTransactions;
315    /// #
316    /// # async fn run() -> Result<()> {
317    /// # let token = "";
318    /// let client: Client = Client::builder().access_token(token).into();
319    /// let transactions = CurrentTransactions::buys(&client).await?;
320    /// println!("{:?}", transactions);
321    /// # Ok(())
322    /// # }
323    /// ```
324    ///
325    /// ```no_run
326    /// # use gw2api_rs::Result;
327    /// # use gw2api_rs::blocking::Client;
328    /// # use gw2api_rs::v2::commerce::CurrentTransactions;
329    /// #
330    /// # fn run() -> Result<()> {
331    /// # let token = "";
332    /// let client: Client = Client::builder().access_token(token).into();
333    /// let transactions = CurrentTransactions::buys(&client)?;
334    /// println!("{:?}", transactions);
335    /// # Ok(())
336    /// # }
337    /// ```
338    ///
339    /// [`Error`]: struct@crate::Error
340    /// [`blocking`]: crate::blocking
341    pub fn buys<C>(client: &C) -> C::Result
342    where
343        C: ClientExecutor<Self>,
344    {
345        let uri = format!("{}/buys", Self::URI);
346        client.send(RequestBuilder::new(uri).authenticated(Authentication::Required))
347    }
348
349    /// Returns all outstanding *sell* transactions for the currently authenticated account.
350    ///
351    /// # Authentication
352    ///
353    /// This endpoint requires authentication and returns an [`Error`] if no access token is set.
354    /// When authenticated it returns the outstanding *sell* transactions for the account of the
355    /// current access token.
356    ///
357    /// # Examples
358    ///
359    /// ```no_run
360    /// # use gw2api_rs::{Client, Result};
361    /// # use gw2api_rs::v2::commerce::CurrentTransactions;
362    /// #
363    /// # async fn run() -> Result<()> {
364    /// # let token = "";
365    /// let client: Client = Client::builder().access_token(token).into();
366    /// let transactions = CurrentTransactions::sells(&client).await?;
367    /// println!("{:?}", transactions);
368    /// # Ok(())
369    /// # }
370    /// ```
371    ///
372    /// ```no_run
373    /// # use gw2api_rs::Result;
374    /// # use gw2api_rs::blocking::Client;
375    /// # use gw2api_rs::v2::commerce::CurrentTransactions;
376    /// #
377    /// # fn run() -> Result<()> {
378    /// # let token = "";
379    /// let client: Client = Client::builder().access_token(token).into();
380    /// let transactions = CurrentTransactions::sells(&client)?;
381    /// println!("{:?}", transactions);
382    /// # Ok(())
383    /// # }
384    /// ```
385    ///
386    /// [`Error`]: struct@crate::Error
387    /// [`blocking`]: crate::blocking
388    pub fn sells<C>(client: &C) -> C::Result
389    where
390        C: ClientExecutor<Self>,
391    {
392        let uri = format!("{}/sells", Self::URI);
393        client.send(RequestBuilder::new(uri).authenticated(Authentication::Required))
394    }
395}
396
397#[derive(Clone, Debug, Serialize, Deserialize)]
398#[serde(transparent)]
399pub struct HistoryTransactions {
400    pub transations: Vec<HistoryTransaction>,
401}
402
403#[derive(Clone, Debug, Serialize, Deserialize)]
404pub struct HistoryTransaction {
405    pub id: u64,
406    pub item_id: u64,
407    pub price: u64,
408    pub quantity: u64,
409    pub created: DateTime<Utc>,
410    pub purchased: DateTime<Utc>,
411}
412
413impl HistoryTransactions {
414    const URI: &'static str = "/v2/commerce/transactions/history";
415
416    /// Returns all *buy* transactions that were fulfilled in the past 90 days for the currently
417    /// authenticated account.
418    ///
419    /// # Authentication
420    ///
421    /// This endpoint requires authentication and returns an [`Error`] if no access token is set.
422    /// When authenticated it returns the fulfilled *buy* transactions for the account of the
423    /// current access token.
424    ///
425    /// # Examples
426    ///
427    /// ```no_run
428    /// # use gw2api_rs::{Client, Result};
429    /// # use gw2api_rs::v2::commerce::HistoryTransactions;
430    /// #
431    /// # async fn run() -> Result<()> {
432    /// # let token = "";
433    /// let client: Client = Client::builder().access_token(token).into();
434    /// let transactions = HistoryTransactions::buys(&client).await?;
435    /// println!("{:?}", transactions);
436    /// # Ok(())
437    /// # }
438    /// ```
439    ///
440    /// ```no_run
441    /// # use gw2api_rs::Result;
442    /// # use gw2api_rs::blocking::Client;
443    /// # use gw2api_rs::v2::commerce::HistoryTransactions;
444    /// #
445    /// # fn run() -> Result<()> {
446    /// # let token = "";
447    /// let client: Client = Client::builder().access_token(token).into();
448    /// let transactions = HistoryTransactions::buys(&client)?;
449    /// println!("{:?}", transactions);
450    /// # Ok(())
451    /// # }
452    /// ```
453    ///
454    /// [`Error`]: struct@crate::Error
455    /// [`blocking`]: crate::blocking
456    pub fn buys<C>(client: &C) -> C::Result
457    where
458        C: ClientExecutor<Self>,
459    {
460        let uri = format!("{}/buys", Self::URI);
461        client.send(RequestBuilder::new(uri).authenticated(Authentication::Required))
462    }
463
464    /// Returns all *sell* transactions that were fulfilled in the past 90 days for the currently
465    /// authenticated account.
466    ///
467    /// # Authentication
468    ///
469    /// This endpoint requires authentication and returns an [`Error`] if no access token is set.
470    /// When authenticated it returns the fulfilled *sell* transactions for the account of the
471    /// current access token.
472    ///
473    /// # Examples
474    ///
475    /// ```no_run
476    /// # use gw2api_rs::{Client, Result};
477    /// # use gw2api_rs::v2::commerce::HistoryTransactions;
478    /// #
479    /// # async fn run() -> Result<()> {
480    /// # let token = "";
481    /// let client: Client = Client::builder().access_token(token).into();
482    /// let transactions = HistoryTransactions::sells(&client).await?;
483    /// println!("{:?}", transactions);
484    /// # Ok(())
485    /// # }
486    /// ```
487    ///
488    /// ```no_run
489    /// # use gw2api_rs::Result;
490    /// # use gw2api_rs::blocking::Client;
491    /// # use gw2api_rs::v2::commerce::HistoryTransactions;
492    /// #
493    /// # fn run() -> Result<()> {
494    /// # let token = "";
495    /// let client: Client = Client::builder().access_token(token).into();
496    /// let transactions = HistoryTransactions::sells(&client)?;
497    /// println!("{:?}", transactions);
498    /// # Ok(())
499    /// # }
500    /// ```
501    ///
502    /// [`Error`]: struct@crate::Error
503    /// [`blocking`]: crate::blocking
504    pub fn sells<C>(client: &C) -> C::Result
505    where
506        C: ClientExecutor<Self>,
507    {
508        let uri = format!("{}/sells", Self::URI);
509        client.send(RequestBuilder::new(uri).authenticated(Authentication::Required))
510    }
511}