mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 06:25:10 +01:00
Separate types and functions
This commit is contained in:
parent
db7755d398
commit
5020cafcd2
3 changed files with 50 additions and 44 deletions
35
src/core/functions.rs
Normal file
35
src/core/functions.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use crate::core::types::User;
|
||||
use crate::core::{Error, Response, TELEGRAM_URL_START, REQWEST_CLIENT};
|
||||
use serde_json::Value;
|
||||
use futures::compat::Future01CompatExt;
|
||||
|
||||
pub async fn get_me(bot_token: &str) -> Response<User> {
|
||||
let mut response = REQWEST_CLIENT
|
||||
.get(&format!(
|
||||
"{}{bot_token}/getMe",
|
||||
TELEGRAM_URL_START,
|
||||
bot_token = bot_token
|
||||
))
|
||||
.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())
|
||||
}
|
|
@ -1,8 +1,9 @@
|
|||
use futures::compat::Future01CompatExt;
|
||||
use reqwest::r#async::Client;
|
||||
|
||||
use reqwest::StatusCode;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
mod functions;
|
||||
mod types;
|
||||
|
||||
lazy_static! {
|
||||
static ref REQWEST_CLIENT: Client = Client::new();
|
||||
|
@ -21,44 +22,3 @@ pub enum Error {
|
|||
}
|
||||
|
||||
pub type Response<T> = Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct User {
|
||||
id: i64,
|
||||
is_bot: bool,
|
||||
first_name: String,
|
||||
last_name: Option<String>,
|
||||
username: Option<String>,
|
||||
language_code: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn get_me(bot_token: &str) -> Response<User> {
|
||||
let mut response = REQWEST_CLIENT
|
||||
.get(&format!(
|
||||
"{}{bot_token}/getMe",
|
||||
TELEGRAM_URL_START,
|
||||
bot_token = bot_token
|
||||
))
|
||||
.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())
|
||||
}
|
||||
|
|
11
src/core/types.rs
Normal file
11
src/core/types.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct User {
|
||||
id: i64,
|
||||
is_bot: bool,
|
||||
first_name: String,
|
||||
last_name: Option<String>,
|
||||
username: Option<String>,
|
||||
language_code: Option<String>,
|
||||
}
|
Loading…
Reference in a new issue