Page cover image

Crea un cliente de una API

Supongamos que existe un servicio REST ejecutándose en un servidor que provee de los siguientes endpoints:

Endpoints

🔹 1. Obtener todas las tareas

📍 Endpoint: GET /tasks 📥 Request Body:No requiere body 📤 Response (200 OK):

response-body
[
    {
        "id": 1,
        "title": "Comprar víveres",
        "description": "Leche, huevos, pan"
    },
    {
        "id": 2,
        "title": "Llamar a Juan",
        "description": "Confirmar reunión del martes"
    }
]

🔹 2. Obtener una tarea por ID

📍 Endpoint: GET /tasks/{id} 📥 Request Body:No requiere body 📤 Response (200 OK):

response-body
{
    "id": 1,
    "title": "Comprar víveres",
    "description": "Leche, huevos, pan"
}

📤 Response (404 Not Found):

response-body
{
    "error": "Tarea no encontrada"
}

🔹 3. Crear una nueva tarea

📍 Endpoint: POST /tasks 📥 Request Body:

body
{
    "title": "Leer un libro",
    "description": "Capítulo 4 de 'El Principito'"
}

📤 Response (201 Created):

response-body
{
    "id": 3,
    "title": "Leer un libro",
    "description": "Capítulo 4 de 'El Principito'"
}

📤 Response (400 Bad Request) - Si falta algún campo:

response-body
{
    "error": "El campo 'title' es obligatorio"
}

🔹 4. Actualizar una tarea existente

📍 Endpoint: PUT /tasks/{id} 📥 Request Body:

body
{
    "title": "Comprar víveres",
    "description": "Leche, huevos, pan y queso"
}

📤 Response (200 OK):

response-body
{
    "id": 1,
    "title": "Comprar víveres",
    "description": "Leche, huevos, pan y queso"
}

📤 Response (404 Not Found) - Si la tarea no existe:

response-body
{
    "error": "Tarea no encontrada"
}

🔹 5. Eliminar una tarea

📍 Endpoint: DELETE /tasks/{id} 📥 Request Body:No requiere body 📤 Response (204 No Content):No devuelve contenido

📤 Response (404 Not Found) - Si la tarea no existe:

{
    "error": "Tarea no encontrada"
}

DTOs

Debes crear los DTO necesarios para comunicarte con el servicio REST

data.model.CreateTaskDto.kt
package com.example.tasksapp.data.model

import com.example.tasksapp.domain.model.Task

data class CreateTaskDto(
    val title: String,
    val description: String
) {
    companion object {
        fun fromTask(task: Task) =
            CreateTaskDto(
                title = task.title,
                description = task.description
            )
    }

    fun toTask(id: Int) =
        Task(
            id = id,
            title = title,
            description = description
        )
}
data.model.TaskDto
package com.example.tasksapp.data.model

import com.example.tasksapp.domain.model.Task

data class TaskDto(
    val id: Int,
    val title: String,
    val description: String
) {

    fun toTask() =
        Task(
            id = id,
            title = title,
            description = description
        )
}

Cliente

Para acceder a este servicio tendríamos que implementar un cliente como este

package com.example.tasksapp.data.source.remote

import com.example.tasksapp.data.model.CreateTaskDto
import com.example.tasksapp.data.model.TaskDto
import retrofit2.http.*

// Interfaz de API
interface TaskServiceClient {

    @GET("tasks")
    suspend fun getTasks(): List<TaskDto>

    @POST("tasks")
    suspend fun createTask(@Body task: CreateTaskDto)

    @DELETE("tasks/{id}")
    suspend fun removeTask(@Path("id") taskId: Int)
}

Una vez hemos implementado el cliente, hay que instanciarlo en retrofitModule. Es muy importante editar la baseURL poniendo la ip / host donde está corriendo el servicio REST

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create

val retrofitModule = module {
    // API retrofit
    single {
        Retrofit.Builder()
            // Se configura la URL del servicio REST
            .baseUrl("http://10.0.2.2:8080")
            // Se configura la serialización con JSON
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    single<TaskServiceClient> { get<Retrofit>().create(TaskServiceClient::class.java) }
}

Última actualización

¿Te fue útil?