mercredi 13 octobre 2021

Strategy+Factory Pattern when implementing lambda handlers

I'm using serverless framework which means I would need to write separate handlers for each function I wish to expose.

For now, I'm planning to keep all the CRUD endpoints for same models in the same handlers to optimize a little for cold starts.

So my handler class looks something like this :

class Handler : RequestHandler<Map<String, Any>, ApiGatewayResponse> {


    override fun handleRequest(input: Map<String, Any>, context: Context): ApiGatewayResponse {

        val method = input["httpMethod"] as String
        val path = input["path"] as String


        if (method == "GET" && path.startsWith("xyz")) {

            //impl

        } else if (method == "POST" && path == "abv") {

            //impl

        } else if (method == "DELETE" && path.startsWith("xyz")) {

            //impl

        } else if (method == "POST" && path == "123") {
             //impl


        } else if (method == "GET" && path.startsWith("abc") && path.endsWith("asd") ) {

            //impl

        } else {
            //impl
        }

    }

}

If in every condition I was to compare path value and not partial comparison, I could've used a requestObject containing both method and path and created a Registry class containing all the mappings from different requestObjects to different implementations.

However, since here I also have partial comparisons, I cannot directly insert passed path into the object. How should I proceed here? Any design pattern I can use here?

Aucun commentaire:

Enregistrer un commentaire