diff --git a/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/AbstractDeviceInfoUpdateWorker.kt b/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/AbstractDeviceInfoUpdateWorker.kt index 72c7ed0ec..ac0968cd9 100644 --- a/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/AbstractDeviceInfoUpdateWorker.kt +++ b/CrossAppLogin/Back/src/main/kotlin/com/infomaniak/core/crossapplogin/back/internal/deviceinfo/AbstractDeviceInfoUpdateWorker.kt @@ -32,9 +32,9 @@ import androidx.work.OneTimeWorkRequestBuilder import androidx.work.WorkManager import androidx.work.WorkerParameters import androidx.work.await -import com.infomaniak.core.common.DynamicLazyMap import com.infomaniak.core.auth.api.ApiRoutesCore import com.infomaniak.core.auth.room.UserDatabase +import com.infomaniak.core.common.DynamicLazyMap import com.infomaniak.core.common.autoCancelScope import com.infomaniak.core.common.cancellable import com.infomaniak.core.crossapplogin.back.CrossAppLogin @@ -44,8 +44,6 @@ import createHttpClient import io.ktor.client.HttpClient import io.ktor.client.request.post import io.ktor.client.request.setBody -import io.ktor.http.ContentType -import io.ktor.http.contentType import io.ktor.http.isSuccess import kotlinx.coroutines.Deferred import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -138,7 +136,6 @@ abstract class AbstractDeviceInfoUpdateWorker( val url = ApiRoutesCore.sendDeviceInfo() val response = httpClient.post(url) { - contentType(ContentType.Application.Json) setBody(deviceInfo) } if (response.status.isSuccess()) { diff --git a/Network/Ktor/src/main/kotlin/CreateHttpClient.kt b/Network/Ktor/src/main/kotlin/CreateHttpClient.kt index d189a491e..d964c8567 100644 --- a/Network/Ktor/src/main/kotlin/CreateHttpClient.kt +++ b/Network/Ktor/src/main/kotlin/CreateHttpClient.kt @@ -30,7 +30,6 @@ import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.request import io.ktor.http.ContentType import io.ktor.http.HttpHeaders -import io.ktor.http.headers import io.ktor.serialization.kotlinx.json.json import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json @@ -53,7 +52,7 @@ fun createHttpClient( retryOnExceptionIf { _, cause -> cause !is SerializationException } } defaultRequest { - headers { applyDefaultHeaders() } + applyDefaultHeaders() } HttpResponseValidator { validateResponse { response -> diff --git a/Network/src/main/kotlin/com/infomaniak/core/network/networking/HttpUtils.kt b/Network/src/main/kotlin/com/infomaniak/core/network/networking/HttpUtils.kt index c6b993afc..ed7cb7f54 100644 --- a/Network/src/main/kotlin/com/infomaniak/core/network/networking/HttpUtils.kt +++ b/Network/src/main/kotlin/com/infomaniak/core/network/networking/HttpUtils.kt @@ -20,8 +20,15 @@ package com.infomaniak.core.network.networking import android.os.Build import com.infomaniak.core.network.NetworkConfiguration import com.infomaniak.core.network.utils.Utils.getPreferredLocaleList +import io.ktor.client.request.accept +import io.ktor.client.request.headers +import io.ktor.client.utils.CacheControl import io.ktor.http.ContentType import io.ktor.http.HeadersBuilder +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpMessageBuilder +import io.ktor.http.contentType +import io.ktor.http.header.AcceptEncoding import io.ktor.util.appendIfNameAbsent import okhttp3.Headers import okhttp3.Headers.Companion.toHeaders @@ -57,21 +64,31 @@ object HttpUtils { } } - fun HeadersBuilder.applyDefaultHeaders(contentType: ContentType? = ContentType.Application.Json) { - headerMap(contentType?.toString()).forEach { + fun HttpMessageBuilder.applyDefaultHeaders(contentType: ContentType? = ContentType.Application.Json) { + contentType?.let { + contentType(contentType) + accept(contentType) + } + headers { + applyDefaultHeaders() + } + } + + private fun HeadersBuilder.applyDefaultHeaders() { + headerMap(contentType = null).forEach { appendIfNameAbsent(it.key, it.value) } } private fun headerMap(contentType: String?): Map = with(NetworkConfiguration) { buildMap { - put("Accept-Language", getAcceptedLanguageHeaderValue()) - put("Accept-Encoding", "gzip") - put("User-Agent", getUserAgent) - if (HttpClientConfig.cacheDir == null) put("Cache-Control", "no-cache") + put(HttpHeaders.AcceptLanguage, getAcceptedLanguageHeaderValue()) + put(HttpHeaders.AcceptEncoding, AcceptEncoding.Gzip.toString()) + put(HttpHeaders.UserAgent, getUserAgent) + if (HttpClientConfig.cacheDir == null) put(HttpHeaders.CacheControl, CacheControl.NO_CACHE) contentType?.let { - put("Accept-type", contentType) - put("Content-type", contentType) + put(HttpHeaders.Accept, contentType) + put(HttpHeaders.ContentType, contentType) } customHeaders?.forEach { customHeader -> computeIfAbsent(customHeader.key) { diff --git a/Notifications/Registration/src/main/kotlin/AbstractNotificationsRegistrationWorker.kt b/Notifications/Registration/src/main/kotlin/AbstractNotificationsRegistrationWorker.kt index 27ee01d72..4f03a4072 100644 --- a/Notifications/Registration/src/main/kotlin/AbstractNotificationsRegistrationWorker.kt +++ b/Notifications/Registration/src/main/kotlin/AbstractNotificationsRegistrationWorker.kt @@ -36,8 +36,6 @@ import createHttpClient import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.client.statement.HttpResponse -import io.ktor.http.ContentType -import io.ktor.http.contentType import io.ktor.http.isSuccess import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll @@ -96,7 +94,6 @@ abstract class AbstractNotificationsRegistrationWorker( val httpClient = createHttpClient(okHttpClient) return runCatching { val response = httpClient.post("$INFOMANIAK_API_V1/devices/register") { - contentType(ContentType.Application.Json) setBody(registrationInfo) } if (response.status.isSuccess()) { diff --git a/TwoFactorAuth/Back/src/main/kotlin/TwoFactorAuthImpl.kt b/TwoFactorAuth/Back/src/main/kotlin/TwoFactorAuthImpl.kt index c86f200ac..5437b96e9 100644 --- a/TwoFactorAuth/Back/src/main/kotlin/TwoFactorAuthImpl.kt +++ b/TwoFactorAuth/Back/src/main/kotlin/TwoFactorAuthImpl.kt @@ -31,7 +31,6 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.client.plugins.defaultRequest import io.ktor.client.request.delete import io.ktor.client.request.get -import io.ktor.client.request.headers import io.ktor.client.request.patch import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.bodyAsText @@ -76,8 +75,8 @@ internal class TwoFactorAuthImpl( retryOnExceptionIf { _, cause -> cause !is SerializationException } } defaultRequest { + applyDefaultHeaders() url("$LOGIN_ENDPOINT_URL/api/2fa/push/") - headers { applyDefaultHeaders() } } }