|
Pythomnic » Documentation » Request execution context (implementation)
Whenever an interface accepts a requests,
it creates a new context and populates it with a few values doing something like this:
pmnc_ctx = {}
pmnc_ctx.set("request_interface", "__http__")
pmnc_ctx.set("request_id", "0123456789ABCDEF")
pmnc_ctx.set("request_auth_tokens", { "ip": "1.2.3.4" })
pmnc_ctx.set("request_arrival_time", time())
pmnc_ctx.set("request_deadline", time() + request_timeout)
from that moment on, any application module, either on the same
cage or on different cage, can access the context, for example:
assert pmnc_ctx.get("request_interface") == "__http__"
The context can be modified, and the changes that your code makes
stick and propagate across local calls or cage-to-cage calls.
For example, this is how you may implement the mentioned real vs.
test request filtering:
# in http_interface_receiver.py:
def incoming_request(source_interface, ...):
pmnc_ctx.set("test_request",
source_interface == "http_interface_test")
...
# anywhere else:
if not pmnc_ctx.get("test_request"):
# send completion notification e-mail
Because the context is shared by all the modules at all times,
the changes to the context propagate both ways, if the called
module modifies the context and then returns, the caller's
context just got updated, for example:
pmnc.security.authorize()
assert pmnc_ctx.get("client_role") == "admin", "access denied"
...
# in security.py:
def authorize():
if pmnc_ctx.get("request_auth_tokens")["ip"] == "127.0.0.1":
pmnc_ctx.set("client_role", "admin")
else:
pmnc_ctx.set("client_role", "nobody")
|
Features Download Documentation Tutorial |