From de0b9f4ab82003ed00619cfcb0ad867a28de7452 Mon Sep 17 00:00:00 2001 From: P0lunin Date: Sun, 15 Sep 2019 14:40:24 +0300 Subject: [PATCH] moved answerPreCheckoutQuery from types to requests --- .../requests/answer_pre_checkout_query.rs | 83 +++++++++++++++++++ src/core/requests/mod.rs | 1 + src/core/types/answer_pre_checkout_query.rs | 7 -- src/core/types/mod.rs | 2 - 4 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 src/core/requests/answer_pre_checkout_query.rs delete mode 100644 src/core/types/answer_pre_checkout_query.rs diff --git a/src/core/requests/answer_pre_checkout_query.rs b/src/core/requests/answer_pre_checkout_query.rs new file mode 100644 index 00000000..fb995ae2 --- /dev/null +++ b/src/core/requests/answer_pre_checkout_query.rs @@ -0,0 +1,83 @@ +use crate::core::{ + requests::{RequestContext, Request, RequestFuture, ResponseResult}, + network +}; + +#[derive(Debug, Serialize, Clone)] +/// Once the user has confirmed their payment and shipping details, the Bot API +/// sends the final confirmation in the form of an [`Update`] with the field +/// pre_checkout_query. Use this method to respond to such pre-checkout queries. +/// On success, True is returned. Note: The Bot API must receive an answer +/// within 10 seconds after the pre-checkout query was sent. +pub struct AnswerPreCheckoutQuery<'a> { + #[serde(skip_serializing)] + ctx: RequestContext<'a>, + + /// Unique identifier for the query to be answered + pub pre_checkout_query_id: String, + + /// Specify True if everything is alright (goods are available, etc.) and + /// the bot is ready to proceed with the order. Use False if there are any + /// problems. + pub ok: bool, + + #[serde(skip_serializing_if = "Option::is_none")] + /// Required if ok is False. Error message in human readable form that + /// explains the reason for failure to proceed with the checkout (e.g. + /// "Sorry, somebody just bought the last of our amazing black T-shirts + /// while you were busy filling out your payment details. Please choose a + /// different color or garment!"). Telegram will display this message to + /// the user. + pub error_message: Option, +} + +impl<'a> Request<'a> for AnswerPreCheckoutQuery<'a> { + type ReturnValue = bool; + + fn send(self) -> RequestFuture<'a, ResponseResult> { + Box::pin(async move { + network::request_json( + &self.ctx.client, + &self.ctx.token, + "answerPreCheckoutQuery", + &self + ).await + }) + } +} + +impl<'a> AnswerPreCheckoutQuery<'a> { + pub(crate) fn new( + ctx: RequestContext<'a>, + pre_checkout_query_id: String, + ok: bool + ) -> Self { + Self { + ctx, + pre_checkout_query_id, + ok, + error_message: None + } + } + + pub fn pre_checkout_query_id(mut self, pre_checkout_query_id: T) -> Self + where T: Into + { + self.pre_checkout_query_id = pre_checkout_query_id.into(); + self + } + + pub fn ok(mut self, ok: T) -> Self + where T: Into + { + self.ok = ok.into(); + self + } + + pub fn error_message(mut self, error_message: T) -> Self + where T: Into + { + self.error_message = Some(error_message.into()); + self + } +} diff --git a/src/core/requests/mod.rs b/src/core/requests/mod.rs index 05e69f7f..bb7c4310 100644 --- a/src/core/requests/mod.rs +++ b/src/core/requests/mod.rs @@ -89,6 +89,7 @@ mod tests { } } +pub mod answer_pre_checkout_query; pub mod edit_message_live_location; pub mod forward_message; pub mod get_file; diff --git a/src/core/types/answer_pre_checkout_query.rs b/src/core/types/answer_pre_checkout_query.rs deleted file mode 100644 index 07d1c758..00000000 --- a/src/core/types/answer_pre_checkout_query.rs +++ /dev/null @@ -1,7 +0,0 @@ -#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] -pub struct AnswerPreCheckoutQuery { - pub pre_checkout_query_id: String, - pub ok: bool, - #[serde(skip_serializing_if = "Option::is_none")] - pub error_message: Option, -} diff --git a/src/core/types/mod.rs b/src/core/types/mod.rs index 7e6d7e08..bd76f782 100644 --- a/src/core/types/mod.rs +++ b/src/core/types/mod.rs @@ -1,6 +1,5 @@ use self::not_implemented_types::*; pub use self::{ - answer_pre_checkout_query::AnswerPreCheckoutQuery, answer_shipping_query::AnswerShippingQuery, animation::Animation, audio::Audio, @@ -51,7 +50,6 @@ pub use self::{ }; mod animation; -mod answer_pre_checkout_query; mod answer_shipping_query; mod audio; mod callback_query;