I am porting some Retrofit code to ktor. I need to...
# ktor
t
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
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
Hi @tylerwilson, can you add it before the build?
t
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.