I have an apikey "token" that has to be provided as a query parameter in every request. How can i setup retrofit as to always include this query parameter in the request?
I am currently providing the token as a default parameter:
interface CompanyService {
@GET("accounts/{id}")
fun getCompany(
@Path("id") id: Number,
@Query("token") token: String = Constants.TOKEN) <---- here
: Call<CompanyResponse>
@GET("accounts/")
fun getCompanies(
@Query("id") page: String,
@Query("limit") limit: Int,
@Query("sort") sort: String = "id",
@Query("token") token: String = Constants.TOKEN) <---- here
: Call<CompanyListResponse>
}
But since i know that every request requires the same token, it feels redundant to have to provide it as a default parameter in every request function i create. How can i setup retrofit to decorate every outgoing request with a default query parameter?
This is how i build the retrofit2 instance:
class CompanyAPI {
companion object {
private var retrofit: Retrofit? = null
val client:Retrofit get() {
if (retrofit == null) {
retrofit=Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Constants.BASE_URL)
.build()
}
return retrofit!!
}
}
}
I would think that you would add some additional function call in the call chain before calling .build() but looking at the available functions i can't say i see anything that would provide this functionality.