Cies
05/20/2025, 9:13 AMdave
05/20/2025, 7:16 PMfun Uri.with(vararg inject: (Request) -> Request): Uri = Request(GET, this).with(*inject).uri
then...
fun main() {
val time = Path.localDate().of("time")
val id = Path.uuid().of("id")
val sort = Query.boolean().required("sort")
val url = Uri.of("/{id}/{time}").with(time of LocalDate.now(), id of UUID.randomUUID(), sort of true)
println(url)
}
yields:
/dfcbf834-d739-4140-8f03-be365e7fdea8/2025-05-20?sort=true
With a little more finagling, you could possibly apply this to a RoutedHttpHandler, but I haven't checkeddave
05/20/2025, 7:22 PMCies
05/21/2025, 7:45 AMauth::signInHandler
and the Router is "aware" of that. Then building a url for that should be:
val fullUrl = Router.fullUrlFor(request, books::findByIsbnHandler).with(isbn of "123123") // => "<https://b.co/books/by-isbn/123123>"
val relativeUrl = Router.urlFor(books::findByIsbnHandler).with(isbn of "123123", showSimilar = true) // => "<https://b.co/books/by-isbn/123123?showSimilar=true>"
For a route that's defined as "/books/by-isbn/{isbn}"
.
Now if I were to change the route definition to "/books/find-by-isbn/{isbn}"
all would still work. The KTor implementation goes as far to even make the parameters part of the type, so I can change isbn
to isbnCode
with the rename refactor tool and all should still work.natpryce
05/21/2025, 2:05 PMnatpryce
05/21/2025, 2:09 PMCies
05/21/2025, 2:54 PMMikael Ståldal
05/21/2025, 6:52 PMsort of cool
!