Hey, I am facing an issue where I have multiple u...
# ktor
s
Hey, I am facing an issue where I have multiple url's that I need to switch between depending on the screen so for example -->
First url --> <https://main-a.com>
Second url --> <https://main-b.com>
and I am using Ktor --> I know I can build a new
Copy code
url {
host = <https://main-b.com> 
encodedPath = v1 
protocol = URLProtocol.HTTPS
}
and I have my default request in my
networkmodule.kt
Copy code
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 this
a
Can you please share the code snippet where you create a new UrlBuilder?
s
sure
This is the rebaseUrl -->
Copy code
fun 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 -->
Copy code
suspend inline fun <reified T> HttpClient.getNetworkResponse(
    urlString: String,
    block: HttpRequestBuilder.() -> Unit = {}
): NetworkResponse<T, ErrorApiResponse> =
    getNetworkResponseKtor {
        url(urlString)
        block()
    }
following this one -->
Copy code
suspend inline fun <reified T, reified E> HttpClient.getNetworkResponseKtor(
    block: HttpRequestBuilder.() -> Unit,
): NetworkResponse<T, E> = getNetworkResponseKtor(HttpRequestBuilder().apply(block))
following this one -->
Copy code
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 -->
Copy code
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
Copy code
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 -->
Copy code
client.getNetworkResponse<Any>("/Home") {
rebaseUrl("<https://a.com/>")
}
@Aleksei Tirman [JB] thank you
@Aleksei Tirman [JB] any idea about my issue would be really helpful if you have any idea ?
a
It's difficult to determine the exact issue by looking at the code. Can you please either simplify your code to the minimum required to reproduce the problem or share the complete code snippet (with all dependencies and without unresolved references)?
s
Okay deal I am going to reshare it in simpler way
Here I have my default request where I have my default host and default Headers and params
Copy code
defaultRequest {
 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 --->
Copy code
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)

}
}
@Aleksei Tirman [JB] check this
It's like the defaultRequest that have the default params are being override
a
This seems to be a bug, so I've filed an issue to address it.
K 1
K 1
🚀 1