1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::time::Duration;
use telegram_bot_async_raw::{Request, ResponseType};
use crate::connector::Connector;
#[cfg(feature = "hyper_connector")]
use crate::{
connector::{default_connector, hyper::DefaultConnector},
errors::Error,
stream::{NewUpdatesStream, UpdatesStream},
};
/// Default API client type.
pub type DefaultApi = Api<DefaultConnector>;
/// Main type for sending requests to the Telegram bot API.
#[derive(Clone)]
pub struct Api<C: Connector> {
url: Option<String>,
token: String,
connector: C,
}
impl<C: Connector> Api<C> {
/// Start construction of the `Api` instance.
///
/// # Examples
///
/// Using default connector.
///
/// ```rust
/// # extern crate telegram_bot_fork;
/// # extern crate tokio;
/// use telegram_bot_fork::DefaultApi;
///
/// # fn main() {
/// # let telegram_token = "token";
/// let api = DefaultApi::new_default(telegram_token.to_string()).unwrap();
/// # }
/// ```
#[cfg(feature = "hyper_connector")]
pub fn new_default(token: String) -> Result<DefaultApi, Error> {
Ok(Api::with_connector(token, default_connector()?))
}
/// Creates new API using custom connector.
///
///
/// ```rust
/// # extern crate telegram_bot_fork;
/// # extern crate tokio;
/// # #[cfg(feature = "hyper_connector")]
/// # fn main() {
/// use telegram_bot_fork::{connector::hyper, Api};
///
/// # let telegram_token = "token";
/// let api = Api::with_connector(telegram_token.to_string(), hyper::default_connector().unwrap());
/// # }
///
/// # #[cfg(not(feature = "hyper_connector"))]
/// # fn main() {}
/// ```
pub fn with_connector(token: String, connector: C) -> Api<C> {
Api {
url: None,
token,
connector,
}
}
/// Sets base telegram API server URL.
pub fn set_url<T: AsRef<str>>(&mut self, url: T) -> &mut Self {
self.url = Some(url.as_ref().into());
self
}
/// Create a stream which produces updates from the Telegram server.
///
/// # Examples
///
/// ```rust
/// # #![feature(stmt_expr_attributes, proc_macro_hygiene)]
/// # extern crate futures;
/// # extern crate telegram_bot_fork;
/// # extern crate tokio;
/// # use telegram_bot_fork::{DefaultApi, Error};
/// # use futures_async_stream::for_await;
///
/// # #[tokio::main]
/// # pub async fn main() -> Result<(), Error> {
/// # let api = DefaultApi::new_default("token".to_string()).unwrap();
/// # let mut stream = api.into_stream();
/// #[for_await]
/// for update in stream.updates() {
/// println!("{:?}", update);
/// }
/// Ok(())
/// # }
/// ```
pub fn into_stream(self) -> UpdatesStream<C> {
UpdatesStream::new(self)
}
/// Send a request to the Telegram server and wait for a response, timing out after `duration`.
/// Future will resolve to `None` if timeout fired.
///
/// # Examples
///
/// ```rust
/// # extern crate futures;
/// # extern crate telegram_bot_fork;
/// # extern crate tokio;
/// # use futures::Future;
/// # use telegram_bot_fork::{DefaultApi, GetMe, Error};
/// #
/// # #[tokio::main]
/// # pub async fn main() -> Result<(), Error> {
/// # let telegram_token = "token";
/// # let api = DefaultApi::new_default(telegram_token.to_string()).unwrap();
/// # if false {
/// use std::time::Duration;
///
/// let resp = api.send_timeout(GetMe, Duration::from_secs(5)).await?;
/// # }
/// Ok(())
/// # }
/// ```
pub async fn send_timeout<Req: Request>(
&self,
request: Req,
duration: Duration,
) -> Result<<Req::Response as ResponseType>::Type, Error> {
tokio::time::timeout(duration, self.send(request)).await?
}
/// Send a request to the Telegram server and wait for a response.
///
/// # Examples
///
/// ```rust
/// # extern crate futures;
/// # extern crate telegram_bot_fork;
/// # extern crate tokio;
/// # use futures::Future;
/// # use telegram_bot_fork::{DefaultApi, GetMe, Error};
/// #
/// # #[tokio::main]
/// # pub async fn main() -> Result<(), Error> {
/// # let telegram_token = "token";
/// # let api = DefaultApi::new_default(telegram_token.to_string()).unwrap();
/// # if false {
/// let resp = api.send(GetMe).await?;
/// # }
/// Ok(())
/// # }
/// ```
pub async fn send<Req: Request>(
&self,
request: Req,
) -> Result<<Req::Response as ResponseType>::Type, Error> {
let request = request.serialize()?;
let response = self
.connector
.request(self.url.as_ref().map(String::as_str), &self.token, request)
.await?;
Ok(Req::Response::deserialize(response)?)
}
}