Skip to content

Commit

Permalink
Add param forceSAM=true when targeting sam endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Loic-Dumas committed Nov 7, 2024
1 parent a7ad25d commit cd7f19c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit cd7f19c

Please sign in to comment.