0

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.

1 Answer 1

1

Can you not just use @inject and then:

search(data_provider: RestaurantsProvider)
1
  • Thanks a lot. Once it's out there, I feel a lot stupid to not be able to figure this out myself.
    – Navendra
    Commented Apr 12, 2018 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.