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

ribesg

04/12/2019, 2:36 PM
I should not be forced to call
.build()
on a
HttpRequestBuilder
to get the
attributes
already set on that builder
e

e5l

04/12/2019, 2:47 PM
Could you provide your use case?
r

ribesg

04/12/2019, 3:11 PM
I have X multiplatform libraries which implement micro-services (MS libs) using another library H which is basically a configured ktor client provider. Among other things, this configured client handles everything jwt tokens (storage on device, auto-refresh with our API on 401 & retry, etc). this library H exposes an extension function
useAuthentication()
on
HttpRequestBuilder
which is used on the endpoint implementations that require authentication in MS libs. This extensions function just adds an attribute to the request, so that the authentication feature interceptor knows if it needs to add the authentication header (among other things).
I use another extension on
HttpRequestBuilder
to check if
useAuthentication
has been called, and it would be nice if it did less work.
Copy code
internal fun HttpRequestBuilder.isUseAuthentication(): Boolean =
    build().attributes.getOrNull(requiresAuth) ?: false
🤔 1
@e5l you actually can’t call
.build()
mutliple times it seems...
Copy code
@Suppress("KDocMissingDocumentation")
class HeadersBuilder(size: Int = 8) : StringValuesBuilder(true, size) {
    override fun build(): Headers {
        require(!built) { "HeadersBuilder can only build a single Headers instance" }
        built = true
        return HeadersImpl(values)
    }
}
Isn’t this incredibly random
Meh, I’m forced to use a toplevel data structure next to my extensions to implement this as I can’t use attributes. Actually I wonder how I could do that.
e

e5l

04/15/2019, 12:13 PM
Probably we’ll make request builder
attributes
available in
1.2.0
could you file an issue?
📝 1
r

ribesg

04/15/2019, 12:13 PM
It already is, the problem is that it’s not available in
HttpRequestBuilder
. I’ll open an issue for that
👍 1
Oh, I can use the fact that
setAttributes
is a function to remove the builder from my toplevel map when it’s called 🧞‍♂️
I’m still exposed to memory leaks with this solution...
23 Views