io.polyglotted.common.web.WebHttpRequest Maven / Gradle / Ivy
package io.polyglotted.common.web;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ListMultimap;
import io.polyglotted.common.model.MapResult;
import lombok.RequiredArgsConstructor;
import java.util.Map;
import static io.polyglotted.common.util.MapRetriever.STRING_LIST_CLASS;
import static io.polyglotted.common.util.StrUtil.toLower;
@RequiredArgsConstructor
public final class WebHttpRequest {
public final HttpMethod method;
public final String uriPath;
public final String body; //could be base64Encoded
public final Map requestContext;
public final ListMultimap headers;
public final ListMultimap queryParams;
static WebHttpRequest from(MapResult event) {
return new WebHttpRequest(HttpMethod.valueOf(event.reqdStr("httpMethod")), event.reqdStr("path"), event.optStr("body"),
event.mapVal("requestContext"), buildParams(event, "headers", "multiValueHeaders", true),
buildParams(event, "queryStringParameters", "multiValueQueryStringParameters", false));
}
private static ListMultimap buildParams(MapResult event, String singleParam, String multiParam, boolean lower) {
ImmutableListMultimap.Builder builder = ImmutableListMultimap.builder();
for (Map.Entry entry : event.mapVal(singleParam).entrySet()) {
builder.put(lower ? toLower(entry.getKey()) : entry.getKey(), (String) entry.getValue());
}
for (Map.Entry entry : event.mapVal(multiParam).entrySet()) {
builder.putAll(lower ? toLower(entry.getKey()) : entry.getKey(), STRING_LIST_CLASS.cast(entry.getValue()));
}
return builder.build();
}
}