Skip to main content

twilight_http/request/application/monetization/
get_entitlements.rs

1use std::future::IntoFuture;
2
3use twilight_model::{
4    application::monetization::Entitlement,
5    id::{
6        Id,
7        marker::{ApplicationMarker, EntitlementMarker, GuildMarker, SkuMarker, UserMarker},
8    },
9};
10
11use crate::{
12    Client, Error,
13    request::{Request, TryIntoRequest},
14    routing::Route,
15};
16#[cfg(not(target_os = "wasi"))]
17use crate::{
18    Response,
19    response::{ResponseFuture, marker::ListBody},
20};
21
22use twilight_validate::request::{
23    ValidationError, get_entitlements_limit as validate_get_entitlements_limit,
24};
25
26struct GetEntitlementsFields<'a> {
27    after: Option<Id<EntitlementMarker>>,
28    before: Option<Id<EntitlementMarker>>,
29    exclude_ended: Option<bool>,
30    guild_id: Option<Id<GuildMarker>>,
31    limit: Option<u8>,
32    sku_ids: &'a [Id<SkuMarker>],
33    user_id: Option<Id<UserMarker>>,
34}
35
36/// Get all entitlements for a given app, active and expired.
37#[must_use = "requests must be configured and executed"]
38pub struct GetEntitlements<'a> {
39    application_id: Id<ApplicationMarker>,
40    fields: GetEntitlementsFields<'a>,
41    http: &'a Client,
42}
43
44impl<'a> GetEntitlements<'a> {
45    pub(crate) const fn new(http: &'a Client, application_id: Id<ApplicationMarker>) -> Self {
46        Self {
47            application_id,
48            fields: GetEntitlementsFields {
49                after: None,
50                before: None,
51                exclude_ended: None,
52                guild_id: None,
53                limit: None,
54                sku_ids: &[],
55                user_id: None,
56            },
57            http,
58        }
59    }
60
61    /// Retrieve entitlements after this time.
62    pub const fn after(mut self, after: Id<EntitlementMarker>) -> Self {
63        self.fields.after = Some(after);
64
65        self
66    }
67
68    /// Retrieve entitlements before this time.
69    pub const fn before(mut self, before: Id<EntitlementMarker>) -> Self {
70        self.fields.before = Some(before);
71
72        self
73    }
74
75    /// Whether to exclude ended entitlements.
76    pub const fn exclude_ended(mut self, exclude_ended: bool) -> Self {
77        self.fields.exclude_ended = Some(exclude_ended);
78
79        self
80    }
81
82    /// Guild ID to look up entitlements for.
83    pub const fn guild_id(mut self, guild_id: Id<GuildMarker>) -> Self {
84        self.fields.guild_id = Some(guild_id);
85
86        self
87    }
88
89    /// Number of entitlements to return. Set to 100 if unspecified.
90    ///
91    /// The minimum is 1 and the maximum is 100.
92    ///
93    /// # Errors
94    ///
95    /// Returns a [`GetEntitlementsError`] error type if the amount
96    /// is less than 1 or greater than 100.
97    ///
98    /// [`GetEntitlementsError`]: twilight_validate::request::ValidationErrorType::GetEntitlements
99    pub fn limit(mut self, limit: u8) -> Result<Self, ValidationError> {
100        validate_get_entitlements_limit(limit)?;
101
102        self.fields.limit = Some(limit);
103
104        Ok(self)
105    }
106
107    /// List of SKU IDs to check entitlements for.
108    pub const fn sku_ids(mut self, sku_ids: &'a [Id<SkuMarker>]) -> Self {
109        self.fields.sku_ids = sku_ids;
110
111        self
112    }
113
114    /// User ID to look up entitlements for.
115    pub const fn user_id(mut self, user_id: Id<UserMarker>) -> Self {
116        self.fields.user_id = Some(user_id);
117
118        self
119    }
120}
121
122#[cfg(not(target_os = "wasi"))]
123impl IntoFuture for GetEntitlements<'_> {
124    type Output = Result<Response<ListBody<Entitlement>>, Error>;
125
126    type IntoFuture = ResponseFuture<ListBody<Entitlement>>;
127
128    fn into_future(self) -> Self::IntoFuture {
129        let http = self.http;
130
131        match self.try_into_request() {
132            Ok(request) => http.request(request),
133            Err(source) => ResponseFuture::error(source),
134        }
135    }
136}
137
138impl TryIntoRequest for GetEntitlements<'_> {
139    fn try_into_request(self) -> Result<Request, Error> {
140        Ok(Request::from_route(&Route::GetEntitlements {
141            after: self.fields.after.map(Id::get),
142            application_id: self.application_id.get(),
143            before: self.fields.before.map(Id::get),
144            exclude_ended: self.fields.exclude_ended,
145            guild_id: self.fields.guild_id.map(Id::get),
146            limit: self.fields.limit,
147            sku_ids: self.fields.sku_ids,
148            user_id: self.fields.user_id.map(Id::get),
149        }))
150    }
151}