Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
ISSUE #457
Browse files Browse the repository at this point in the history
* Rename `USER_CONTEXT_VAR` as `REQUEST_USER_CONTEXT_VAR`.
* Rename `REPLY_TOPIC_CONTEXT_VAR` as `REQUEST_REPLY_TOPIC_CONTEXT_VAR`.
* Rename `HEADERS_CONTEXT_VAR` as `REQUEST_HEADERS_CONTEXT_VAR`.
  • Loading branch information
Sergio García Prado committed Nov 29, 2021
1 parent 6f16aae commit 708dc17
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 32 deletions.
6 changes: 3 additions & 3 deletions minos/networks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
__version__ = "0.3.0"

from .brokers import (
HEADERS_CONTEXT_VAR,
REPLY_TOPIC_CONTEXT_VAR,
REQUEST_HEADERS_CONTEXT_VAR,
REQUEST_REPLY_TOPIC_CONTEXT_VAR,
BrokerConsumer,
BrokerConsumerService,
BrokerHandler,
Expand Down Expand Up @@ -57,7 +57,7 @@
MinosRedefinedEnrouteDecoratorException,
)
from .requests import (
USER_CONTEXT_VAR,
REQUEST_USER_CONTEXT_VAR,
Request,
Response,
ResponseException,
Expand Down
4 changes: 2 additions & 2 deletions minos/networks/brokers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
BrokerResponseException,
)
from .messages import (
HEADERS_CONTEXT_VAR,
REPLY_TOPIC_CONTEXT_VAR,
REQUEST_HEADERS_CONTEXT_VAR,
REQUEST_REPLY_TOPIC_CONTEXT_VAR,
BrokerMessage,
BrokerMessageStatus,
BrokerMessageStrategy,
Expand Down
6 changes: 3 additions & 3 deletions minos/networks/brokers/dynamic/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
BrokerConsumer,
)
from ..messages import (
REPLY_TOPIC_CONTEXT_VAR,
REQUEST_REPLY_TOPIC_CONTEXT_VAR,
)
from ..publishers import (
BrokerPublisher,
Expand Down Expand Up @@ -150,9 +150,9 @@ def __init__(self, wrapper: AsyncContextManager[DynamicBroker]):

async def __aenter__(self) -> DynamicBroker:
handler = await self.wrapper.__aenter__()
self._token = REPLY_TOPIC_CONTEXT_VAR.set(handler.topic)
self._token = REQUEST_REPLY_TOPIC_CONTEXT_VAR.set(handler.topic)
return handler

async def __aexit__(self, exc_type, exc_val, exc_tb):
REPLY_TOPIC_CONTEXT_VAR.reset(self._token)
REQUEST_REPLY_TOPIC_CONTEXT_VAR.reset(self._token)
await self.wrapper.__aexit__(exc_type, exc_val, exc_tb)
18 changes: 9 additions & 9 deletions minos/networks/brokers/handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@
MinosActionNotFoundException,
)
from ...requests import (
USER_CONTEXT_VAR,
REQUEST_USER_CONTEXT_VAR,
Response,
ResponseException,
)
from ...utils import (
consume_queue,
)
from ..messages import (
HEADERS_CONTEXT_VAR,
REQUEST_HEADERS_CONTEXT_VAR,
BrokerMessage,
BrokerMessageStatus,
)
Expand Down Expand Up @@ -329,25 +329,25 @@ def get_callback(

async def _fn(raw: BrokerMessage) -> tuple[Any, BrokerMessageStatus, dict[str, str]]:
request = BrokerRequest(raw)
user_token = USER_CONTEXT_VAR.set(request.user)
headers_token = HEADERS_CONTEXT_VAR.set(raw.headers)
user_token = REQUEST_USER_CONTEXT_VAR.set(request.user)
headers_token = REQUEST_HEADERS_CONTEXT_VAR.set(raw.headers)

try:
response = fn(request)
if isawaitable(response):
response = await response
if isinstance(response, Response):
response = await response.content()
return response, BrokerMessageStatus.SUCCESS, HEADERS_CONTEXT_VAR.get()
return response, BrokerMessageStatus.SUCCESS, REQUEST_HEADERS_CONTEXT_VAR.get()
except ResponseException as exc:
logger.warning(f"Raised an application exception: {exc!s}")
return repr(exc), BrokerMessageStatus.ERROR, HEADERS_CONTEXT_VAR.get()
return repr(exc), BrokerMessageStatus.ERROR, REQUEST_HEADERS_CONTEXT_VAR.get()
except Exception as exc:
logger.exception(f"Raised a system exception: {exc!r}")
return repr(exc), BrokerMessageStatus.SYSTEM_ERROR, HEADERS_CONTEXT_VAR.get()
return repr(exc), BrokerMessageStatus.SYSTEM_ERROR, REQUEST_HEADERS_CONTEXT_VAR.get()
finally:
USER_CONTEXT_VAR.reset(user_token)
HEADERS_CONTEXT_VAR.reset(headers_token)
REQUEST_USER_CONTEXT_VAR.reset(user_token)
REQUEST_HEADERS_CONTEXT_VAR.reset(headers_token)

return _fn

Expand Down
4 changes: 2 additions & 2 deletions minos/networks/brokers/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
DeclarativeModel,
)

REPLY_TOPIC_CONTEXT_VAR: Final[ContextVar[Optional[str]]] = ContextVar("reply_topic", default=None)
HEADERS_CONTEXT_VAR: Final[ContextVar[Optional[dict[str, str]]]] = ContextVar("headers", default=None)
REQUEST_REPLY_TOPIC_CONTEXT_VAR: Final[ContextVar[Optional[str]]] = ContextVar("reply_topic", default=None)
REQUEST_HEADERS_CONTEXT_VAR: Final[ContextVar[Optional[dict[str, str]]]] = ContextVar("headers", default=None)


class BrokerMessage(DeclarativeModel):
Expand Down
3 changes: 1 addition & 2 deletions minos/networks/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
MinosException,
)

USER_CONTEXT_VAR: Final[ContextVar[Optional[UUID]]] = ContextVar("user", default=None)
USER_CONTEXT_VAR.set(None) # needed to "register" the context variable.
REQUEST_USER_CONTEXT_VAR: Final[ContextVar[Optional[UUID]]] = ContextVar("user", default=None)


class Request(ABC):
Expand Down
6 changes: 3 additions & 3 deletions minos/networks/rest/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
EnrouteBuilder,
)
from ..requests import (
USER_CONTEXT_VAR,
REQUEST_USER_CONTEXT_VAR,
Response,
ResponseException,
)
Expand Down Expand Up @@ -128,7 +128,7 @@ async def _fn(request: web.Request) -> web.Response:
logger.info(f"Dispatching '{request!s}' from '{request.remote!s}'...")

request = RestRequest(request)
token = USER_CONTEXT_VAR.set(request.user)
token = REQUEST_USER_CONTEXT_VAR.set(request.user)

try:
response = fn(request)
Expand All @@ -146,7 +146,7 @@ async def _fn(request: web.Request) -> web.Response:
logger.exception(f"Raised a system exception: {exc!r}")
raise web.HTTPInternalServerError()
finally:
USER_CONTEXT_VAR.reset(token)
REQUEST_USER_CONTEXT_VAR.reset(token)

return _fn

Expand Down
8 changes: 4 additions & 4 deletions tests/test_networks/test_brokers/test_dynamic/test_pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
PostgresAsyncTestCase,
)
from minos.networks import (
REPLY_TOPIC_CONTEXT_VAR,
REQUEST_REPLY_TOPIC_CONTEXT_VAR,
BrokerConsumer,
BrokerPublisher,
DynamicBroker,
Expand Down Expand Up @@ -70,12 +70,12 @@ async def test_acquire(self):
self.assertIn(broker.topic, self.pool.client.list_topics())

async def test_acquire_reply_topic_context_var(self):
self.assertEqual(None, REPLY_TOPIC_CONTEXT_VAR.get())
self.assertEqual(None, REQUEST_REPLY_TOPIC_CONTEXT_VAR.get())

async with self.pool.acquire() as broker:
self.assertEqual(broker.topic, REPLY_TOPIC_CONTEXT_VAR.get())
self.assertEqual(broker.topic, REQUEST_REPLY_TOPIC_CONTEXT_VAR.get())

self.assertEqual(None, REPLY_TOPIC_CONTEXT_VAR.get())
self.assertEqual(None, REQUEST_REPLY_TOPIC_CONTEXT_VAR.get())


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
PostgresAsyncTestCase,
)
from minos.networks import (
USER_CONTEXT_VAR,
REQUEST_USER_CONTEXT_VAR,
BrokerHandler,
BrokerHandlerEntry,
BrokerMessage,
Expand Down Expand Up @@ -313,7 +313,7 @@ async def test_get_callback_raises_exception(self):
async def test_get_callback_with_user(self):
async def _fn(request) -> None:
self.assertEqual(self.user, request.user)
self.assertEqual(self.user, USER_CONTEXT_VAR.get())
self.assertEqual(self.user, REQUEST_USER_CONTEXT_VAR.get())

mock = AsyncMock(side_effect=_fn)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_networks/test_rest/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
PostgresAsyncTestCase,
)
from minos.networks import (
USER_CONTEXT_VAR,
REQUEST_USER_CONTEXT_VAR,
Request,
Response,
RestHandler,
Expand Down Expand Up @@ -98,7 +98,7 @@ async def test_get_callback_with_user(self):

async def _fn(request) -> None:
self.assertEqual(user, request.user)
self.assertEqual(user, USER_CONTEXT_VAR.get())
self.assertEqual(user, REQUEST_USER_CONTEXT_VAR.get())

mock = AsyncMock(side_effect=_fn)

Expand Down

0 comments on commit 708dc17

Please sign in to comment.