Skip to main content

twilight_http/request/
get_current_authorization_information.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::oauth::CurrentAuthorizationInformation;
11
12/// Retrieve information about the current OAuth2 authorization.
13///
14/// Returns the application's, authorization's, and if applicable the user's
15/// details.
16///
17/// Refer to [Discord Docs/Get Current Authorization Information][1].
18///
19/// # Examples
20///
21/// ```no_run
22/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
23/// use std::env;
24/// use twilight_http::Client;
25///
26/// let bearer_token = env::var("BEARER_TOKEN")?;
27///
28/// let client = Client::new(bearer_token);
29/// let response = client.current_authorization().await?;
30/// let authorization = response.model().await?;
31///
32/// println!("Application: {}", authorization.application.name);
33///
34/// if let Some(user) = authorization.user {
35///     println!("User: {}", user.name);
36/// }
37/// # Ok(()) }
38/// ```
39///
40/// [1]: https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information
41#[must_use = "requests must be configured and executed"]
42pub struct GetCurrentAuthorizationInformation<'a> {
43    http: &'a Client,
44}
45
46impl<'a> GetCurrentAuthorizationInformation<'a> {
47    pub(crate) const fn new(http: &'a Client) -> Self {
48        Self { http }
49    }
50}
51
52#[cfg(not(target_os = "wasi"))]
53impl IntoFuture for GetCurrentAuthorizationInformation<'_> {
54    type Output = Result<Response<CurrentAuthorizationInformation>, Error>;
55
56    type IntoFuture = ResponseFuture<CurrentAuthorizationInformation>;
57
58    fn into_future(self) -> Self::IntoFuture {
59        let http = self.http;
60
61        match self.try_into_request() {
62            Ok(request) => http.request(request),
63            Err(source) => ResponseFuture::error(source),
64        }
65    }
66}
67
68impl TryIntoRequest for GetCurrentAuthorizationInformation<'_> {
69    fn try_into_request(self) -> Result<Request, Error> {
70        Ok(Request::from_route(
71            &Route::GetCurrentAuthorizationInformation,
72        ))
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::GetCurrentAuthorizationInformation;
79    use crate::{client::Client, request::TryIntoRequest};
80    use static_assertions::assert_impl_all;
81    use std::{error::Error, future::IntoFuture};
82    use twilight_http_ratelimiting::Method;
83
84    assert_impl_all!(GetCurrentAuthorizationInformation<'_>: IntoFuture, Send, Sync, TryIntoRequest);
85
86    #[test]
87    fn get_current_authorization_information() -> Result<(), Box<dyn Error>> {
88        let client = Client::new(String::new());
89        let req = client.current_authorization().try_into_request()?;
90
91        assert!(req.use_authorization_token());
92        assert!(req.body().is_none());
93        assert!(req.form().is_none());
94        assert!(req.headers().is_none());
95        assert_eq!(Method::Get, req.method());
96
97        Ok(())
98    }
99}