Hi, How would you recommend to handle the followin...
# ktor
o
Hi, How would you recommend to handle the following need: I have an application which configures an
HttpClient
to reach a REST API. This client is consumed through a business logic layer injected as a singleton in my DI graph (at app start). I currently inject the
HttpClient
in this business logic ctor class. I might need intermediate setup to fully configure this
HttpClient
, typically, I'll get the root URL of the REST API after being authorized/authenticated. My business logic layer relies on "relative" endpoints,
rest/api/foo/bar
and I don't want to expose the root URL in this layer, which may vary depending on the DI setup (prod/preprod, specific setup…). Until now, I didn't have the constraint of knowing the root URL afterwards, it was known at app start which is not the case anymore. So, I'd like to update on the fly the
defaultRequest
block of the client ideally, is it the way to go, how?
Here is my
defaultRequest
initial impl
Copy code
defaultRequest {
    if (url.host.isEmpty()) {
        val defaultUrl = URLBuilder().takeFrom(MY_DEFAULT_URL)
        url.host = defaultUrl.host
        url.protocol = defaultUrl.protocol
        if (!url.encodedPath.startsWith('/')) {
            // prepend base path from configuration
            val basePath = defaultUrl.encodedPath
            url.encodedPath = "$basePath/${url.encodedPath}"
        }
    }
}
a
You can try reconfiguring the
DefaultRequest
plugin by creating a new client using the
HttpClient.config
method. This solution requires the replacement of the
HttpClient
instance in the DI container.
o
Yes, replacing the
HttpClient
instance was the only solution I could think of but hardly usable in my current setup 😞
In the meantime, I thought about injecting a
DefaultRequestDelegate
or something like that which would delegate the lookup of the
defaultRequest
to another configurable entity, don't know if it's viable. I could just expose this directly as a
DefaultRequest.DefaultRequestBuilder
being injected in the
HttpClient
used. Sounds feasible/reasonable?
a
I can't find a way of doing that. Do you have an idea?
o
Like what is suggested to inject the
loadTokens
refreshTokens
here https://ktor.io/docs/bearer-client.html#step5 the
bearerTokenStorage
is provided from outside and dynamic, I imagine something close to that but to provide the default request. This
defaultRequest
lambda is called at each new request, right?
a
That's right.