Can't write new plugin - plugin is not iterable
dddenes opened this issue ยท 4 comments
Hi!
I am quite new to Starlette and FastAPI, and I might have holes in my knowledge, however I was not able to write a new plugin.
from fastapi import FastAPI
from fastapi.middleware import Middleware
from starlette_context.header_keys import HeaderKeys
from starlette_context.plugins import Plugin
class LangPlugin(Plugin):
print('****')
middleware = [
Middleware(
RawContextMiddleware,
plugins(
LangPlugin()
)
)
]
app = FastAPI(middleware=middleware)
And it says:
File ".../starlette_context/middleware/raw_middleware.py", line 17, in __init__
if not all([isinstance(plugin, Plugin) for plugin in self.plugins]):
TypeError: 'LangPlugin' object is not iterable
How is my plugin not instance of Plugin, I wonder. Or I guess this is the problem.
To put you into context, I would like to get from here an optional string param, (/path-to-api-endpoint/?lang=en) to access the request language from everywhere. Or maybe you have a more straightforward solution to my problem, what I did not recognize?
You're missing =
.
Change
middleware = [
Middleware(
RawContextMiddleware,
plugins(
LangPlugin()
)
)
]
to
middleware = [
Middleware(
RawContextMiddleware,
plugins=(
LangPlugin()
)
)
]
Sorry for the wrong "copy-paste", the =
after the plugins lost somewhen, when I minimalized the problem. I have it, and if I remove it, I get a different TypeError: 'module' object is not callable
.
So the middleware what you wrote above looks just like mine right now, and I still get TypeError: 'LangPlugin' object is not iterable
.
One element tuple need a comma to still be a tuple. Add comma after LangPlugin
.
middleware = [
Middleware(
RawContextMiddleware,
plugins=(
LangPlugin(),
)
)
]
Ahh, I go hide somewhere, user error indeed. Thank you for your patience!