diff --git a/src/bot/api.rs b/src/bot/api.rs index 63fe8eb5..abd30581 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1,7 +1,7 @@ use crate::{ Bot, requests::{ - simple, json, multipart, + json, multipart, payloads::{GetMe, GetUpdates, SendMessage, SendAnimation} }, types::{ChatId, InputFile}, @@ -22,8 +22,8 @@ impl Bot { /// For tg-method documentation see [`GetMe`] /// /// [`GetMe`]: crate::requests::payloads::GetMe - pub fn get_me(&self) -> simple::Request { - simple::Request::new(self) + pub fn get_me(&self) -> json::Request { + json::Request::new(self, GetMe {}) } /// For tg-method documentation see [`SendMessage`] diff --git a/src/bot/execute.rs b/src/bot/execute.rs index 455e42ae..9f043c0b 100644 --- a/src/bot/execute.rs +++ b/src/bot/execute.rs @@ -3,7 +3,7 @@ use serde::de::DeserializeOwned; use crate::{ Bot, requests::{dynamic, json, multipart, Method, ResponseResult}, - network::{request_dynamic, request_json, request_multipart, request_simple}, + network::{request_dynamic, request_json, request_multipart}, }; impl Bot { @@ -23,12 +23,11 @@ impl Bot { /// 1. we recommend to use `bot.send_message(id, "text").send().await` /// instead /// 2. this is _dynamic_ version of execute, so it has a _little_ overhead, - /// prefer using [`execute_json`], [`execute_multipart`] or - /// [`execute_simple`] depending on type of payload when possible. + /// prefer using [`execute_json`] or [`execute_multipart`] depending on + /// type of payload when possible. /// /// [`execute_json`]: self::Bot::execute_json /// [`execute_multipart`]: self::Bot::execute_multipart - /// [`execute_simple`]: self::Bot::execute_simple pub async fn execute_dyn( &self, payload: &dyn dynamic::Payload @@ -95,25 +94,4 @@ impl Bot { payload.payload() ).await } - - /// Execute simple-request - /// - /// ## Example - /// ```no_run - /// # use telebofr::{Bot, requests::payloads::GetMe}; - /// # #[tokio::main] async fn main() { - /// let bot = Bot::new("TOKEN"); - /// bot.execute_simple(&GetMe /* the only one "simple" request */).await; - /// # } - /// ``` - /// - /// **NOTE**: we recommend to use - /// `bot.get_me().send().await` instead - pub async fn execute_simple

(&self, payload: &P) -> ResponseResult - where - P: Method, - P::Output: DeserializeOwned, - { - request_simple(self.client(), self.token(), P::NAME).await - } } diff --git a/src/network/mod.rs b/src/network/mod.rs index 406039dc..f8d533f6 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -3,7 +3,7 @@ pub use download::download_file_stream; pub use self::{ download::download_file, - request::{request_json, request_multipart, request_simple, request_dynamic}, + request::{request_json, request_multipart, request_dynamic}, telegram_response::TelegramResponse, }; diff --git a/src/network/request.rs b/src/network/request.rs index 3c99c2d9..fb5ace3e 100644 --- a/src/network/request.rs +++ b/src/network/request.rs @@ -24,23 +24,6 @@ where process_response(response).await } -pub async fn request_simple( - client: &Client, - token: &str, - method_name: &str, -) -> ResponseResult -where - T: DeserializeOwned, -{ - let response = client - .post(&super::method_url(TELEGRAM_API_URL, token, method_name)) - .send() - .await - .map_err(RequestError::NetworkError)?; - - process_response(response).await -} - pub async fn request_json( client: &Client, token: &str, @@ -92,7 +75,6 @@ pub async fn request_dynamic( use crate::requests::dynamic::Kind; match params { - Kind::Simple => request_simple(client, token, method_name).await, Kind::Json(str) => request_body(client, token, method_name, str).await, Kind::Multipart(form) => request_multipart( client, diff --git a/src/requests/dynamic.rs b/src/requests/dynamic.rs index 773f84f5..ad778b2b 100644 --- a/src/requests/dynamic.rs +++ b/src/requests/dynamic.rs @@ -6,7 +6,6 @@ use super::{ResponseResult, DynMethod}; /// [`Payload`] kind. Used to determinate the way for sending request. pub enum Kind { - Simple, Json(String), Multipart(multipart::Form), } @@ -18,16 +17,14 @@ pub trait Payload: DynMethod { /// Dynamic ready-to-send telegram request. /// /// This type is useful for storing different requests in one place, however -/// this type has _little_ overhead, so prefer using [json], [multipart] or -/// [simple] requests when possible. +/// this type has _little_ overhead, so prefer using [json] or [multipart] +/// requests when possible. /// -/// See [GetMe], [GetUpdates], [SendMessage] and [SendAnimation] for reference +/// See [GetUpdates], [SendMessage] and [SendAnimation] for reference /// implementations. /// /// [json]: crate::requests::json::Request /// [multipart]: crate::requests::multipart::Request -/// [simple]: crate::requests::simple::Request -/// [GetMe]: crate::requests::payloads::GetMe /// [GetUpdates]: crate::requests::payloads::GetUpdates /// [SendMessage]: crate::requests::payloads::SendMessage /// [SendAnimation]: crate::requests::payloads::SendAnimation diff --git a/src/requests/mod.rs b/src/requests/mod.rs index 46acd180..6ad72e14 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -5,7 +5,6 @@ mod utils; pub mod json; pub mod multipart; -pub mod simple; pub mod dynamic; diff --git a/src/requests/payloads/get_me.rs b/src/requests/payloads/get_me.rs index 16ef5301..543e378c 100644 --- a/src/requests/payloads/get_me.rs +++ b/src/requests/payloads/get_me.rs @@ -2,13 +2,14 @@ use crate::{ requests::{Method, dynamic}, types::User, }; +use crate::requests::json; #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Default, Deserialize, Serialize)] /// A filter method for testing your bot's auth token. Requires no parameters. /// Returns basic information about the bot in form of a [`User`] object. /// /// [`User`]: crate::types::User -pub struct GetMe; +pub struct GetMe {} impl Method for GetMe { type Output = User; @@ -16,8 +17,16 @@ impl Method for GetMe { const NAME: &'static str = "getMe"; } +impl json::Payload for GetMe {} + impl dynamic::Payload for GetMe { fn kind(&self) -> dynamic::Kind { - dynamic::Kind::Simple + dynamic::Kind::Json(serde_json::to_string(self).unwrap()) + } +} + +impl GetMe { + fn new() -> Self { + GetMe {} } } diff --git a/src/requests/simple.rs b/src/requests/simple.rs deleted file mode 100644 index 0f7e3a66..00000000 --- a/src/requests/simple.rs +++ /dev/null @@ -1,38 +0,0 @@ -use serde::de::DeserializeOwned; -use reqwest::multipart; - -use crate::{Bot, network}; -use super::{ResponseResult, Method}; -use std::marker::PhantomData; - -/// Ready-to-send telegram request without params. -/// -/// NOTE: Currently where is only one request without params - [GetMe] -/// -/// See [GetMe] for reference implementation. -/// -/// [GetMe]: crate::requests::payloads::GetMe -#[must_use = "requests do nothing until sent"] -pub struct Request<'b, M> { - bot: &'b Bot, - marker: PhantomData, -} - -impl<'b, M> Request<'b, M> -where - M: Method, - M::Output: DeserializeOwned, -{ - pub fn new(bot: &'b Bot) -> Self { - Self { bot, marker: PhantomData } - } - - /// Send request to telegram - pub async fn send(&self) -> ResponseResult { - network::request_simple( - self.bot.client(), - self.bot.token(), - M::NAME, - ).await - } -}