package com.shmenkins.infra.aws.lambda;

import java.util.Collections;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import com.shmenkins.core.handler.BasicHandler;
import com.shmenkins.core.handler.http.HttpRequest;
import com.shmenkins.core.handler.http.HttpResponse;

public abstract class ApiGwLambda {

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	private final BasicHandler<HttpRequest, HttpResponse> handler;

	protected ApiGwLambda(BasicHandler<HttpRequest, HttpResponse> handler) {
		// all logs from this instance can be identified using this value
		MDC.put("lambdaInstanceId", UUID.randomUUID().toString());

		this.handler = handler;

		log.info("Instantiated; handler={}", handler);
	}

	public Map<Object, Object> handle(Map<Object, Object> apiGwRequest) {

		log.debug("Handling; request={}", apiGwRequest);

		HttpRequest httpRequest = apiGwToHttpRequest(apiGwRequest);

		try {
			HttpResponse httpResponse = handler.handle(httpRequest);
			Map<Object, Object> apiGwResponse = httpToApiGwResponse(httpResponse);
			return apiGwResponse;
		} catch (Throwable e) {
			log.error("Cannot handle apigw request; request=" + apiGwRequest, e);
			throw new RuntimeException("500", e);
		}
	}

	private HttpRequest apiGwToHttpRequest(Map<Object, Object> apiGwRequest) {
		log.trace("ApiGwRequest={}", apiGwRequest);
		return new HttpRequest();
	}

	private Map<Object, Object> httpToApiGwResponse(HttpResponse httpResponse) {
		return Collections.emptyMap();
	}

}

@@ -381,16 +381,159 @@
 mbda %7B%0A%0A
+%09static %7B%0A%09%09// all logs from this instance can be identified using this value%0A%09%09MDC.put(%22lambdaInstanceId%22, UUID.randomUUID().toString());%0A%09%7D%0A%0A
 %09private
@@ -738,138 +738,8 @@
 ) %7B%0A
-%09%09// all logs from this instance can be identified using this value%0A%09%09MDC.put(%22lambdaInstanceId%22, UUID.randomUUID().toString());%0A%0A
 %09%09th
@@ -760,17 +760,16 @@
 andler;%0A
-%0A
 %09%09log.in
@@ -1583,11 +1583,12 @@
 p();%0A%09%7D%0A
+%09
 %0A%7D%0A

Initialize MDC in static context