Skip to content

Commit 65eb1db

Browse files
committed
Provide easier access to path parameters
1 parent d4890b0 commit 65eb1db

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
2828
if (matchResult.match) {
2929
val handler: HandlerFunction<Any, Any> = routerFunction.handler
3030
val requestBody = deserializeRequest(handler, input)
31-
val request = Request(input, requestBody)
31+
val request = Request(input, requestBody, routerFunction.requestPredicate.pathPattern)
3232
val response = router.filter.then(handler as HandlerFunction<*, *>).invoke(request)
3333
return createResponse(input, response)
3434
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,8 @@ data class RouterFunction<I, T>(
8585
val handler: HandlerFunction<I, T>
8686
)
8787

88-
data class Request<I>(val apiRequest: APIGatewayProxyRequestEvent, val body: I)
88+
data class Request<I>(val apiRequest: APIGatewayProxyRequestEvent, val body: I, val pathPattern: String) {
89+
90+
val pathParameters by lazy { UriTemplate.from(pathPattern).extract(apiRequest.path) }
91+
fun getPathParameter(name: String) = pathParameters[name]
92+
}

src/test/kotlin/com/github/mduesterhoeft/router/RequestHandlerTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class RequestHandlerTest {
188188
ResponseEntity.ok(TestResponse("Hello"))
189189
}
190190
GET("/some/{id}") { r: Request<Unit> ->
191-
ResponseEntity.ok(TestResponse("Hello ${UriTemplate.from("/some/{id}").extract(r.apiRequest.path)["id"]}"))
191+
ResponseEntity.ok(TestResponse("Hello ${r.getPathParameter("id")}"))
192192
}
193193
GET("/some-proto") { _: Request<Unit> ->
194194
ResponseEntity.ok(Sample.newBuilder().setHello("Hello").build())

0 commit comments

Comments
 (0)