From 050b3ba113d334d887d425ffa7895e1ae4900e38 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sat, 22 Jul 2017 20:14:44 +0300 Subject: [PATCH] Optimize parameters parser --- aiogram/bot/api.py | 8 ++++- aiogram/bot/base.py | 77 ++------------------------------------------- aiogram/bot/bot.py | 54 +------------------------------ 3 files changed, 11 insertions(+), 128 deletions(-) diff --git a/aiogram/bot/api.py b/aiogram/bot/api.py index ab3ce22a..5cf78f96 100644 --- a/aiogram/bot/api.py +++ b/aiogram/bot/api.py @@ -64,7 +64,13 @@ def _compose_data(params, files=None): if params: for key, value in params.items(): - data.add_field(key, str(value)) + if isinstance(value, dict): + value = json.dumps(value) + elif hasattr(value, 'to_json'): + value = json.dumps(value.to_json()) + else: + value = str(value) + data.add_field(key, value) if files: for key, f in files.items(): diff --git a/aiogram/bot/base.py b/aiogram/bot/base.py index 854fe709..c4920754 100644 --- a/aiogram/bot/base.py +++ b/aiogram/bot/base.py @@ -4,7 +4,6 @@ import io import aiohttp from . import api -from ..utils import json from ..utils.payload import generate_payload @@ -166,17 +165,12 @@ class BaseBot: async def send_photo(self, chat_id, photo, caption=None, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: _message_type = 'photo' - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.SEND_PHOTO, photo, payload) async def send_audio(self, chat_id, audio, caption=None, duration=None, performer=None, title=None, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: _message_type = 'audio' - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.SEND_AUDIO, audio, payload) @@ -184,18 +178,12 @@ class BaseBot: async def send_document(self, chat_id, document, caption=None, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: _message_type = 'document' - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.SEND_DOCUMENT, document, payload) async def send_sticker(self, chat_id, sticker, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: _message_type = 'sticker' - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.SEND_STICKER, sticker, payload) @@ -211,17 +199,12 @@ class BaseBot: async def create_new_sticker_set(self, name: str, title: str, png_sticker, emojis: str, is_mask: bool = None, mask_position: dict or str = None) -> bool: _message_type = 'png_sticker' - if mask_position and isinstance(mask_position, dict): - mask_position = json.dumps(mask_position) - payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.CREATE_NEW_STICKER_SET, png_sticker, payload) async def add_sticker_to_set(self, user_id: int, name: str, png_sticker, emojis: str, mask_position: str or dict) -> bool: _message_type = 'png_sticker' - if mask_position and isinstance(mask_position, dict): - mask_position = json.dumps(mask_position) payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.ADD_STICKER_TO_SET, png_sticker, payload) @@ -237,51 +220,33 @@ class BaseBot: async def send_video(self, chat_id, video, duration=None, width=None, height=None, caption=None, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: _message_type = 'video' - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.SEND_VIDEO, video, payload) async def send_voice(self, chat_id, voice, caption=None, duration=None, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: _message_type = 'voice' - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.SEND_VOICE, voice, payload) async def send_video_note(self, chat_id, video_note, duration=None, length=None, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: _message_type = 'video_note' - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals(), exclude=[_message_type]) return await self.send_file(_message_type, api.Methods.SEND_VIDEO_NOTE, video_note, payload) async def send_location(self, chat_id, latitude, longitude, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals()) return await self.request(api.Methods.SEND_LOCATION, payload) async def send_venue(self, chat_id, latitude, longitude, title, address, foursquare_id, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals()) return await self.request(api.Methods.SEND_VENUE, payload) async def send_contact(self, chat_id, phone_number, first_name, last_name=None, disable_notification=None, reply_to_message_id=None, reply_markup=None) -> dict: - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals()) return await self.request(api.Methods.SEND_CONTACT, payload) @@ -322,19 +287,10 @@ class BaseBot: return await self.request(api.Methods.EXPORT_CHAT_INVITE_LINK, payload) async def set_chat_photo(self, chat_id: int, photo) -> bool: + _message_type = 'photo' payload = generate_payload(**locals(), exclude=['photo']) - if isinstance(photo, str): - payload['photo'] = photo - req = self.request(api.Methods.SET_CHAT_PHOTO, payload) - elif isinstance(photo, io.IOBase): - data = {'photo': photo.read()} - req = self.request(api.Methods.SET_CHAT_PHOTO, payload, data) - else: - data = {'photo': photo} - req = self.request(api.Methods.SET_CHAT_PHOTO, payload, data) - - return await req + return await self.send_file(_message_type, api.Methods.SET_CHAT_PHOTO, photo, payload) async def delete_chat_photo(self, chat_id: int) -> bool: payload = generate_payload(**locals()) @@ -392,17 +348,11 @@ class BaseBot: async def answer_inline_query(self, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None, switch_pm_text=None, switch_pm_parameter=None) -> bool: - if isinstance(results, list): - results = json.dumps([item for item in results]) - payload = generate_payload(**locals()) return await self.request(api.Methods.ANSWER_INLINE_QUERY, payload) async def edit_message_text(self, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None, disable_web_page_preview=None, reply_markup=None) -> dict or bool: - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals()) raw = await self.request(api.Methods.EDIT_MESSAGE_TEXT, payload) if raw is True: @@ -411,9 +361,6 @@ class BaseBot: async def edit_message_caption(self, chat_id=None, message_id=None, inline_message_id=None, caption=None, reply_markup=None) -> dict or bool: - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals()) raw = await self.request(api.Methods.EDIT_MESSAGE_CAPTION, payload) if raw is True: @@ -422,9 +369,6 @@ class BaseBot: async def edit_message_reply_markup(self, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None) -> dict or bool: - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals()) raw = await self.request(api.Methods.EDIT_MESSAGE_REPLY_MARKUP, payload) if raw is True: @@ -442,21 +386,12 @@ class BaseBot: need_shipping_address: bool = None, is_flexible: bool = None, disable_notification: bool = None, reply_to_message_id: int = None, reply_markup: dict or str = None) -> dict: - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - - if isinstance(prices, list): - prices = json.dumps(prices) - payload_ = generate_payload(**locals()) return await self.request(api.Methods.SEND_INVOICE, payload_) async def answer_shipping_query(self, shipping_query_id: str, ok: bool, shipping_options: [dict] = None, error_message: str = None) -> bool: - if shipping_options and isinstance(shipping_options, list): - shipping_options = json.dumps(shipping_options) - payload = generate_payload(**locals()) return await self.request(api.Methods.ANSWER_SHIPPING_QUERY, payload) @@ -469,9 +404,6 @@ class BaseBot: async def send_game(self, chat_id: int, game_short_name: str, disable_notification: bool = None, reply_to_message_id: int = None, reply_markup: dict = None) -> dict: - if reply_markup and isinstance(reply_markup, dict): - reply_markup = json.dumps(reply_markup) - payload = generate_payload(**locals()) return await self.request(api.Methods.SEND_GAME, payload) @@ -481,10 +413,7 @@ class BaseBot: inline_message_id: str = None) -> dict or bool: payload = generate_payload(**locals()) - raw = self.request(api.Methods.SET_GAME_SCORE, payload) - if raw is True: - return raw - return raw + return self.request(api.Methods.SET_GAME_SCORE, payload) async def get_game_high_scores(self, user_id: int, chat_id: int = None, message_id: int = None, inline_message_id: str = None) -> dict: diff --git a/aiogram/bot/bot.py b/aiogram/bot/bot.py index 7ec80d5e..a636ee4a 100644 --- a/aiogram/bot/bot.py +++ b/aiogram/bot/bot.py @@ -1,9 +1,9 @@ import datetime -from ..utils import json import time from .base import BaseBot from .. import types +from ..utils import json class Bot(BaseBot): @@ -130,9 +130,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -166,9 +163,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -193,9 +187,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -216,9 +207,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -238,9 +226,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -282,9 +267,6 @@ class Bot(BaseBot): :param mask_position: :return: """ - if isinstance(mask_position, types.MaskPosition): - mask_position = json.dumps(mask_position.to_json()) - return await super(Bot, self).create_new_sticker_set(name, title, png_sticker, emojis, is_mask, mask_position) async def add_sticker_to_set(self, user_id: int, name: str, png_sticker, emojis: str, @@ -299,8 +281,6 @@ class Bot(BaseBot): :param mask_position: :return: """ - if isinstance(mask_position, types.MaskPosition): - mask_position = json.dumps(mask_position.to_json()) return await super(Bot, self).add_sticker_to_set(user_id, name, png_sticker, emojis, mask_position) async def set_sticker_position_in_set(self, sticker: str, position: int) -> bool: @@ -339,9 +319,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -366,9 +343,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -390,9 +364,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -413,9 +384,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -438,9 +406,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -462,9 +427,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.Serializable` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(reply_to_message_id, 'message_id'): reply_to_message_id = reply_to_message_id.message_id @@ -762,9 +724,6 @@ class Bot(BaseBot): :param reply_markup: :return: :class:`aiogram.types.Message` or bool """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(message_id, 'message_id'): message_id = message_id.message_id @@ -790,9 +749,6 @@ class Bot(BaseBot): :param reply_markup: :return: :class:`aiogram.types.Message` or bool """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(message_id, 'message_id'): message_id = message_id.message_id @@ -816,9 +772,6 @@ class Bot(BaseBot): :param reply_markup: :return: :class:`aiogram.types.Message` or bool """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - if hasattr(message_id, 'message_id'): message_id = message_id.message_id @@ -879,8 +832,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.InlineReplyMarkup` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) prices = json.dumps([item.to_json() for item in prices]) message = await super(Bot, self).send_invoice(chat_id, title, description, payload, provider_token, @@ -935,9 +886,6 @@ class Bot(BaseBot): :param reply_markup: :class:`aiogram.types.InlineKeyboardMarkup` :return: :class:`aiogram.types.Message` """ - if reply_markup and hasattr(reply_markup, 'to_json'): - reply_markup = json.dumps(reply_markup.to_json()) - message = await super(Bot, self).send_game(chat_id, game_short_name, disable_notification, reply_to_message_id, reply_markup) return self.prepare_object(types.Message.de_json(message))