Skip to content

Commit ca71b63

Browse files
committed
Add more convenience methods to access request and response data
1 parent e4b2863 commit ca71b63

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/main/kotlin/com/github/mduesterhoeft/router/APIGatewayProxyEventExtensions.kt

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ import java.util.Base64
77
fun APIGatewayProxyRequestEvent.acceptHeader() = getHeaderCaseInsensitive("accept")
88
fun APIGatewayProxyRequestEvent.contentType() = getHeaderCaseInsensitive("content-type")
99

10-
fun APIGatewayProxyRequestEvent.getHeaderCaseInsensitive(httpHeader: String): String? {
11-
return headers.entries
12-
.firstOrNull { it.key.toLowerCase() == httpHeader.toLowerCase() }
10+
fun APIGatewayProxyRequestEvent.getHeaderCaseInsensitive(httpHeader: String): String? =
11+
getCaseInsensitive(httpHeader, headers)
12+
13+
fun APIGatewayProxyResponseEvent.getHeaderCaseInsensitive(httpHeader: String): String? =
14+
getCaseInsensitive(httpHeader, headers)
15+
16+
private fun getCaseInsensitive(key: String, map: Map<String, String>): String? =
17+
map.entries
18+
.firstOrNull { it.key.toLowerCase() == key.toLowerCase() }
1319
?.value
14-
}
1520

1621
fun APIGatewayProxyResponseEvent.bodyAsBytes() = Base64.getDecoder().decode(body)
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.github.mduesterhoeft.router
22

3+
import java.net.URI
4+
35
data class ResponseEntity<T>(
46
val statusCode: Int,
57
val body: T? = null,
68
val headers: Map<String, String> = emptyMap()
79
) {
810
companion object {
911
fun <T> ok(body: T? = null, headers: Map<String, String> = emptyMap()) =
10-
ResponseEntity<T>(200, body, headers
11-
)
12+
ResponseEntity<T>(200, body, headers)
13+
14+
fun <T> created(body: T? = null, location: URI, headers: Map<String, String> = emptyMap()) =
15+
ResponseEntity<T>(201, body, headers + ("location" to location.toString()))
16+
17+
fun noContent(headers: Map<String, String> = emptyMap()) =
18+
ResponseEntity<Unit>(204, null, headers)
1219
}
1320
}

0 commit comments

Comments
 (0)