twilight_http/request/
get_gateway.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{GetGatewayAuthed, Request, TryIntoRequest},
5    response::{Response, ResponseFuture},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::gateway::connection_info::ConnectionInfo;
10
11/// Get information about the gateway, optionally with additional information detailing the
12/// number of shards to use and sessions remaining.
13///
14/// # Examples
15///
16/// Get the gateway connection URL without bot information:
17///
18/// ```no_run
19/// use twilight_http::Client;
20///
21/// # #[tokio::main]
22/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
23/// let client = Client::new("my token".to_owned());
24///
25/// let info = client.gateway().await?.model().await?;
26/// # Ok(()) }
27/// ```
28///
29/// Get the gateway connection URL with additional shard and session information, which
30/// requires specifying a bot token:
31///
32/// ```no_run
33/// use twilight_http::Client;
34///
35/// # #[tokio::main]
36/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
37/// let client = Client::new("my token".to_owned());
38///
39/// let info = client.gateway().authed().await?.model().await?;
40///
41/// println!("URL: {}", info.url);
42/// println!("Recommended shards to use: {}", info.shards);
43/// # Ok(()) }
44/// ```
45#[must_use = "requests must be configured and executed"]
46pub struct GetGateway<'a> {
47    http: &'a Client,
48}
49
50impl<'a> GetGateway<'a> {
51    pub(crate) const fn new(http: &'a Client) -> Self {
52        Self { http }
53    }
54
55    /// Call to authenticate this request.
56    ///
57    /// Returns additional information: the recommended number of shards to use, and information on
58    /// the current session start limit.
59    pub const fn authed(self) -> GetGatewayAuthed<'a> {
60        GetGatewayAuthed::new(self.http)
61    }
62}
63
64impl IntoFuture for GetGateway<'_> {
65    type Output = Result<Response<ConnectionInfo>, Error>;
66
67    type IntoFuture = ResponseFuture<ConnectionInfo>;
68
69    fn into_future(self) -> Self::IntoFuture {
70        let http = self.http;
71
72        match self.try_into_request() {
73            Ok(request) => http.request(request),
74            Err(source) => ResponseFuture::error(source),
75        }
76    }
77}
78
79impl TryIntoRequest for GetGateway<'_> {
80    fn try_into_request(self) -> Result<Request, Error> {
81        Ok(Request::from_route(&Route::GetGateway))
82    }
83}