Said Shatila
08/14/2025, 2:23 PMFirst url --> <https://main-a.com>
Second url --> <https://main-b.com>
and I am using Ktor --> I know I can build a new
url {
host = <https://main-b.com>
encodedPath = v1
protocol = URLProtocol.HTTPS
}
and I have my default request in my networkmodule.kt
defaultRequest {
param.append("","")
param.append("","")
......
}
So I have a bunch of default parameters so let's say I am doing a normal call just adding the endpoint nothing will change but if I created a new urlBuilder all of the params are being override.
What could be the solution for thisAleksei Tirman [JB]
08/18/2025, 9:49 AMSaid Shatila
08/18/2025, 1:54 PMSaid Shatila
08/18/2025, 1:57 PMfun HttpRequestBuilder.rebaseUrl(
baseUrl: String,
) {
val endPoint = url.encodedPath // capture whatever was set by getNetworkResponse()
url {
takeFrom(baseUrl)
encodedPath = "${encodedPath.trimEnd('/')}/${endPoint.trimStart('/')}"
applyStandardQueryParams()
}
}
This function here I use it so I can make a GET network call -->
suspend inline fun <reified T> HttpClient.getNetworkResponse(
urlString: String,
block: HttpRequestBuilder.() -> Unit = {}
): NetworkResponse<T, ErrorApiResponse> =
getNetworkResponseKtor {
url(urlString)
block()
}
following this one -->
suspend inline fun <reified T, reified E> HttpClient.getNetworkResponseKtor(
block: HttpRequestBuilder.() -> Unit,
): NetworkResponse<T, E> = getNetworkResponseKtor(HttpRequestBuilder().apply(block))
following this one -->
suspend inline fun <reified T, reified E> HttpClient.getNetworkResponseKtor(
builder: HttpRequestBuilder,
): NetworkResponse<T, E> {
builder.method = HttpMethod.Get
return networkResponseOf { request(builder) }
}
following the result -->
suspend inline fun <reified T, reified E> networkResponseOf(
crossinline httpCallFunction: suspend () -> HttpResponse,
): NetworkResponse<T, E> {
return try {
val response = httpCallFunction()
if (response.status.value in NetworkResponse.successCodeRange) {
return NetworkResponse.Success(data = response.body() ?: Unit as T)
}
NetworkResponse.Failure(response.body() as E, null)
} catch (ex: Exception) {
Log.d("ERROR | Network",
(ex.message ?: ex.getThrowableMsg()) as String
)
NetworkResponse.Failure(null as? E, ex.getThrowableMsg())
}
}
and this is the default request in the KtorEngine
private fun HttpClientConfig<*>.setDefaultRequest(
baseUrl: String,
venueDebug: Boolean,
) {
defaultRequest {
url(baseUrl)
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
applyDefaultHeaders(
venueDebug = venueDebug
)
url {
applyStandardQueryParams()
}
}
}
example -->
client.getNetworkResponse<Any>("/Home") {
rebaseUrl("<https://a.com/>")
}
Said Shatila
08/18/2025, 1:57 PMSaid Shatila
08/19/2025, 3:10 PMAleksei Tirman [JB]
08/19/2025, 3:22 PMSaid Shatila
08/19/2025, 3:23 PMSaid Shatila
08/19/2025, 3:27 PMdefaultRequest {
url(baseUrl) // <https://a.com/>
headers.append("x","y")
url {
parameters.set(APP_VERSION, networkClientConfig.appVersion)
}
}
Jumping directly I want to make an API call where I want to change my host --->
client.get("Home") {
url {
takeFrom(newBaseUrl)
// if I don't append my default parameter // here they won't be included in the default // request so I have to append them again.
parameters.set(APP_VERSION, networkClientConfig.appVersion)
}
}
Said Shatila
08/19/2025, 3:27 PMSaid Shatila
08/19/2025, 3:31 PMAleksei Tirman [JB]
08/19/2025, 7:52 PM