mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Implement core/requests/form_builder.rs
This commit is contained in:
parent
23b94bab01
commit
01cd8ddd5f
4 changed files with 69 additions and 48 deletions
51
src/core/requests/form_builder.rs
Normal file
51
src/core/requests/form_builder.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use reqwest::r#async::multipart::Form;
|
||||
use serde::Serialize;
|
||||
|
||||
/// This is a convenient struct that builds `reqwest::r#async::multipart::Form`
|
||||
/// from scratch.
|
||||
pub struct FormBuilder {
|
||||
form: Form,
|
||||
}
|
||||
|
||||
impl FormBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { form: Form::new() }
|
||||
}
|
||||
|
||||
/// Add the supplied key-value pair to this `FormBuilder`. Don't forget to
|
||||
/// implement `serde::Serialize` for `T`!
|
||||
pub fn add<T>(self, name: &str, value: &T) -> Self
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
Self {
|
||||
form: self.form.text(
|
||||
name.to_owned(),
|
||||
serde_json::to_string(value).expect("serde_json::to_string failed"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a key-value pair to the supplied `FormBuilder` if `value` is some.
|
||||
/// Don't forget to implement `serde::Serialize` for `T`!
|
||||
pub fn add_if_some<T>(self, name: &str, value: Option<&T>) -> Self
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
Self {
|
||||
form: value.map_or_else(
|
||||
|| self.form,
|
||||
|value| {
|
||||
self.form.text(
|
||||
name.to_owned(),
|
||||
serde_json::to_string(value).expect("serde_json::to_string failed"),
|
||||
)
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(self) -> Form {
|
||||
self.form
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
use reqwest::r#async::multipart::Form;
|
||||
use serde::Serialize;
|
||||
|
||||
/// Adds a key-value pair to the supplied form if `value` is some. Don't forget
|
||||
/// to implement `serde::Serialize` for `T`!
|
||||
pub fn add_to_form_if_some<T>(form: Form, name: &str, value: Option<&T>) -> Form
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
value.map_or_else(
|
||||
|| form,
|
||||
|value| {
|
||||
form.text(
|
||||
name.to_owned(),
|
||||
serde_json::to_string(value).expect("serde_json::to_string failed"),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
mod helpers;
|
||||
|
||||
use reqwest::r#async::multipart::Form;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
mod form_builder;
|
||||
|
||||
/// Request that can be sended to telegram.
|
||||
/// `ReturnValue` - a type that will be returned from Telegram.
|
||||
pub trait Request {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use reqwest::r#async::multipart::Form;
|
||||
|
||||
use super::helpers;
|
||||
use crate::core::types::Message;
|
||||
|
||||
use super::form_builder::FormBuilder;
|
||||
use super::{ChatId, Request};
|
||||
|
||||
#[derive(Debug, TypedBuilder, PartialEq, Eq)]
|
||||
|
@ -31,34 +31,23 @@ impl Request for SendMessage {
|
|||
"getMe"
|
||||
}
|
||||
fn params(self) -> Option<Form> {
|
||||
let params = Form::new()
|
||||
.text("chat_id", serde_json::to_string(&self.chat_id).unwrap())
|
||||
.text("text", self.text);
|
||||
let params = helpers::add_to_form_if_some(params, "parse_mode", self.parse_mode.as_ref());
|
||||
let params = helpers::add_to_form_if_some(
|
||||
params,
|
||||
"disable_web_page_preview",
|
||||
self.disable_web_page_preview.as_ref(),
|
||||
);
|
||||
let params = helpers::add_to_form_if_some(
|
||||
params,
|
||||
"disable_notification",
|
||||
self.disable_notification.as_ref(),
|
||||
);
|
||||
let params = helpers::add_to_form_if_some(
|
||||
params,
|
||||
"reply_to_message_id",
|
||||
self.reply_to_message_id.as_ref(),
|
||||
);
|
||||
Some(
|
||||
FormBuilder::new()
|
||||
.add("chat_id", &self.chat_id)
|
||||
.add("text", &self.text)
|
||||
.add_if_some("parse_mode", self.parse_mode.as_ref())
|
||||
.add_if_some(
|
||||
"disable_web_page_preview",
|
||||
self.disable_web_page_preview.as_ref(),
|
||||
)
|
||||
.add_if_some("disable_notification", self.disable_notification.as_ref())
|
||||
.add_if_some("reply_to_message_id", self.reply_to_message_id.as_ref())
|
||||
.build(),
|
||||
)
|
||||
|
||||
// TODO:
|
||||
// helpers::add_to_form_if_some(
|
||||
// f,
|
||||
// "reply_markup",
|
||||
// self.reply_markup,
|
||||
// )
|
||||
|
||||
Some(params)
|
||||
// .add_if_some("reply_markup",
|
||||
// self.reply_markup.as_ref()))
|
||||
}
|
||||
fn token(&self) -> &str {
|
||||
&self.token
|
||||
|
|
Loading…
Reference in a new issue