Skip to content

Commit

Permalink
Change class name of exceptions and description project
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-gilfanov committed Apr 20, 2021
1 parent 45deb35 commit d751a81
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
aiohttp-sqlalchemy
==================

SQLAlchemy >= 1.4 support for aiohttp.
SQLAlchemy 1.4 / 2.0 support for aiohttp.

Install
-------
Expand Down
21 changes: 12 additions & 9 deletions aiohttp_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import wraps
from sqlalchemy.ext.asyncio import AsyncSession
from typing import TYPE_CHECKING
from aiohttp_sqlalchemy.exceptions import DuplicatedAppKeyError, DuplicatedRequestKeyError
from aiohttp_sqlalchemy.exceptions import DuplicateAppKeyError, DuplicateRequestKeyError

if TYPE_CHECKING:
from aiohttp.web import Application, Request, StreamResponse
Expand All @@ -15,23 +15,26 @@
__version__ = '0.1b1'


def sa_decorator(key: str = 'sa_main'):
DEFAULT_KEY = 'sa_main'


def sa_decorator(key: str = DEFAULT_KEY):
def wrapper(handler):
@wraps(handler)
async def wrapped(*args, **kwargs):
# Class based view decorating
if isinstance(args[0], AbstractView):
request = args[0].request
if key in request:
raise DuplicatedRequestKeyError(key)
raise DuplicateRequestKeyError(key)
async with AsyncSession(request.app[key]) as request[key]:
return await handler(*args, **kwargs)

# Coroutine function decorating
elif iscoroutinefunction(handler):
request = args[0]
if key in request:
raise DuplicatedRequestKeyError(key)
raise DuplicateRequestKeyError(key)
async with AsyncSession(request.app[key]) as request[key]:
return await handler(*args, **kwargs)

Expand All @@ -42,24 +45,24 @@ async def wrapped(*args, **kwargs):
return wrapper


def sa_middleware(key: str = 'sa_main') -> 'Callable':
def sa_middleware(key: str = DEFAULT_KEY) -> 'Callable':
@middleware
async def sa_middleware_(request: 'Request', handler: 'Callable') -> 'StreamResponse':
if key in request:
raise DuplicatedRequestKeyError(key)
raise DuplicateRequestKeyError(key)
async with AsyncSession(request.app[key]) as request[key]:
return await handler(request)
return sa_middleware_


def sa_engine(engine: 'AsyncEngine', key: str = 'sa_main') -> 'Tuple[AsyncEngine, str]':
def sa_engine(engine: 'AsyncEngine', key: str = DEFAULT_KEY) -> 'Tuple[AsyncEngine, str]':
return engine, key


def setup(app: 'Application', engines: 'Iterable[Tuple[AsyncEngine, str]]'):
for engine, key in engines:
if key in app:
raise DuplicatedAppKeyError(key)
raise DuplicateAppKeyError(key)
app[key] = engine


Expand All @@ -68,7 +71,7 @@ class SAViewMixin:

@property
def sa_main_session(self) -> 'AsyncEngine':
return self.request['sa_main']
return self.request[DEFAULT_KEY]


class SAView(View, SAViewMixin):
Expand Down
4 changes: 2 additions & 2 deletions aiohttp_sqlalchemy/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class DuplicatedAppKeyError(ValueError):
class DuplicateAppKeyError(ValueError):
def __init__(self, key):
self.message = f'Duplicated app key `{key}`. Check `engines` argument in `aiohttp_sqlalchemy.setup()` call.'
super().__init__(self.message)


class DuplicatedRequestKeyError(ValueError):
class DuplicateRequestKeyError(ValueError):
def __init__(self, key):
self.message = f'Duplicated request key `{key}`. Check middlewares and decorators from `aiohttp_sqlalchemy`.'
super().__init__(self.message)
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Welcome to aiohttp-sqlalchemy's documentation!
Overview
--------

SQLAlchemy >= 1.4 support for aiohttp.
SQLAlchemy 1.4 / 2.0 support for aiohttp.

Install
-------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "aiohttp-sqlalchemy"
version = "0.1b1"
description = "SQLAlchemy >= 1.4 support for aiohttp."
description = "SQLAlchemy 1.4 / 2.0 support for aiohttp."
authors = [
"Ruslan Ilyasovich Gilfanov <ri.gilfanov@yandex.ru>",
]
Expand Down

0 comments on commit d751a81

Please sign in to comment.