Morten Minke
01/04/2025, 5:36 PMyschimke
01/04/2025, 5:49 PMGiorgi
01/04/2025, 5:49 PM@Singleton
class CCHttpClient(
@Named(IO) private val ioDispatcher: CoroutineDispatcher
) {
private var client: HttpClient = defaultClient()
fun setBearerToken(token: String) {
client = client.config {
defaultRequest {
headers {
if (contains(HttpHeaders.Authorization)) {
remove(HttpHeaders.Authorization)
}
append(HttpHeaders.Authorization, "Bearer $token")
}
}
}
}
fun removeBearerTokens() {
client = client.config {
defaultRequest {
headers {
if (contains(HttpHeaders.Authorization)) {
remove(HttpHeaders.Authorization)
}
}
}
}
}
suspend fun post(
url: String,
body: Any,
useDefaultErrorHandler: Boolean = true
): HttpResponse = withContext(ioDispatcher) {
<http://client.post|client.post>(url) {
setBody(body)
}.checkSuccessIf(useDefaultErrorHandler)
}
}
Maybe you could do something similar? so instead of creating httpClient directly, create some manager class. when user enters the url then call .setUrl function on that class and make all requests go thru that class.streetsofboston
01/04/2025, 9:16 PM(URI) -> HttpClient
instead of plain HttpClient
Morten Minke
01/05/2025, 11:39 AM