Polymorphic Python

Important: Since Jets v5, Polymorphic support is no longer supported with Controllers. It is supported in other types of classes like Jobs though. The reason it is no longer supported in Controllers is because only one Lambda Function is used for Jets Controllers in Jets v5. The docs below will be updated in time.

Polymorphic support for python works like so for the controller code:

app/controllers/posts_controller.rb:

class PostsController < ApplicationController
  python :python_example
end

You add your corresponding python code in the posts_controller/python folder:

app/controllers/posts_controller/python/python_example.py:

from pprint import pprint
import json
import platform

def lambda_handler(event, context):
    message = 'PostsController#python_example hi from python %s' % platform.python_version()
    return response({'message': message}, 200)

def response(message, status_code):
    return {
        'statusCode': str(status_code),
        'body': json.dumps(message),
        'headers': {
                'Content-Type': 'application/json'
            },
        }

Notice, how with the python code, you must handle returning the proper lambda proxy structure to API Gateway.

Lambda console

On the function show page:

Default Handler Name

The default python handler name is lambda_handler. The default can be changed with the handler method. Example:

app/controllers/posts_controller.rb:

class PostsController < ApplicationController
  handler :handle
  python :python_example
end

The python code would then look something like this:

def handle(event, context):
  ...
end

You can also set the handler for the entire class. Example:

class PostsController < ApplicationController
  class_handler :handle
  # ...
end