Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class based AWS lambda in Python

I have created a class based AWS lambda function in python called requestHandler.py as below

from action_dispatcher import ActionDispatcher

class RequestHandler(ActionDispatcher):


    @staticmethod
    def createTemplate(event, context):
        return "Hello world"

My action_dispatcher.py is as shown below.

import json

class ActionDispatcher(object):

    def __call__(self, event, context, *args, **kwargs):

        action = event.get('action')
        handler = getattr(self, action, None)

        if handler is None:
            return json.loads({'status': 'error', 'code': 404, 'message':"Action {0} not found.".format(action) })

        return handler(request, *args, **kwargs)

With this above setup and lambda handler as requestHandler.RequestHandler, i get error "RequestHandler() takes no arguments" in this case i create action as createTemplate. so i want to call this method from RequestHandler.

like image 570
santosh Avatar asked Oct 20 '25 05:10

santosh


1 Answers

It looks to me like you are trying to call your class instead of an instance of the class. RequestHandler() will call the __init__ method to initialize an instance of the class. Since you haven't defined the method it doesn't take any arguments. To access __call__ you need to call an instance of your class.

handler = RequestHandler()
result = handler(request, context, *args, **kwargs)
like image 124
Jacinator Avatar answered Oct 21 '25 18:10

Jacinator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!