-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom header to RetroAchievements API requests
Fixes API requests returning 403
- Loading branch information
1 parent
9462f0c
commit c1f35d5
Showing
3 changed files
with
41 additions
and
3 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/src/main/java/me/magnum/melonds/common/network/MelonOkHttpInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package me.magnum.melonds.common.network | ||
|
||
import android.content.Context | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
class MelonOkHttpInterceptor(private val context: Context) : Interceptor { | ||
private companion object { | ||
const val USER_AGENT = "User-Agent" | ||
const val MELON_USER_AGENT_PREFIX = "melonDS-android" | ||
} | ||
|
||
private val userAgentVersion by lazy { | ||
val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0) | ||
val appVersion = packageInfo.versionName | ||
val userAgentSuffix = appVersion.lowercase().replace(' ', '-').replace("(", "").replace(")", "") | ||
"$MELON_USER_AGENT_PREFIX/$userAgentSuffix" | ||
} | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val newRequest = chain.request() | ||
.newBuilder() | ||
.addHeader(USER_AGENT, userAgentVersion) | ||
.build() | ||
|
||
return chain.proceed(newRequest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters