mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Create network/mod.rs
This commit is contained in:
parent
631159f92d
commit
cd7982ec29
3 changed files with 65 additions and 22 deletions
|
@ -11,3 +11,4 @@ reqwest = "0.9.20"
|
||||||
serde_json = "1.0.39"
|
serde_json = "1.0.39"
|
||||||
serde = {version = "1.0.92", features = ["derive"] }
|
serde = {version = "1.0.92", features = ["derive"] }
|
||||||
lazy_static = "1.3"
|
lazy_static = "1.3"
|
||||||
|
apply = "0.2.2"
|
|
@ -1,7 +1,5 @@
|
||||||
use reqwest::r#async::Client;
|
use reqwest::r#async::Client;
|
||||||
|
|
||||||
use reqwest::StatusCode;
|
|
||||||
|
|
||||||
mod games;
|
mod games;
|
||||||
mod getting_updates;
|
mod getting_updates;
|
||||||
mod inline_mode;
|
mod inline_mode;
|
||||||
|
@ -9,22 +7,5 @@ mod other;
|
||||||
mod payments;
|
mod payments;
|
||||||
mod stickers;
|
mod stickers;
|
||||||
mod telegram_passport;
|
mod telegram_passport;
|
||||||
|
mod network;
|
||||||
mod updating_messages;
|
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<String>,
|
|
||||||
},
|
|
||||||
Send(reqwest::Error),
|
|
||||||
InvalidJson(reqwest::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Response<T> = Result<T, Error>;
|
|
||||||
|
|
61
src/core/network/mod.rs
Normal file
61
src/core/network/mod.rs
Normal file
|
@ -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<String>,
|
||||||
|
},
|
||||||
|
Send(reqwest::Error),
|
||||||
|
InvalidJson(reqwest::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Response<T> = Result<T, Error>;
|
||||||
|
|
||||||
|
pub async fn request<T: serde::de::DeserializeOwned>(
|
||||||
|
client: &Client,
|
||||||
|
token: &str,
|
||||||
|
method_name: &str,
|
||||||
|
params: Option<Form>,
|
||||||
|
) -> Response<T> {
|
||||||
|
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::<Value>()
|
||||||
|
.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())
|
||||||
|
}
|
Loading…
Reference in a new issue