Skip to content

Commit

Permalink
Add param forceSAM=true when targeting sam endpoint (#56)
Browse files Browse the repository at this point in the history
# Description

All sam call need to have the param `forceSAM=true` to ensure call are
retrieved from SAM and not proxied through IL.

# Changes made:

- Add `HostInterceptor` to add param `forceSAM=true` to all calls made
to sam end points (prod, stage, test).
  • Loading branch information
Loic-Dumas authored Nov 27, 2024
1 parent 43466f8 commit 280f2d2
Show file tree
Hide file tree
Showing 2 changed files with 35 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,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"
}
}

0 comments on commit 280f2d2

Please sign in to comment.