mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-14 11:44:04 +01:00
Add json::{Payload, Request}
This commit is contained in:
parent
0a36537aa5
commit
602d07623b
1 changed files with 40 additions and 7 deletions
|
@ -99,12 +99,45 @@ use serde::de::DeserializeOwned;
|
|||
/// A type that is returned from `Request::send_boxed`.
|
||||
pub type ResponseResult<T> = Result<T, crate::RequestError>;
|
||||
|
||||
/// A request that can be sent to Telegram.
|
||||
#[async_trait]
|
||||
pub trait Request {
|
||||
/// A type of response.
|
||||
type Output: DeserializeOwned; // TODO: do we need this bound _here_?
|
||||
///// A request that can be sent to Telegram.
|
||||
//#[async_trait]
|
||||
//pub trait Request {
|
||||
// /// A type of response.
|
||||
// type Output: DeserializeOwned; // TODO: do we need this bound _here_?
|
||||
//
|
||||
// /// Send this request.
|
||||
// async fn send_boxed(self) -> ResponseResult<Self::Output>;
|
||||
//}
|
||||
|
||||
/// Send this request.
|
||||
async fn send_boxed(self) -> ResponseResult<Self::Output>;
|
||||
pub mod json {
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
use crate::{Bot, network};
|
||||
use super::ResponseResult;
|
||||
|
||||
pub trait Payload: Serialize {
|
||||
type Output;
|
||||
|
||||
const METHOD: &'static str;
|
||||
}
|
||||
|
||||
pub struct Request<'b, P> {
|
||||
pub(crate) bot: &'b Bot,
|
||||
pub(crate) payload: P,
|
||||
}
|
||||
|
||||
impl<P> Request<'_, P>
|
||||
where
|
||||
P: Payload,
|
||||
P::Output: DeserializeOwned,
|
||||
{
|
||||
pub async fn send(&self) -> ResponseResult<P::Output> {
|
||||
network::request_json(
|
||||
self.bot.client(),
|
||||
self.bot.token(),
|
||||
P::METHOD,
|
||||
&self.payload,
|
||||
).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue