Skip to content

Commit 719138c

Browse files
Modify http requests headers before retrying to offset time by some value (#56)
1 parent d7ca0a7 commit 719138c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

core/network/src/main/kotlin/com/mr3y/podcaster/core/network/di/NetworkModule.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object NetworkModule {
5656
val requestAuthenticationPlugin = createClientPlugin("requestAuthenticatorPlugin") {
5757
onRequest { request, _ ->
5858
request.headers {
59-
val epoch = (System.currentTimeMillis() / 1000).let { if (BuildConfig.DEBUG) it - 150 else it }
59+
val epoch = (System.currentTimeMillis() / 1000)
6060
append("User-Agent", "Podcaster/1.0")
6161
append("X-Auth-Date", epoch.toString())
6262
append("X-Auth-Key", BuildConfig.API_KEY)
@@ -72,10 +72,19 @@ object NetworkModule {
7272
}
7373
}
7474
install(HttpRequestRetry) {
75+
modifyRequest { request ->
76+
// The server expects the auth date to be within a 3 minutes time window around the server time
77+
// but sometimes the request time/auth date is off by a few seconds/milliseconds, therefore,
78+
// to solve this, retry the request with a 1.5 minutes offset.
79+
val epoch = (System.currentTimeMillis() / 1000) - 150
80+
request.headers["X-Auth-Date"] = epoch.toString()
81+
request.headers["Authorization"] = authHeader(epoch)
82+
}
7583
retryIf(3) { _, httpResponse ->
7684
when {
7785
httpResponse.status.value in 500..599 -> true
7886
httpResponse.status == HttpStatusCode.TooManyRequests -> true
87+
httpResponse.status == HttpStatusCode.Unauthorized -> true
7988
else -> false
8089
}
8190
}

0 commit comments

Comments
 (0)