Hello everyone,
I’m quite new to Python and even newer to asyncio. But I like the thinking of Sanic, it reminds me a lot of Silex (from the PHP world). Anyways, this small intro is here to show that my reasoning about web app development is a bit biased towards the PHP / Java world. So what is in my head how to solve the following problem, is probably not really Pythonic, but let’s see.
I created a UserLibrary
that has several methods, such as async def get_tenant(self, tenant_id)
and async def create_ticket(self, subject, content)
. I instantiated this library under app.ctx.user_library
, just after where I create my Sanic app
instance. Is this the right way to create such a variable?
This user library is used by some inner platform, where the user writes their own Python. We massage that Python code into a Playbook
, that looks like this:
class Playbook:
async def run(self, u):
# Some user provided Python
result = {}
# ...
tenant = await u.get_tenant(123)
# ...
return result
‘We’ call run(app.ctx.user_library)
. The users know that they are able to call tenant = await u.get_tenant(123)
to retrieve a tenant.
Now the user library does a HTTP call using httpx when get_tenant
is issued. What I now want to do, is to include some of the original HTTP request headers (request ID, tracing headers, etc. etc.) in this outgoing HTTP request. But I do not want to bother the users by providing this context, or them passing along all these variables.
So I was wondering whether you can somehow retrieve Sanic’s current request from inside the UserLibrary
?