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