twilight_http/request/channel/
get_channel.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::{
10    channel::Channel,
11    id::{marker::ChannelMarker, Id},
12};
13
14/// Get a channel by its ID.
15///
16/// # Examples
17///
18/// Get channel `100`:
19///
20/// ```no_run
21/// use twilight_http::Client;
22/// use twilight_model::id::Id;
23///
24/// # #[tokio::main]
25/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
26/// let client = Client::new("my token".to_owned());
27///
28/// let channel_id = Id::new(100);
29///
30/// let channel = client.channel(channel_id).await?;
31/// # Ok(()) }
32/// ```
33#[must_use = "requests must be configured and executed"]
34pub struct GetChannel<'a> {
35    channel_id: Id<ChannelMarker>,
36    http: &'a Client,
37}
38
39impl<'a> GetChannel<'a> {
40    pub(crate) const fn new(http: &'a Client, channel_id: Id<ChannelMarker>) -> Self {
41        Self { channel_id, http }
42    }
43}
44
45impl IntoFuture for GetChannel<'_> {
46    type Output = Result<Response<Channel>, Error>;
47
48    type IntoFuture = ResponseFuture<Channel>;
49
50    fn into_future(self) -> Self::IntoFuture {
51        let http = self.http;
52
53        match self.try_into_request() {
54            Ok(request) => http.request(request),
55            Err(source) => ResponseFuture::error(source),
56        }
57    }
58}
59
60impl TryIntoRequest for GetChannel<'_> {
61    fn try_into_request(self) -> Result<Request, Error> {
62        Ok(Request::from_route(&Route::GetChannel {
63            channel_id: self.channel_id.get(),
64        }))
65    }
66}