From 2cabc5fb4e3caa58337012e3c2d8e1bdb79a27ed Mon Sep 17 00:00:00 2001 From: Waffle Date: Tue, 22 Sep 2020 22:52:23 +0300 Subject: [PATCH] add Requester trait and GetMe payload --- src/bot/api.rs | 26 ++++++++++++++++++-------- src/lib.rs | 2 ++ src/payloads/get_me.rs | 29 +++++++++++++++++++++++++++++ src/payloads/mod.rs | 5 +++++ src/payloads/setters.rs | 1 + src/prelude.rs | 1 + src/requests/mod.rs | 2 ++ src/requests/requester.rs | 15 +++++++++++++++ 8 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 src/payloads/get_me.rs create mode 100644 src/payloads/mod.rs create mode 100644 src/payloads/setters.rs create mode 100644 src/prelude.rs create mode 100644 src/requests/requester.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index bd7e08de..be6b21f5 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -1,4 +1,5 @@ use crate::{ + payloads, requests::{ AddStickerToSet, AnswerCallbackQuery, AnswerInlineQuery, AnswerPreCheckoutQuery, AnswerShippingQuery, CreateNewStickerSet, DeleteChatPhoto, DeleteChatStickerSet, @@ -8,14 +9,15 @@ use crate::{ EditMessageReplyMarkup, EditMessageText, ExportChatInviteLink, ForwardMessage, GetChat, GetChatAdministrators, GetChatMember, GetChatMembersCount, GetFile, GetGameHighScores, GetMe, GetMyCommands, GetStickerSet, GetUpdates, GetUpdatesNonStrict, GetUserProfilePhotos, - GetWebhookInfo, KickChatMember, LeaveChat, PinChatMessage, PromoteChatMember, - RestrictChatMember, SendAnimation, SendAudio, SendChatAction, SendChatActionKind, - SendContact, SendDice, SendDocument, SendGame, SendInvoice, SendLocation, SendMediaGroup, - SendMessage, SendPhoto, SendPoll, SendSticker, SendVenue, SendVideo, SendVideoNote, - SendVoice, SetChatAdministratorCustomTitle, SetChatDescription, SetChatPermissions, - SetChatPhoto, SetChatStickerSet, SetChatTitle, SetGameScore, SetMyCommands, - SetStickerPositionInSet, SetStickerSetThumb, SetWebhook, StopInlineMessageLiveLocation, - StopMessageLiveLocation, StopPoll, UnbanChatMember, UnpinChatMessage, UploadStickerFile, + GetWebhookInfo, JsonRequest, KickChatMember, LeaveChat, PinChatMessage, PromoteChatMember, + Requester, RestrictChatMember, SendAnimation, SendAudio, SendChatAction, + SendChatActionKind, SendContact, SendDice, SendDocument, SendGame, SendInvoice, + SendLocation, SendMediaGroup, SendMessage, SendPhoto, SendPoll, SendSticker, SendVenue, + SendVideo, SendVideoNote, SendVoice, SetChatAdministratorCustomTitle, SetChatDescription, + SetChatPermissions, SetChatPhoto, SetChatStickerSet, SetChatTitle, SetGameScore, + SetMyCommands, SetStickerPositionInSet, SetStickerSetThumb, SetWebhook, + StopInlineMessageLiveLocation, StopMessageLiveLocation, StopPoll, UnbanChatMember, + UnpinChatMessage, UploadStickerFile, }, types::{ BotCommand, ChatId, ChatPermissions, InlineQueryResult, InputFile, InputMedia, @@ -1701,3 +1703,11 @@ impl Bot { } } } + +impl Requester for Bot { + type GetMe = JsonRequest; + + fn get_me(&self) -> JsonRequest { + Self::GetMe::new(self.clone(), payloads::GetMe::new()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 4736649d..f453e1b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,8 @@ pub use self::{ errors::{ApiErrorKind, DownloadError, KnownApiErrorKind, RequestError}, }; +pub mod payloads; +pub mod prelude; pub mod requests; pub mod types; diff --git a/src/payloads/get_me.rs b/src/payloads/get_me.rs new file mode 100644 index 00000000..cabd6f22 --- /dev/null +++ b/src/payloads/get_me.rs @@ -0,0 +1,29 @@ +use serde::{Deserialize, Serialize}; + +use crate::{ + requests::{HasPayload, Payload}, + types::User, +}; + +/// 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 +#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Default, Deserialize, Serialize)] +pub struct GetMe {} + +impl GetMe { + pub const fn new() -> Self { + GetMe {} + } +} + +impl Payload for GetMe { + type Output = User; + + const NAME: &'static str = "getMe"; +} + +pub trait GetMeSetters: HasPayload + Sized {} + +impl

GetMeSetters for P where P: HasPayload {} diff --git a/src/payloads/mod.rs b/src/payloads/mod.rs new file mode 100644 index 00000000..e01013c2 --- /dev/null +++ b/src/payloads/mod.rs @@ -0,0 +1,5 @@ +pub mod setters; + +mod get_me; + +pub use get_me::{GetMe, GetMeSetters}; diff --git a/src/payloads/setters.rs b/src/payloads/setters.rs new file mode 100644 index 00000000..937d0b50 --- /dev/null +++ b/src/payloads/setters.rs @@ -0,0 +1 @@ +pub use crate::payloads::{GetMeSetters as _}; diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 00000000..f805598b --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1 @@ +pub use crate::requests::Requester; diff --git a/src/requests/mod.rs b/src/requests/mod.rs index 188bfc11..67962379 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -9,11 +9,13 @@ pub use self::{has_payload::HasPayload, payload::Payload, request::Request}; mod all; mod json; mod multipart; +mod requester; mod utils; pub use all::*; pub use json::JsonRequest; pub use multipart::MultipartRequest; +pub use requester::Requester; /// A type that is returned after making a request to Telegram. pub type ResponseResult = Result; diff --git a/src/requests/requester.rs b/src/requests/requester.rs new file mode 100644 index 00000000..8390b786 --- /dev/null +++ b/src/requests/requester.rs @@ -0,0 +1,15 @@ +use crate::{payloads::GetMe, requests::Request}; + +/// The trait implemented by all bots & bot wrappers. +/// Essentially a request builder factory (?). +/// +/// _This trait is included in the crate's [`prelude`](crate::prelude)_. +#[cfg_attr(all(docsrs, feature = "nightly"), doc(spotlight))] +pub trait Requester { + type GetMe: Request; + + /// For telegram documentation of the method see [`GetMe`]. + fn get_me(&self) -> Self::GetMe; + + // FIXME(waffle): add remaining 69 methods +}