From cd7f19c7e128fdf462ddfaf153614e537f9ffe46 Mon Sep 17 00:00:00 2001 From: Loic-Dumas Date: Thu, 7 Nov 2024 17:03:45 +0100 Subject: [PATCH] Add param `forceSAM=true` when targeting sam endpoint --- .../dependencies/modules/OkHttpModule.kt | 2 ++ .../integrationlayer/utils/HostInterceptor.kt | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 dataprovider-retrofit/src/main/java/ch/srg/dataProvider/integrationlayer/utils/HostInterceptor.kt diff --git a/dataprovider-retrofit/src/main/java/ch/srg/dataProvider/integrationlayer/dependencies/modules/OkHttpModule.kt b/dataprovider-retrofit/src/main/java/ch/srg/dataProvider/integrationlayer/dependencies/modules/OkHttpModule.kt index 7018468..a51aca4 100644 --- a/dataprovider-retrofit/src/main/java/ch/srg/dataProvider/integrationlayer/dependencies/modules/OkHttpModule.kt +++ b/dataprovider-retrofit/src/main/java/ch/srg/dataProvider/integrationlayer/dependencies/modules/OkHttpModule.kt @@ -2,6 +2,7 @@ package ch.srg.dataProvider.integrationlayer.dependencies.modules import android.content.Context import android.content.res.Configuration +import ch.srg.dataProvider.integrationlayer.utils.HostInterceptor import ch.srg.dataProvider.integrationlayer.utils.UserAgentInterceptor import ch.srg.dataProvider.integrationlayer.utils.UserAgentUtils import ch.srg.dataProvider.integrationlayer.utils.VectorInterceptor @@ -31,6 +32,7 @@ object OkHttpModule { logging.setLevel(HttpLoggingInterceptor.Level.HEADERS) builder.addInterceptor(logging) builder.addInterceptor(UserAgentInterceptor(UserAgentUtils.createUserAgent(context))) + builder.addInterceptor(HostInterceptor()) builder.addInterceptor(VectorInterceptor(vector)) builder.readTimeout(READ_TIMEOUT_SECONDS, TimeUnit.SECONDS) builder.connectTimeout(CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS) diff --git a/dataprovider-retrofit/src/main/java/ch/srg/dataProvider/integrationlayer/utils/HostInterceptor.kt b/dataprovider-retrofit/src/main/java/ch/srg/dataProvider/integrationlayer/utils/HostInterceptor.kt new file mode 100644 index 0000000..f8bdf09 --- /dev/null +++ b/dataprovider-retrofit/src/main/java/ch/srg/dataProvider/integrationlayer/utils/HostInterceptor.kt @@ -0,0 +1,29 @@ +package ch.srg.dataProvider.integrationlayer.utils + +import okhttp3.Interceptor +import okhttp3.Response + +/** + * Adds a query parameter "forceSAM=true" to each SAM HTTP request. + * See [IlHost] for more details. + */ +class HostInterceptor : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val originalRequest = chain.request() + val originalHttpUrl = originalRequest.url + + if (originalHttpUrl.queryParameter("forceSAM") != null || !originalHttpUrl.pathSegments.contains("sam")) { + return chain.proceed(originalRequest) + } + + val url = originalHttpUrl.newBuilder() + .addQueryParameter("forceSAM", "true") + .build() + + // Request customization: add request headers + val requestBuilder = originalRequest.newBuilder().url(url) + val request = requestBuilder.build() + return chain.proceed(request) + } +}