You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For now, I work around this issue by manually building the host URL string, like this:
funcreateConnectClient(): ProtocolClientInterface {
returnProtocolClient(
httpClient =ConnectOkHttpClient(),
ProtocolClientConfig(
// We cannot use `server.toString()` because it adds a trailing slash
host ="${server.scheme}://${server.host}:${server.port}",
serializationStrategy =GoogleJavaLiteProtobufStrategy(),
networkProtocol =NetworkProtocol.CONNECT,
interceptors =listOf(
{ AuthenticationInterceptor(User.bearerToken) },
{ LoggingInterceptor() },
)
),
)
}
However, I do not think this is a good long-term solution, for three reasons:
It is confusing for future developers who read the code and need to understand why we did this (requiring extra comments to explain).
In general, it would be nice to have some kind of validation that a proper URL has been provided.
The way that ProtocolClient builds a URL by concatenating strings (url = URL("${config.host}/${methodSpec.path}")) seems fragile.
I suggest that the host be stored as a URL object instead of as a String. Doing that fixes all three of my issues listed above, as follows:
Constructing the URL object from a String doesn't care if there's a trailing slash or not (or, in my case, using the toUrl() method on the HttpUrl object).
If an invalid URL is provided, the exception will be thrown immediately on construction instead of waiting until execution to fail.
You can build new URLs without string concatenation using the two-argument URL constructor that takes a base URL as "context": url = URL(config.host, methodSpec.path)
Regarding the last point about building the URL with a context, note that all five of the following invocations produce identical output:
I noticed that my Android app was using URLs with a double slash after the hostname, like this:
I realized that this is happening because I store my server base URL as an
okhttp3.HttpUrl
object, and then create theProtocolClientConfig
like this:For now, I work around this issue by manually building the host URL string, like this:
However, I do not think this is a good long-term solution, for three reasons:
ProtocolClient
builds a URL by concatenating strings (url = URL("${config.host}/${methodSpec.path}")
) seems fragile.I suggest that the
host
be stored as aURL
object instead of as aString
. Doing that fixes all three of my issues listed above, as follows:toUrl()
method on theHttpUrl
object).url = URL(config.host, methodSpec.path)
Regarding the last point about building the URL with a context, note that all five of the following invocations produce identical output:
The text was updated successfully, but these errors were encountered: