Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve module structure #203

Closed
wants to merge 9 commits into from
7 changes: 4 additions & 3 deletions examples/api_trait_implementation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use frankenstein::ErrorResponse;
use frankenstein::SendMessageParams;
use frankenstein::parameters::SendMessageParams;
use frankenstein::response::ErrorResponse;
use frankenstein::TelegramApi;
use isahc::{prelude::*, Request};
use isahc::prelude::*;
use isahc::Request;
use std::path::PathBuf;

static TOKEN: &str = "TOKEN";
Expand Down
3 changes: 1 addition & 2 deletions examples/async_custom_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use frankenstein::AsyncApi;
use frankenstein::AsyncTelegramApi;
use frankenstein::{AsyncApi, AsyncTelegramApi};
use std::time::Duration;

static TOKEN: &str = "API_TOKEN";
Expand Down
5 changes: 2 additions & 3 deletions examples/async_file_upload.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use frankenstein::api_params::SendPhotoParams;
use frankenstein::AsyncApi;
use frankenstein::AsyncTelegramApi;
use frankenstein::parameters::SendPhotoParams;
use frankenstein::{AsyncApi, AsyncTelegramApi};

static TOKEN: &str = "TOKEN";
static CHAT_ID: i64 = 1;
Expand Down
3 changes: 1 addition & 2 deletions examples/async_get_me.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use frankenstein::AsyncApi;
use frankenstein::AsyncTelegramApi;
use frankenstein::{AsyncApi, AsyncTelegramApi};

static TOKEN: &str = "API_TOKEN";

Expand Down
9 changes: 3 additions & 6 deletions examples/async_reply_to_message_updates.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use frankenstein::AsyncTelegramApi;
use frankenstein::GetUpdatesParams;
use frankenstein::Message;
use frankenstein::ReplyParameters;
use frankenstein::SendMessageParams;
use frankenstein::{AsyncApi, UpdateContent};
use frankenstein::objects::{Message, UpdateContent};
use frankenstein::parameters::{GetUpdatesParams, ReplyParameters, SendMessageParams};
use frankenstein::{AsyncApi, AsyncTelegramApi};

static TOKEN: &str = "API_TOKEN";

Expand Down
3 changes: 1 addition & 2 deletions examples/custom_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use frankenstein::Api;
use frankenstein::TelegramApi;
use frankenstein::{Api, TelegramApi};
use std::time::Duration;

static TOKEN: &str = "API_TOKEN";
Expand Down
3 changes: 1 addition & 2 deletions examples/get_me.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use frankenstein::Api;
use frankenstein::TelegramApi;
use frankenstein::{Api, TelegramApi};

static TOKEN: &str = "API_TOKEN";

Expand Down
9 changes: 3 additions & 6 deletions examples/inline_keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use frankenstein::Api;
use frankenstein::InlineKeyboardButton;
use frankenstein::InlineKeyboardMarkup;
use frankenstein::ReplyMarkup;
use frankenstein::SendMessageParams;
use frankenstein::TelegramApi;
use frankenstein::objects::{InlineKeyboardButton, InlineKeyboardMarkup};
use frankenstein::parameters::{ReplyMarkup, SendMessageParams};
use frankenstein::{Api, TelegramApi};

// replace with your token
static TOKEN: &str = "TOKEN";
Expand Down
9 changes: 3 additions & 6 deletions examples/reply_keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use frankenstein::Api;
use frankenstein::KeyboardButton;
use frankenstein::ReplyKeyboardMarkup;
use frankenstein::ReplyMarkup;
use frankenstein::SendMessageParams;
use frankenstein::TelegramApi;
use frankenstein::objects::{KeyboardButton, ReplyKeyboardMarkup};
use frankenstein::parameters::{ReplyMarkup, SendMessageParams};
use frankenstein::{Api, TelegramApi};

// replace with your token
static TOKEN: &str = "TOKEN";
Expand Down
8 changes: 3 additions & 5 deletions examples/reply_to_message_updates.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use frankenstein::GetUpdatesParams;
use frankenstein::ReplyParameters;
use frankenstein::SendMessageParams;
use frankenstein::TelegramApi;
use frankenstein::{Api, UpdateContent};
use frankenstein::objects::UpdateContent;
use frankenstein::parameters::{GetUpdatesParams, ReplyParameters, SendMessageParams};
use frankenstein::{Api, TelegramApi};

static TOKEN: &str = "API_TOKEN";

Expand Down
18 changes: 7 additions & 11 deletions src/client_reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ impl AsyncTelegramApi for AsyncApi {
#[cfg(test)]
mod async_tests {
use super::*;
use crate::api_params::SendMessageParams;
use crate::response::ErrorResponse;
use crate::parameters::SendMessageParams;

#[tokio::test]
async fn async_send_message_success() {
Expand Down Expand Up @@ -186,16 +185,13 @@ mod async_tests {
.await;
let api = AsyncApi::new_url(server.url());

if let Err(Error::Api(ErrorResponse {
ok: false,
description,
error_code: 400,
parameters: None,
})) = api.send_message(&params).await
{
assert_eq!("Bad Request: chat not found".to_string(), description);
if let Err(Error::Api(error)) = dbg!(api.send_message(&params).await) {
assert_eq!(error.description, "Bad Request: chat not found");
assert_eq!(error.error_code, 400);
assert_eq!(error.parameters, None);
assert!(!error.ok);
} else {
panic!("Error was expected but there is none");
panic!("API Error expected");
}
}
}
113 changes: 29 additions & 84 deletions src/client_ureq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,81 +142,29 @@ impl TelegramApi for Api {
#[cfg(test)]
mod tests {
use super::*;
use crate::api_params::AnswerCallbackQueryParams;
use crate::api_params::AnswerInlineQueryParams;
use crate::api_params::BanChatMemberParams;
use crate::api_params::BotCommandScope;
use crate::api_params::BotCommandScopeChat;
use crate::api_params::ChatAction;
use crate::api_params::ChatId;
use crate::api_params::CopyMessageParams;
use crate::api_params::CreateChatInviteLinkParams;
use crate::api_params::DeleteChatPhotoParams;
use crate::api_params::DeleteChatStickerSetParams;
use crate::api_params::DeleteMessageParams;
use crate::api_params::DeleteMyCommandsParams;
use crate::api_params::DeleteWebhookParams;
use crate::api_params::EditChatInviteLinkParams;
use crate::api_params::EditMessageCaptionParams;
use crate::api_params::EditMessageLiveLocationParams;
use crate::api_params::EditMessageMediaParams;
use crate::api_params::EditMessageTextParams;
use crate::api_params::ExportChatInviteLinkParams;
use crate::api_params::FileUpload;
use crate::api_params::ForwardMessageParams;
use crate::api_params::GetChatAdministratorsParams;
use crate::api_params::GetChatMemberCountParams;
use crate::api_params::GetChatMemberParams;
use crate::api_params::GetChatParams;
use crate::api_params::GetFileParams;
use crate::api_params::GetMyCommandsParams;
use crate::api_params::GetStickerSetParams;
use crate::api_params::GetUpdatesParams;
use crate::api_params::GetUserProfilePhotosParams;
use crate::api_params::InlineQueryResult;
use crate::api_params::InputFile;
use crate::api_params::InputMedia;
use crate::api_params::InputMediaPhoto;
use crate::api_params::LeaveChatParams;
use crate::api_params::Media;
use crate::api_params::PinChatMessageParams;
use crate::api_params::PromoteChatMemberParams;
use crate::api_params::RestrictChatMemberParams;
use crate::api_params::RevokeChatInviteLinkParams;
use crate::api_params::SendAnimationParams;
use crate::api_params::SendAudioParams;
use crate::api_params::SendChatActionParams;
use crate::api_params::SendContactParams;
use crate::api_params::SendDiceParams;
use crate::api_params::SendDocumentParams;
use crate::api_params::SendLocationParams;
use crate::api_params::SendMediaGroupParams;
use crate::api_params::SendMessageParams;
use crate::api_params::SendPhotoParams;
use crate::api_params::SendPollParams;
use crate::api_params::SendStickerParams;
use crate::api_params::SendVenueParams;
use crate::api_params::SendVideoNoteParams;
use crate::api_params::SendVideoParams;
use crate::api_params::SendVoiceParams;
use crate::api_params::SetChatAdministratorCustomTitleParams;
use crate::api_params::SetChatDescriptionParams;
use crate::api_params::SetChatPermissionsParams;
use crate::api_params::SetChatPhotoParams;
use crate::api_params::SetChatStickerSetParams;
use crate::api_params::SetChatTitleParams;
use crate::api_params::SetMyCommandsParams;
use crate::api_params::SetWebhookParams;
use crate::api_params::StopMessageLiveLocationParams;
use crate::api_params::StopPollParams;
use crate::api_params::UnbanChatMemberParams;
use crate::api_params::UnpinChatMessageParams;
use crate::objects::AllowedUpdate;
use crate::objects::BotCommand;
use crate::objects::ChatPermissions;
use crate::objects::InlineQueryResultVenue;
use crate::objects::InputPollOption;
use crate::response::ErrorResponse;
use crate::objects::{
AllowedUpdate, BotCommand, ChatPermissions, InlineQueryResultVenue, InputPollOption,
};
use crate::parameters::{
AnswerCallbackQueryParams, AnswerInlineQueryParams, BanChatMemberParams, BotCommandScope,
BotCommandScopeChat, ChatAction, ChatId, CopyMessageParams, CreateChatInviteLinkParams,
DeleteChatPhotoParams, DeleteChatStickerSetParams, DeleteMessageParams,
DeleteMyCommandsParams, DeleteWebhookParams, EditChatInviteLinkParams,
EditMessageCaptionParams, EditMessageLiveLocationParams, EditMessageMediaParams,
EditMessageTextParams, ExportChatInviteLinkParams, FileUpload, ForwardMessageParams,
GetChatAdministratorsParams, GetChatMemberCountParams, GetChatMemberParams, GetChatParams,
GetFileParams, GetMyCommandsParams, GetStickerSetParams, GetUpdatesParams,
GetUserProfilePhotosParams, InlineQueryResult, InputFile, InputMedia, InputMediaPhoto,
LeaveChatParams, Media, PinChatMessageParams, PromoteChatMemberParams,
RestrictChatMemberParams, RevokeChatInviteLinkParams, SendAnimationParams, SendAudioParams,
SendChatActionParams, SendContactParams, SendDiceParams, SendDocumentParams,
SendLocationParams, SendMediaGroupParams, SendMessageParams, SendPhotoParams,
SendPollParams, SendStickerParams, SendVenueParams, SendVideoNoteParams, SendVideoParams,
SendVoiceParams, SetChatAdministratorCustomTitleParams, SetChatDescriptionParams,
SetChatPermissionsParams, SetChatPhotoParams, SetChatStickerSetParams, SetChatTitleParams,
SetMyCommandsParams, SetWebhookParams, StopMessageLiveLocationParams, StopPollParams,
UnbanChatMemberParams, UnpinChatMessageParams,
};

#[test]
fn new_sets_correct_url() {
Expand Down Expand Up @@ -289,16 +237,13 @@ mod tests {
.create();
let api = Api::new_url(server.url());

if let Err(Error::Api(ErrorResponse {
ok: false,
description,
error_code: 400,
parameters: None,
})) = api.send_message(&params)
{
assert_eq!("Bad Request: chat not found".to_string(), description);
if let Err(Error::Api(error)) = dbg!(api.send_message(&params)) {
assert_eq!(error.description, "Bad Request: chat not found");
assert_eq!(error.error_code, 400);
assert_eq!(error.parameters, None);
assert!(!error.ok);
} else {
panic!("Error was expected but there is none");
panic!("API Error expected");
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::response::ErrorResponse;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, thiserror::Error)]
Expand All @@ -8,7 +7,7 @@ pub enum Error {
#[error("Http Error {code}: {message}")]
Http { code: u16, message: String },
#[error("Api Error {0:?}")]
Api(ErrorResponse),
Api(crate::response::ErrorResponse),
#[error("Decode Error {0}")]
Decode(String),
#[error("Encode Error {0}")]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ pub use reqwest;
#[cfg(feature = "ureq")]
pub use ureq;

pub use self::api_params::*;
#[cfg(feature = "async-http-client")]
pub use self::client_reqwest::*;
#[cfg(feature = "http-client")]
pub use self::client_ureq::*;
pub use self::error::Error;
pub use self::objects::*;
pub use self::parameters::*;
pub use self::parse_mode::ParseMode;
pub use self::response::*;
#[cfg(feature = "async-telegram-trait")]
pub use self::trait_async::AsyncTelegramApi;
#[cfg(feature = "telegram-trait")]
pub use self::trait_sync::TelegramApi;

pub mod api_params;
#[cfg(feature = "async-http-client")]
mod client_reqwest;
#[cfg(feature = "http-client")]
Expand All @@ -28,6 +27,7 @@ mod error;
#[cfg(feature = "serde_json")]
mod json;
pub mod objects;
pub mod parameters;
mod parse_mode;
pub mod response;
#[cfg(feature = "async-telegram-trait")]
Expand Down
3 changes: 2 additions & 1 deletion src/objects.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Objects returned or used with the Telegram API.

#![allow(deprecated)]
use crate::api_params::FileUpload;

use crate::parameters::FileUpload;
use crate::ParseMode;
use bon::Builder;
use serde::{Deserialize, Serialize};
Expand Down
7 changes: 4 additions & 3 deletions src/api_params.rs → src/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Parameters to Telegram API methods.

#![allow(clippy::module_name_repetitions)]

use crate::objects::{
AllowedUpdate, BotCommand, ChatAdministratorRights, ChatPermissions, ForceReply,
InlineKeyboardMarkup, InlineQueryResultArticle, InlineQueryResultAudio,
Expand All @@ -17,10 +19,9 @@ use crate::objects::{
ReplyKeyboardMarkup, ReplyKeyboardRemove, ShippingOption, StickerFormat, StickerType,
WebAppInfo,
};
use crate::ParseMode;
use crate::parse_mode::ParseMode;
use bon::Builder;
use serde::Deserialize;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
Expand Down
3 changes: 1 addition & 2 deletions src/parse_mode.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![allow(deprecated)]

use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

/// Text Formatting Options
///
/// See <https://core.telegram.org/bots/api#formatting-options>
Expand Down
5 changes: 2 additions & 3 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#![allow(clippy::module_name_repetitions)]

use crate::objects::{Message, ResponseParameters};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
Expand All @@ -31,12 +30,12 @@ pub struct ErrorResponse {
/// Contents are subject to change in the future
pub error_code: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub parameters: Option<ResponseParameters>,
pub parameters: Option<crate::objects::ResponseParameters>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum EditMessageResponse {
Message(MethodResponse<Message>),
Message(MethodResponse<crate::objects::Message>),
Bool(MethodResponse<bool>),
}
Loading