I am working on an API app using flask-injector and connexion. I have following setups -
The Interface -
class RestaurantsProvider(Module):
@inject
def __init__(self, items: list=[]):
self._items = items
@provider
def get(self, number_of_items: int=5) -> list:
if not self._items:
return []
if number_of_items > len(self._items):
number_of_items = len(self._items)
return self._items
The binding -
from connexion.resolver import RestyResolver
import connexion
from injector import Binder, CallableProvider
from flask_injector import FlaskInjector
from services.provider import RestaurantsProvider
def configure(binder: Binder) -> Binder:
binder.bind(
interface=RestaurantsProvider.RestaurantsProvider,
to= RestaurantsProvider.RestaurantsProvider([{"Name":"Fgh"}])
);
if __name__ == '__main__':
app = connexion.App(__name__, port=2508,
specification_dir='swagger/')
app.add_api('restaurants.yaml', resolver=RestyResolver('api'))
# app.run(ssl_context=('cert.pem','key.pem'))
FlaskInjector(app=app.app, modules=[configure])
app.run()
And Now I want to inject this interface in a function like this
@inject(data_provider = RestaurantsProvider)
def search(data_provider) -> list:
return data_provider.get()
But the newer version of injector (0.13.0) doesn't allow parameterized injection and I went through the documentation and couldn't figure out how to inject the interface to this function.
Note - This function is in api.restaurants.py and is getting called as expected.