From cd7982ec29e35d4f84bb9db4aed295137036d0f1 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Mon, 2 Sep 2019 12:29:04 +0600 Subject: [PATCH] Create network/mod.rs --- Cargo.toml | 3 +- src/core/mod.rs | 23 ++-------------- src/core/network/mod.rs | 61 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 src/core/network/mod.rs diff --git a/Cargo.toml b/Cargo.toml index f8fa400b..d5d760a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ futures-preview = { version = "0.3.0-alpha.14", features = ["compat"] } reqwest = "0.9.20" serde_json = "1.0.39" serde = {version = "1.0.92", features = ["derive"] } -lazy_static = "1.3" \ No newline at end of file +lazy_static = "1.3" +apply = "0.2.2" \ No newline at end of file diff --git a/src/core/mod.rs b/src/core/mod.rs index f0f5128c..2c8d076f 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,7 +1,5 @@ use reqwest::r#async::Client; -use reqwest::StatusCode; - mod games; mod getting_updates; mod inline_mode; @@ -9,22 +7,5 @@ mod other; mod payments; mod stickers; mod telegram_passport; -mod updating_messages; - -lazy_static! { - static ref REQWEST_CLIENT: Client = Client::new(); -} - -const TELEGRAM_URL_START: &str = "https://api.telegram.org/bot"; - -#[derive(Debug)] -pub enum Error { - Api { - status_code: StatusCode, - description: Option, - }, - Send(reqwest::Error), - InvalidJson(reqwest::Error), -} - -pub type Response = Result; +mod network; +mod updating_messages; \ No newline at end of file diff --git a/src/core/network/mod.rs b/src/core/network/mod.rs new file mode 100644 index 00000000..883b6274 --- /dev/null +++ b/src/core/network/mod.rs @@ -0,0 +1,61 @@ +use reqwest::StatusCode; +use reqwest::r#async::Client; +use serde_json::Value; +use futures::compat::Future01CompatExt; +use reqwest::r#async::multipart::Form; +use apply::Apply; + + +const TELEGRAM_URL_START: &str = "https://api.telegram.org/bot"; + +#[derive(Debug)] +pub enum Error { + Api { + status_code: StatusCode, + description: Option, + }, + Send(reqwest::Error), + InvalidJson(reqwest::Error), +} + +pub type Response = Result; + +pub async fn request( + client: &Client, + token: &str, + method_name: &str, + params: Option
, +) -> Response { + let mut response = client + .post(&format!( + "{}{token}/{method}", + TELEGRAM_URL_START, + token = token, + method = method_name, + )) + .apply(|req| if let Some(params) = params { + req.multipart(params) + } else { req }) + .send() + .compat() + .await + .map_err(Error::Send)?; + + let response_json = response + .json::() + .compat() + .await + .map_err(Error::InvalidJson)?; + + if response_json["ok"] == "false" { + return Err(Error::Api { + status_code: response.status(), + description: match response_json.get("description") { + None => None, + Some(description) => Some(description.to_string()), + }, + }); + } + + Ok(serde_json::from_value(response_json["result"].clone()).unwrap()) +} \ No newline at end of file