Created
June 10, 2018 17:25
-
-
Save juancho088/1b8a308cda44def9155e586794b155d2 to your computer and use it in GitHub Desktop.
New Handler Implementation for a Kotlin Serverless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myblockbuster | |
import com.amazonaws.services.lambda.runtime.Context | |
import com.amazonaws.services.lambda.runtime.RequestHandler | |
import com.myblockbuster.ApiGatewayResponse.Companion.LOG | |
import com.myblockbuster.core.* | |
import com.myblockbuster.core.dispatchers.RequestDispatcher | |
open class Handler: RequestHandler<Map<String, Any>, ApiGatewayResponse> { | |
var requestDispatcher: RequestDispatcher = RequestDispatcher() | |
override fun handleRequest(input:Map<String, Any>, context: Context): ApiGatewayResponse { | |
var status = 500 | |
var body: Any? = EmptyModel() | |
try { | |
body = requestDispatcher.locate(ApiGatewayRequest(input, context)) ?: EmptyModel() | |
status = when (body is EmptyModel) { | |
true -> 204 | |
false -> 200 | |
} | |
} | |
catch (e: MyException) { | |
LOG.error(e.message, e) | |
status = e.code | |
body = ErrorModel(e.message) | |
} | |
catch (e: Throwable) { | |
LOG.error(e.message, e) | |
status = 500 | |
body = ErrorModel("Internal server error") | |
} | |
finally { | |
return ApiGatewayResponse.build { | |
statusCode = status | |
if (body is Model) | |
objectBody = body as Model? | |
else | |
listBody = body as List<Any> | |
headers = mapOf("X-Powered-By" to "AWS Lambda & Serverless") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment