Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add param forceSAM=true when targeting sam endpoint #56

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,33 @@
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(FORCE_SAM) != null || !originalHttpUrl.pathSegments.contains("sam")) {
return chain.proceed(originalRequest)
}

val url = originalHttpUrl.newBuilder()
.addQueryParameter(FORCE_SAM, "true")
.build()

// Request customization: add request headers
val requestBuilder = originalRequest.newBuilder().url(url)
val request = requestBuilder.build()
return chain.proceed(request)
}

companion object {
private const val FORCE_SAM = "forceSAM"
}
}
Loading