Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running In Serverless Lambda Using serverless-wsgi #54

Closed
AhmedNourJamalElDin opened this issue Mar 11, 2020 · 5 comments
Closed

Running In Serverless Lambda Using serverless-wsgi #54

AhmedNourJamalElDin opened this issue Mar 11, 2020 · 5 comments

Comments

@AhmedNourJamalElDin
Copy link

Hello,
Is there a way to use this package without app.run() where serverless doesnt require running the app using the aforementioned command?

When using this package, for some reason I can't use "contructor injection", nor injector.get(INTERFACE) but this works:

flask_injector = FlaskInjector(app, modules=modules)

# some code
# some other code
# also some other code

flask_injector.get(INTERFACE)

Thanks :D

@jstasiak
Copy link
Collaborator

I'm not entirely sure what this question is about. It'd help if you could provide

  • a minimal, complete piece of code to reproduce the issue
  • the interaction with the piece of code (the way you run it etc.) to reproduce the issue
  • what the issue is (stack trace)
  • the versions of the software you're running (Python, Flask, Flask-Injector, injector)

@AhmedNourJamalElDin
Copy link
Author

AhmedNourJamalElDin commented Mar 12, 2020

  • a minimal, complete piece of code to reproduce the issue

init_app.py

from flask import Flask
from flask_restful import Api
from flask_injector import FlaskInjector
from injector import Module, singleton, provider

app = Flask(__name__)
api = Api(app)

class InjectorModule(Module):
    @singleton
    @provider
    def provide_email_client(self) -> EmailClient:
        return SesClient(some_region)

flask_injector = FlaskInjector(app=app, modules=[InjectorModule])

from src.resources import *
app.register_blueprint(routes)

app.app_context().push()

some resource file

class SomeResource(Resource):
    @inject
    def __init__(self, email_client: EmailClient):
          self.email_client = email_client

    def post(self):
        # some code using self.email_client
  • what the issue is (stack trace)

the exception:

TypeError: __init__() missing 1 required positional argument: 'email_client'
Stacktrace:
Traceback (most recent call last):
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router
    return original_handler(e)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router
    return original_handler(e)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask_restful/__init__.py", line 458, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/anjd/.local/share/virtualenvs/nfx-ares-0nfxTZOd/lib/python3.7/site-packages/flask/views.py", line 88, in view
    self = view.view_class(*class_args, **class_kwargs)
TypeError: __init__() missing 1 required positional argument: 'email_client'
  • the versions of the software you're running (Python, Flask, Flask-Injector, injector)

Python: 3.7
Flask: 1.1.1 and 1.0.2
Flask-RESTful: 0.3.7
injector: 0.18.3
Flask-Injector: 0.12.3

Anything else? :D

TBH the code doesn't run app.run() at all because, you know, it's AWS Lambda, which acts like a normal python function which takes two arguments: event and context. Some wrappers are there to integrate AWS Lambda with WSGI hence it works with Flask, and one of them is serverless-wsgi which I use.

@jstasiak
Copy link
Collaborator

app.run() shouldn't matter, Flask-Injector doesn't need it per se. I don't really know AWS Lambda, but what stands out from your example (which still seems incomplete so I can't run it) is this part:

flask_injector = FlaskInjector(app=app, modules=[InjectorModule])

from src.resources import *
app.register_blueprint(routes)

Registering all views (even those owned by blueprints) needs to happen before FlaskInjector instance is created, otherwise they're not gonna be injected into.

@AhmedNourJamalElDin
Copy link
Author

Hollllllllllllllly :D

My bad, this solved my problem.

Thanks soooooooooooooooooo much <3

@jstasiak
Copy link
Collaborator

No worries, good to hear it's working for you now. Also this should be documented better I think :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants