https://kotlinlang.org logo
#ktor
Title
# ktor
w

Werner Guttmann

02/23/2022, 3:08 PM
HI; Ktor (1.6.7) provides a url {} dsl to facilitate building new urls based on e.g. incoming requests, etc. Unfortunately, this only returns the url as String; would it be possible to allow returning a Url instead of the String ?
a

Aleksei Tirman [JB]

02/24/2022, 9:04 AM
Could you please share a code snippet for clarifying your question?
w

Werner Guttmann

02/24/2022, 9:10 AM
Sure:
Copy code
val newUrl = url {
        path(language.name, areasTerm(language = language))
    }
always returns a String, which makes it hard to access e.g. just the full path of that Url instead everything. Right now I have to fall back to
Copy code
val url = URLBuilder(
        parameters = ParametersBuilder().apply {
            set(name = "verbund", value = verbund)
            set(name = "show", value = deriveToggleValue(sortOrder = sortOrder))
        }
    ).apply {
        path(language.name, areasTerm(language = language))
    }.build().fullPath
to be able to work with parts of the Url.
Does that make sense ?
a

Aleksei Tirman [JB]

02/24/2022, 9:17 AM
Yes, it makes sense to UrlBuilder for your case. I think your code could be simplified to the following:
Copy code
val path = URLBuilder().apply {
    parameters["verbund"] = verbund
    parameters["show"] = deriveToggleValue(sortOrder = sortOrder)
    path(language.name, areasTerm(language = language))
}.build().fullPath
The
url
method is just a helper to build an URL and get it as String.
w

Werner Guttmann

02/24/2022, 9:19 AM
That’s m point sort of; it’s name is slightly mis-leading, as it doesn’t return a Url 😉
Given that it’s probably highly used, this won’t be easy/possible to change
But how about providing another helper method along the lines of buildUrl { .. } that retuns a Url instead of a String ?
a

Aleksei Tirman [JB]

02/24/2022, 12:01 PM
Please file an issue if you want to see this method in Ktor.
w

Werner Guttmann

02/24/2022, 3:30 PM
Done
2 Views