I'm having an issue with getting data from this API. The API I'm trying to get data from is the Perenual Garden API where every plant has it's own id number. The documentation states to get information about the plant by an ID endpoint like so:
https://perenual.com/api/species/details/[PLANT_ID_NUMBER]?key=[YOUR_SDK_KEY]
But the issue with this is that retrofit get requests require a compile-time constant, and therefore I cannot dynamically change the request of the URL into a constant variable. There are 10,000+ plants in this API and making every individual endpoint would be a terrible idea. Here is my interface where the problem lies:
interface ViewerPlantInterface {
// Needs to be compile time constant?
@GET("species/details/${ApiConstants.SPECIES_ID}")
fun getPlantData(
@Query("key") key: String = ApiConstants.API_KEY_PERENUAL
): Call<ViewerPlant>
}
ApiConstants.SPECIES_ID
is a variable I want to be able to change to the ID of a selected plant by the user and get the data for it. How can I do this while avoiding making an unbearable amount of compile-time constants? Is this even possible?
You can do it using @Path
annotation as follows:
@GET("species/details/{speciesId}")
fun getPlantData(
@Query("key") key: String = ApiConstants.API_KEY_PERENUAL,
@Path("speciesId") speciesId: String,
): Call<ViewerPlant>