Skip to content

Commit

Permalink
Add custom header to RetroAchievements API requests
Browse files Browse the repository at this point in the history
Fixes API requests returning 403
  • Loading branch information
rafaelvcaetano committed Dec 22, 2023
1 parent 9462f0c commit c1f35d5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
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)
}
}
14 changes: 12 additions & 2 deletions app/src/main/java/me/magnum/melonds/di/RAModule.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package me.magnum.melonds.di

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import me.magnum.melonds.common.network.MelonOkHttpInterceptor
import me.magnum.melonds.common.retroachievements.AndroidRAAchievementSignatureProvider
import me.magnum.melonds.common.retroachievements.AndroidRAUserAuthStore
import me.magnum.rcheevosapi.RAAchievementSignatureProvider
Expand All @@ -19,10 +22,17 @@ import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
object RAModule {

@Provides
fun provideMelonOkHttpInterceptor(@ApplicationContext context: Context): MelonOkHttpInterceptor {
return MelonOkHttpInterceptor(context)
}

@Provides
@Named("ra-api-client")
fun provideRAApiOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder().build()
fun provideRAApiOkHttpClient(melonOkHttpInterceptor: MelonOkHttpInterceptor): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(melonOkHttpInterceptor)
.build()
}

@Provides
Expand Down
2 changes: 1 addition & 1 deletion rcheevos-api/src/main/java/me/magnum/rcheevosapi/RAApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RAApi(
private const val VALUE_HARDMODE_DISABLED = "0"
private const val VALUE_HARDMODE_ENABLED = "1"

private const val REQUEST_LOGIN = "login"
private const val REQUEST_LOGIN = "login2"
private const val REQUEST_HASH_LIBRARY = "hashlibrary"
private const val REQUEST_GAME_DATA = "patch"
private const val REQUEST_USER_UNLOCKED_ACHIEVEMENTS = "unlocks"
Expand Down

0 comments on commit c1f35d5

Please sign in to comment.