Retrofit turns your HTTP API into a Kotlin interface. Create an interface called JournalerBackendService inside the API package. Let's put some code in it:
package com.journaler.api import com.journaler.model.Note import com.journaler.model.Todo import retrofit2.Call import retrofit2.http.* interface JournalerBackendService { @POST("user/authenticate") fun login( @HeaderMap headers: Map<String, String>, @Body payload: UserLoginRequest ): Call<JournalerApiToken> @GET("entity/note") fun getNotes( @HeaderMap headers: Map<String, String> ): Call<List<Note>> @GET("entity/todo") fun getTodos( @HeaderMap headers: Map<String, String> ): Call<List<Todo>> @PUT("entity/note") fun publishNotes( @HeaderMap headers: Map<String, String>, @Body payload: List<Note> ): Call<Unit> @PUT("entity/todo") fun publishTodos( @HeaderMap headers: Map<String, String>, @Body payload: List<Todo> ): Call<Unit> @DELETE("entity/note") fun removeNotes( @HeaderMap headers: Map<String, String>, @Body payload: List<Note> ): Call<Unit> @DELETE("entity/todo") fun removeTodos( @HeaderMap headers: Map<String, String>, @Body payload: List<Todo> ): Call<Unit> }
What do we have in this interface? We defined a list of calls that will be able to execute the following:
- Authentication for the user: This accepts request headers and instances of the UserLoginRequest class containing user credentials. It will be used as a payload for our call. Executing the call will return a wrapped JournalerApiToken instance. We will need a token for all other calls, and we will put its content into the header of each call.
- Notes and TODOs obtain: This accepts request headers containing the authentication token as well. As a result of the call, we get a wrapped list of the Note or Todo class instances.
- Notes and TODOs putting (when we send new stuff to the server): This accepts request headers containing the authentication token as well. Payload for the call will be a list of the Note or Todo class instances. We will not return any important data for these calls. It is important that the response code is positive.
- Notes and TODOs removal--This accepts request headers containing the authentication token as well. Payload for the call will be a list of Note or Todo class instances to be removed from our remote backend server instance. We will not return any important data for these calls. It is important that the response code is positive.
Each has a proper annotation representing the HTTP method with the path. We also use annotations to mark the payload body and headers map.