sanic-prometheus icon indicating copy to clipboard operation
sanic-prometheus copied to clipboard

'Request' object does not support item assignment

Open SakuraSound opened this issue 4 years ago • 3 comments

Sanic no longer allows for item assignment in the request object.

I am using sanic==20.6.3 and sanic-prometheus==0.2.0

ERROR:sanic.root:Exception occurred while handling uri: 'http://localhost:8080/status/ok'
Traceback (most recent call last):
  File "/home/test/.local/lib/python3.8/site-packages/sanic/app.py", line 906, in handle_request
    response = await self._run_request_middleware(
  File "/home/test/.local/lib/python3.8/site-packages/sanic/app.py", line 1265, in _run_request_middleware
    response = await response
  File "/home/test/.local/lib/python3.8/site-packages/sanic_prometheus/__init__.py", line 120, in before_request
    metrics.before_request_handler(request)
  File "/home/test/.local/lib/python3.8/site-packages/sanic_prometheus/metrics.py", line 52, in before_request_handler
    request['__START_TIME__'] = time.time()
TypeError: 'Request' object does not support item assignment
Traceback (most recent call last):
  File "/home/test/.local/lib/python3.8/site-packages/sanic/app.py", line 906, in handle_request
    response = await self._run_request_middleware(
  File "/home/test/.local/lib/python3.8/site-packages/sanic/app.py", line 1265, in _run_request_middleware
    response = await response
  File "/home/test/.local/lib/python3.8/site-packages/sanic_prometheus/__init__.py", line 120, in before_request
    metrics.before_request_handler(request)
  File "/home/test/.local/lib/python3.8/site-packages/sanic_prometheus/metrics.py", line 52, in before_request_handler
    request['__START_TIME__'] = time.time()
TypeError: 'Request' object does not support item assignment

SakuraSound avatar Sep 22 '20 16:09 SakuraSound

Same here w/ Sanic 20.9.0

koug44 avatar Oct 16 '20 15:10 koug44

@SakuraSound Any luck? how you resolved this error ?

ashshakya avatar Oct 28 '22 17:10 ashshakya

Sanic app and request have slots so normally you can't add extra param there. But all of this objects have ctx param (context). And you can add all you want there.

For example current user object or smth else.

request.ctx.user = user

Of course you need to refactor your code to use it, but it's ok I think

mrkiril avatar Oct 11 '23 12:10 mrkiril