Reputation: 51824
Given the following Retrofit interface:
@GET("offices")
fun getOffices(@Query("uid") uid: String,
@Query("lat") latitude: Double,
@Query("lon") longitude: Double
): Call<List<Office>>
How can I replace the location parameters with a more user friendly GeoLocation
type ...
data class GeoLocation(
val latitude: Double,
val longitude: Double
)
... which is automatically converted to lat
and lon
at request time such as:
@GET("offices")
fun getOffices(@Query("uid") uid: String,
@Query("location") location: GeoLocation
): Call<List<Office>>
Here is the Retrofit setup:
fun createRetrofit(baseUrl: String,
okHttpClient: OkHttpClient): Retrofit {
val moshi = Moshi.Builder()
.build()
return Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.client(okHttpClient)
.build()
}
Upvotes: 11
Views: 17123
Reputation: 7478
You can use Query in suspend function as a parameter like that:
@GET("Posts")
suspend fun getPosts(@Query("MaxCount") maxCount: Int): Response<Posts>
Upvotes: -2
Reputation: 952
If userfriendly access is your concern, you could just create a wrapper function. This way you don't have to mess with your Retrofit configuration at all
fun getOffices(uid: String, location: GeoLocation): Call<List<Office>> {
return getOfficesIf(uid, location.lat, location.lon)
}
@GET("offices")
fun getOfficesIf(@Query("uid") uid: String,
@Query("lat") latitude: Double,
@Query("lon") longitude: Double
): Call<List<Office>>
Upvotes: 4