twilight_http/request/application/monetization/
get_entitlements.rs

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