twilight_http/request/
get_current_authorization_information.rs

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