Remove "simple" request-related things

This commit is contained in:
Waffle 2019-11-08 01:52:04 +03:00
parent a20b3f132a
commit 8056872c02
8 changed files with 21 additions and 94 deletions

View file

@ -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<GetMe> {
simple::Request::new(self)
pub fn get_me(&self) -> json::Request<GetMe> {
json::Request::new(self, GetMe {})
}
/// For tg-method documentation see [`SendMessage`]

View file

@ -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<O>(
&self,
payload: &dyn dynamic::Payload<Output = O>
@ -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<P>(&self, payload: &P) -> ResponseResult<P::Output>
where
P: Method,
P::Output: DeserializeOwned,
{
request_simple(self.client(), self.token(), P::NAME).await
}
}

View file

@ -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,
};

View file

@ -24,23 +24,6 @@ where
process_response(response).await
}
pub async fn request_simple<T>(
client: &Client,
token: &str,
method_name: &str,
) -> ResponseResult<T>
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<T, P>(
client: &Client,
token: &str,
@ -92,7 +75,6 @@ pub async fn request_dynamic<T>(
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,

View file

@ -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

View file

@ -5,7 +5,6 @@ mod utils;
pub mod json;
pub mod multipart;
pub mod simple;
pub mod dynamic;

View file

@ -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 {}
}
}

View file

@ -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<M>,
}
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<M::Output> {
network::request_simple(
self.bot.client(),
self.bot.token(),
M::NAME,
).await
}
}