I'd like to know the best way to put my APIKEY in all my REST requests withtout having to add it in parameters of the request.
For now I just have a couple of calls, but I'm trying to see further.
@GET(".")
fun getSearch(@Query("s") text: String, @Query("apikey") APIKEY: String) : Observable<ResponseSearch>
I was wondering if there was a way not to have the APIKEY in variables of every call
You can, but the better solution is to use Okhttp Interceptors
Here's an example:
class TokenInterceptor(private val preferencesStorage: SharedPreferencesStorage) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var original = chain.request()
val token = preferencesStorage.getAccessToken()
val url = original.url().newBuilder().addQueryParameter("apikey", token).build()
original = original.newBuilder().url(url).build()
return chain.proceed(original)
}
}
You should also add TokenInterceptor to your Okhttp client builder
val client = OkHttpClient.Builder()
.addInterceptor(TokenInterceptor(SharedPreferencesStorage()))
.build()