Olivier Patry
01/16/2024, 10:37 AMHttpClient
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?Olivier Patry
01/16/2024, 10:38 AMdefaultRequest
initial impl
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}"
}
}
}
Aleksei Tirman [JB]
01/17/2024, 11:57 AMDefaultRequest
plugin by creating a new client using the HttpClient.config
method. This solution requires the replacement of the HttpClient
instance in the DI container.Olivier Patry
01/17/2024, 1:04 PMHttpClient
instance was the only solution I could think of but hardly usable in my current setup 😞Olivier Patry
01/17/2024, 1:06 PMDefaultRequestDelegate
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?Aleksei Tirman [JB]
01/17/2024, 4:04 PMOlivier Patry
01/17/2024, 5:14 PMloadTokens
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?Aleksei Tirman [JB]
01/18/2024, 8:36 AM