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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
use std::{rc::Rc, time::Duration}; use futures::{future::result, Future}; use tokio; use tokio_timer; use telegram_bot_fork_raw::{Request, ResponseType}; use connector::Connector; use future::{NewTelegramFuture, TelegramFuture}; use stream::{NewUpdatesStream, UpdatesStream}; #[cfg(feature = "hyper_connector")] use {connector::default_connector, errors::Error}; /// Main type for sending requests to the Telegram bot API. #[derive(Clone)] pub struct Api { url: Option<String>, inner: Rc<ApiInner>, } struct ApiInner { token: String, connector: Box<Connector>, } impl Api { /// Start construction of the `Api` instance. /// /// # Examples /// /// Using default connector. /// /// ```rust /// # extern crate telegram_bot_fork; /// # extern crate tokio; /// use telegram_bot_fork::Api; /// /// # fn main() { /// # let telegram_token = "token"; /// let api = Api::new(telegram_token).unwrap(); /// # } /// ``` /// /// 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, hyper::default_connector().unwrap()); /// # } /// /// # #[cfg(not(feature = "hyper_connector"))] /// # fn main() {} /// ``` #[cfg(feature = "hyper_connector")] pub fn new<T: AsRef<str>>(token: T) -> Result<Self, Error> { Ok(Self::with_connector(token, default_connector()?)) } pub fn with_connector<T: AsRef<str>>(token: T, connector: Box<Connector>) -> Self { Api { url: None, inner: Rc::new(ApiInner { token: token.as_ref().to_string(), connector, }), } } 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 /// # extern crate futures; /// # extern crate telegram_bot_fork; /// # extern crate tokio; /// # use telegram_bot_fork::Api; /// # fn main() { /// # let api: Api = Api::new("token").unwrap(); /// use futures::Stream; /// /// let future = api.stream().for_each(|update| { /// println!("{:?}", update); /// Ok(()) /// }); /// # } /// ``` pub fn stream(&self) -> UpdatesStream { UpdatesStream::new(self.clone()) } /// Send a request to the Telegram server and do not wait for a response. /// /// # Examples /// /// ```rust /// # extern crate futures; /// # extern crate telegram_bot_fork; /// # extern crate tokio; /// # use futures::Future; /// # use telegram_bot_fork::{Api, GetMe, ChatId}; /// # use telegram_bot_fork::prelude::*; /// # /// # fn main() { /// # let telegram_token = "token"; /// # let api = Api::new(telegram_token).unwrap(); /// # if false { /// let chat = ChatId::new(61031); /// api.spawn(chat.text("Message")) /// # } /// # } pub fn spawn<Req: Request>(&self, request: Req) { tokio::executor::current_thread::spawn(self.send(request).then(|_| Ok(()))); } /// 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::{Api, GetMe}; /// # /// # fn main() { /// # let telegram_token = "token"; /// # let api = Api::new(telegram_token).unwrap(); /// # if false { /// use std::time::Duration; /// /// let future = api.send_timeout(GetMe, Duration::from_secs(5)); /// future.and_then(|me| Ok(assert!(me.is_some()))); /// # } /// # } /// ``` pub fn send_timeout<Req: Request>( &self, request: Req, duration: Duration, ) -> TelegramFuture<Option<<Req::Response as ResponseType>::Type>> { let timeout_future = tokio_timer::sleep(duration).from_err().map(|()| None); let send_future = self.send(request).map(Some); let future = timeout_future .select(send_future) .map(|(item, _next)| item) .map_err(|(item, _next)| item); TelegramFuture::new(Box::new(future)) } /// 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::{Api, GetMe}; /// # /// # fn main() { /// # let telegram_token = "token"; /// # let api = Api::new(telegram_token).unwrap(); /// # if false { /// let future = api.send(GetMe); /// future.and_then(|me| Ok(println!("{:?}", me))); /// # } /// # } /// ``` pub fn send<Req: Request>( &self, request: Req, ) -> TelegramFuture<<Req::Response as ResponseType>::Type> { let request = request.serialize().map_err(From::from); let request = result(request); let api = self.clone(); let response = request.and_then(move |request| { let url = &api.url; let token = &api.inner.token; api.inner .connector .request(url.as_ref().map(String::as_str), token, request) }); let future = response .and_then(move |response| Req::Response::deserialize(response).map_err(From::from)); TelegramFuture::new(Box::new(future)) } }