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

tylerwilson

05/28/2019, 11:43 PM
I am porting some Retrofit code to ktor. I need to add additional params (a signature calculation) to an already built URLBuilder. But I get the following:
Copy code
java.lang.IllegalStateException: Cannot modify a builder when final structure has already been built
. Is there a way to do this? Any examples out there I may refer to? Thank you.
s

Shawn

05/29/2019, 2:48 AM
So, I searched for that error and found my way to the
URLBuilder
source, and it seems like your issue is really with the ParametersBuilder that extends StringValuesBuilder (https://github.com/ktorio/ktor/blob/master/ktor-utils/common/src/io/ktor/util/StringValues.kt#L237) rather than the URLBuilder. You very well might need to extract the parameters from the ParametersBuilder using maybe
toMap()
or
flattenEntries()
or something, create a new collection with the params you need to add, then create a new, unbuilt ParametersBuilder using the pairs. Then perhaps pass that ParametersBuilder to a new URLBuilder instance with the appropriate values
might be best to wrap it in an extension function…
the API is not really designed to do this easily, at least, in a type-safe manner anyhow
e

e5l

05/29/2019, 6:20 AM
Hi @tylerwilson, can you add it before the build?
t

tylerwilson

05/29/2019, 2:01 PM
Yeah, thinking about it some more, I think the way to go is the generate sig and create a new URL builder with the updated params. The signature calc needs the host, path and all params to use for calculation (it is based on some Amazon signature procedure). Adding before may be possible, but having an extension on URLBuilder was/is a convenient way to do it. Either way, looks like some copy/calc/apply code is needed. Thank you both.
2 Views