Remove "context" middleware. Rename "bot" middleware to "environment".

This commit is contained in:
Alex Root Junior 2018-08-09 19:37:09 +03:00
parent a992f670c1
commit b2d202bb5d
3 changed files with 7 additions and 118 deletions

View file

@ -70,9 +70,12 @@ class AbstractConnector(abc.ABC):
- The content of the result is invalid JSON.
- The method call was unsuccessful (The JSON 'ok' field equals False)
:raises ApiException: if one of the above listed cases is applicable
:param method_name: The name of the method called
:return: The result parsed to a JSON dictionary.
:param status_code: status code
:param content_type: content type of result
:param body: result body
:return: The result parsed to a JSON dictionary
:raises ApiException: if one of the above listed cases is applicable
"""
log.debug(f"Response for {method_name}: [{status_code}] {body}")

View file

@ -1,114 +0,0 @@
from aiogram import types
from aiogram.dispatcher.middlewares import BaseMiddleware
OBJ_KEY = '_context_data'
class ContextMiddleware(BaseMiddleware):
"""
Allow to store data at all of lifetime of Update object
"""
async def on_pre_process_update(self, update: types.Update, data: dict):
"""
Start of Update lifetime
:param update:
:return:
"""
self._configure_update(update)
async def on_post_process_update(self, update: types.Update, result, data: dict):
"""
On finishing of processing update
:param update:
:param result:
:return:
"""
if OBJ_KEY in update.conf:
del update.conf[OBJ_KEY]
def _configure_update(self, update: types.Update = None):
"""
Setup data storage
:param update:
:return:
"""
obj = update.conf[OBJ_KEY] = {}
return obj
def _get_dict(self):
"""
Get data from update stored in current context
:return:
"""
update = types.Update.current()
obj = update.conf.get(OBJ_KEY, None)
if obj is None:
obj = self._configure_update(update)
return obj
def __getitem__(self, item):
"""
Item getter
:param item:
:return:
"""
return self._get_dict()[item]
def __setitem__(self, key, value):
"""
Item setter
:param key:
:param value:
:return:
"""
data = self._get_dict()
data[key] = value
def __iter__(self):
"""
Iterate over dict
:return:
"""
return self._get_dict().__iter__()
def keys(self):
"""
Iterate over dict keys
:return:
"""
return self._get_dict().keys()
def values(self):
"""
Iterate over dict values
:return:
"""
return self._get_dict().values()
def get(self, key, default=None):
"""
Get item from dit or return default value
:param key:
:param default:
:return:
"""
return self._get_dict().get(key, default)
def export(self):
"""
Export all data s dict
:return:
"""
return self._get_dict()

View file

@ -1,9 +1,9 @@
from aiogram.dispatcher.middlewares import BaseMiddleware
class BotMiddleware(BaseMiddleware):
class EnvironmentMiddleware(BaseMiddleware):
def __init__(self, context=None):
super(BotMiddleware, self).__init__()
super(EnvironmentMiddleware, self).__init__()
if context is None:
context = {}