Hii all,
I am using RetroFit in my application.
My application contains around 5 different Entities like user, observations etc. I have created an interface named 'Entity' which defines the behavior for all the entities in the application.
I intend to write a single Service interface to manage URL calls.
By that, I mean that my URL generation should be dynamic and each entity(user, observations etc) can be specified in the URL request dynamically.
This is what I think will help me achieve retrieving all the Entities(Users, Observations etc) using the method below dynamically by resolving the entity type:
/**
* This method returns the list of all the Entities in resource based on the resourceIdentifier.
*/
@GET("/{resource}")
public Call<List<Entity>> getEntities(@Path("resource") String resource);
The resource is the resource identifier that indicates the type of Entity I want to utilize. Like User, Observations, Activities etc.
What I want to achieve is to dynamically Resolve the type of Entity and also CREATE a New Entity by passing the entity Object as a Param in the method body
/**
* This method is used to create the specified resource.
*
* @return
*/
@POST("/{resource}/new")
Call<Entity> createEntity(@Body Entity entity);
In the above code snippet I want to know that
how shall I pass the type of resource using @Path and along that how to pass the entire object of that type of entity to make a POST request in order to create a new one?
Can I do something like these:
@POST("/{resource}/new")
Call<Entity> createEntity(@Body Entity entity, @Path("resource") String resource);
I am sorry if its too confusing!
Any sort of help or advice is truly appreciated!
Some questions to clarify your issue... Do you struggle on having your path to be dynamically defined on the basis of entity type you are passing?
So that if you pass createEntity(User user) it should produce call on /user/new or createEntity(Observation observation) -> /observation/new ?
Yes. Exactly.
I want to dynamically define the type of entity and also the entire entity object while building the URL.
Everything you posted seems fine. Was there a specific problem you were experiencing? Or are you just asking if this is possible? Because if it's just asking, then yes! That's exactly the kind of thing Retrofit's API is designed for.
"Can I do something like these:"
// This is right way
@POST("/{resource}/new")
Call<Entity> createEntity(@Body Entity entity, @Path("resource") String resource);
@pmk2429 Yes, you can. Though, there is no way to combine annotations like this
// This is wrong way
@POST("/{resource}/new")
Call<Entity> createEntity(@Path("resource") @Body Entity entity);
or to have Retrofit adapt automagically object name to the path of your url.
// This is wrong way
@POST("/{resource}/new")
Call<Entity> createEntity(@Body Entity entity);
So, go and use both annotations. One for posting body, another for dynamically seting path of url.
Ok. Gotcha!
I have tried the above mentioned implementation and it works just fine.
Thank you so much for your help @tomkoptel .
Thank you @JakeWharton for specifying further on the issue!
I truly appreciate it.
Most helpful comment
"Can I do something like these:"
@pmk2429 Yes, you can. Though, there is no way to combine annotations like this
or to have Retrofit adapt automagically object name to the path of your url.
So, go and use both annotations. One for posting body, another for dynamically seting path of url.