Skip to main content

twilight_http/request/application/monetization/
get_sku_subscriptions.rs

1use crate::{
2    Client, Error, Response,
3    request::{Request, TryIntoRequest},
4    response::{ResponseFuture, marker::ListBody},
5    routing::Route,
6};
7use std::future::IntoFuture;
8use twilight_model::{
9    application::monetization::Subscription,
10    id::{
11        Id,
12        marker::{SkuMarker, SubscriptionMarker, UserMarker},
13    },
14};
15use twilight_validate::request::{
16    ValidationError, get_sku_subscriptions_limit as validate_get_sku_subscriptions_limit,
17};
18
19struct GetSKUSubscriptionsFields {
20    after: Option<Id<SubscriptionMarker>>,
21    before: Option<Id<SubscriptionMarker>>,
22    limit: Option<u8>,
23    user_id: Option<Id<UserMarker>>,
24}
25
26/// Returns all subscriptions containing the SKU, filtered by user.
27#[must_use = "requests must be configured and executed"]
28pub struct GetSKUSubscriptions<'a> {
29    fields: Result<GetSKUSubscriptionsFields, ValidationError>,
30    http: &'a Client,
31    sku_id: Id<SkuMarker>,
32}
33
34impl<'a> GetSKUSubscriptions<'a> {
35    pub(crate) const fn new(http: &'a Client, sku_id: Id<SkuMarker>) -> Self {
36        Self {
37            fields: Ok(GetSKUSubscriptionsFields {
38                after: None,
39                before: None,
40                limit: None,
41                user_id: None,
42            }),
43            http,
44            sku_id,
45        }
46    }
47
48    /// List subscriptions after this ID.
49    pub const fn after(mut self, after: Id<SubscriptionMarker>) -> Self {
50        if let Ok(fields) = self.fields.as_mut() {
51            fields.after = Some(after);
52        }
53
54        self
55    }
56
57    /// List subscriptions before this ID.
58    pub const fn before(mut self, before: Id<SubscriptionMarker>) -> Self {
59        if let Ok(fields) = self.fields.as_mut() {
60            fields.before = Some(before);
61        }
62
63        self
64    }
65
66    /// Number of subscriptions to return.
67    ///
68    /// The minimum is 1 and the maximum is 100.
69    ///
70    /// The Discord default is 50.
71    ///
72    /// # Errors
73    ///
74    /// Returns a [`GetSKUSubscriptionsError`] error type if the amount
75    /// is less than 1 or greater than 100.
76    ///
77    /// [`GetSKUSubscriptionsError`]: twilight_validate::request::ValidationErrorType::GetSKUSubscriptions
78    pub fn limit(mut self, limit: u8) -> Self {
79        self.fields = self.fields.and_then(|mut fields| {
80            validate_get_sku_subscriptions_limit(limit)?;
81
82            fields.limit = Some(limit);
83
84            Ok(fields)
85        });
86
87        self
88    }
89
90    /// User ID for which to return subscriptions. Required except for OAuth queries.
91    pub const fn user_id(mut self, user_id: Id<UserMarker>) -> Self {
92        if let Ok(fields) = self.fields.as_mut() {
93            fields.user_id = Some(user_id);
94        }
95
96        self
97    }
98}
99
100impl IntoFuture for GetSKUSubscriptions<'_> {
101    type Output = Result<Response<ListBody<Subscription>>, Error>;
102
103    type IntoFuture = ResponseFuture<ListBody<Subscription>>;
104
105    fn into_future(self) -> Self::IntoFuture {
106        let http = self.http;
107
108        match self.try_into_request() {
109            Ok(request) => http.request(request),
110            Err(source) => ResponseFuture::error(source),
111        }
112    }
113}
114
115impl TryIntoRequest for GetSKUSubscriptions<'_> {
116    fn try_into_request(self) -> Result<Request, Error> {
117        let fields = self.fields.map_err(Error::validation)?;
118
119        Ok(Request::from_route(&Route::GetSKUSubscriptions {
120            after: fields.after.map(Id::get),
121            before: fields.before.map(Id::get),
122            limit: fields.limit.map(u64::from),
123            sku_id: self.sku_id.get(),
124            user_id: fields.user_id.map(Id::get),
125        }))
126    }
127}