twilight_http/request/
update_user_application.rs

1use std::future::IntoFuture;
2
3use serde::Serialize;
4use twilight_model::oauth::{
5    Application, ApplicationFlags, ApplicationIntegrationMap, ApplicationIntegrationTypeConfig,
6    InstallParams,
7};
8
9use crate::{
10    client::Client,
11    error::Error,
12    request::{Nullable, Request, TryIntoRequest},
13    response::{Response, ResponseFuture},
14    routing::Route,
15};
16
17#[derive(Serialize)]
18struct UpdateCurrentUserApplicationFields<'a> {
19    #[serde(skip_serializing_if = "Option::is_none")]
20    cover_image: Option<Nullable<&'a str>>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    custom_install_url: Option<&'a str>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    description: Option<&'a str>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    flags: Option<ApplicationFlags>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    icon: Option<Nullable<&'a str>>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    install_params: Option<InstallParams>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    integration_types_config: Option<ApplicationIntegrationMap<ApplicationIntegrationTypeConfig>>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    interactions_endpoint_url: Option<&'a str>,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    role_connections_verification_url: Option<&'a str>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    tags: Option<Vec<&'a str>>,
39}
40
41/// Update the current user's application.
42///
43/// Returns the newly updated application.
44///
45/// Refer to [Discord Docs/Update Current User Application][1].
46///
47/// # Examples
48///
49/// ```no_run
50/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
51/// use std::env;
52/// use twilight_http::Client;
53///
54/// let bearer_token = env::var("BEARER_TOKEN")?;
55///
56/// let client = Client::new(bearer_token);
57/// let response = client
58///     .update_current_user_application()
59///     .description("My cool application")
60///     .await?;
61/// let application = response.model().await?;
62///
63/// println!("Application: {}", application.description);
64///
65/// # Ok(()) }
66/// ```
67///
68/// [1]: https://discord.com/developers/docs/resources/application#edit-current-application
69#[must_use = "requests must be configured and executed"]
70pub struct UpdateCurrentUserApplication<'a> {
71    fields: UpdateCurrentUserApplicationFields<'a>,
72    http: &'a Client,
73}
74
75impl<'a> UpdateCurrentUserApplication<'a> {
76    pub(crate) const fn new(http: &'a Client) -> Self {
77        Self {
78            fields: UpdateCurrentUserApplicationFields {
79                cover_image: None,
80                custom_install_url: None,
81                description: None,
82                flags: None,
83                icon: None,
84                install_params: None,
85                integration_types_config: None,
86                interactions_endpoint_url: None,
87                role_connections_verification_url: None,
88                tags: None,
89            },
90            http,
91        }
92    }
93
94    /// Sets the cover image of the application.
95    pub const fn cover_image(mut self, cover_image: Option<&'a str>) -> Self {
96        self.fields.cover_image = Some(Nullable(cover_image));
97
98        self
99    }
100
101    /// Sets the custom install URL of the application.
102    pub const fn custom_install_url(mut self, custom_install_url: &'a str) -> Self {
103        self.fields.custom_install_url = Some(custom_install_url);
104
105        self
106    }
107
108    /// Sets the description of the application.
109    pub const fn description(mut self, description: &'a str) -> Self {
110        self.fields.description = Some(description);
111
112        self
113    }
114
115    /// Sets the flags of the application.
116    /// Only limited intent flags (`GATEWAY_PRESENCE_LIMITED`, `GATEWAY_GUILD_MEMBERS_LIMITED`,
117    /// and `GATEWAY_MESSAGE_CONTENT_LIMITED`) can be updated via the API.
118    pub const fn flags(mut self, flags: ApplicationFlags) -> Self {
119        self.fields.flags = Some(flags);
120
121        self
122    }
123
124    /// Sets the icon of the application.
125    pub const fn icon(mut self, icon: Option<&'a str>) -> Self {
126        self.fields.icon = Some(Nullable(icon));
127
128        self
129    }
130
131    /// Sets the install params of the application.
132    pub fn install_params(mut self, install_params: InstallParams) -> Self {
133        self.fields.install_params = Some(install_params);
134
135        self
136    }
137
138    pub fn integrations_types_config(
139        mut self,
140        guild: Option<InstallParams>,
141        user: Option<InstallParams>,
142    ) -> Self {
143        let guild = guild.map(|g| ApplicationIntegrationTypeConfig {
144            oauth2_install_params: Some(g),
145        });
146
147        let user = user.map(|u| ApplicationIntegrationTypeConfig {
148            oauth2_install_params: Some(u),
149        });
150
151        self.fields.integration_types_config = Some(ApplicationIntegrationMap { guild, user });
152
153        self
154    }
155
156    /// Sets the interactions endpoint URL of the application.
157    pub const fn interactions_endpoint_url(mut self, interactions_endpoint_url: &'a str) -> Self {
158        self.fields.interactions_endpoint_url = Some(interactions_endpoint_url);
159
160        self
161    }
162
163    /// Sets the role connections verification URL of the application.
164    pub const fn role_connections_verification_url(
165        mut self,
166        role_connections_verification_url: &'a str,
167    ) -> Self {
168        self.fields.role_connections_verification_url = Some(role_connections_verification_url);
169
170        self
171    }
172
173    /// Sets the tags of the application.
174    pub fn tags(mut self, tags: Vec<&'a str>) -> Self {
175        self.fields.tags = Some(tags);
176
177        self
178    }
179}
180
181impl IntoFuture for UpdateCurrentUserApplication<'_> {
182    type Output = Result<Response<Application>, Error>;
183
184    type IntoFuture = ResponseFuture<Application>;
185
186    fn into_future(self) -> Self::IntoFuture {
187        let http = self.http;
188
189        match self.try_into_request() {
190            Ok(request) => http.request(request),
191            Err(source) => ResponseFuture::error(source),
192        }
193    }
194}
195
196impl TryIntoRequest for UpdateCurrentUserApplication<'_> {
197    fn try_into_request(self) -> Result<Request, Error> {
198        let mut request = Request::builder(&Route::UpdateCurrentUserApplication);
199
200        request = request.json(&self.fields);
201
202        request.build()
203    }
204}