Skip to main content

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