Skip to content

Requester

This API allows to send API requests with your clients providing the basic methods to build your own customized requester following the Equinox's philosophy. It is based on top of Ktor to cover all the available platforms

Implementation

Extending the Requester can be created a tailored instance to use following your requirements

class YourRequester(
    host: String,
    userId: String? = null,
    userToken: String? = null,
    connectionErrorMessage: String,
    enableCertificatesValidation: Boolean = false
): Requester( // extends the Requester to inherit the base methods
    host = host,
    userId = userId,
    userToken = userToken,
    connectionErrorMessage = connectionErrorMessage,
    enableCertificatesValidation = enableCertificatesValidation
) {

    // add the posssibility to send a custom GET request
    suspend fun sendYourRequest(): JsonObject {
        return execGet(
            endpoint = "yourEndpoint"
        )
    }

}

Usage

Initialize Requester instance

val requester = YourRequester(
    host = "host",
    userId = "userId", // null when for example user not logged in
    userToken = "userToken", // null when for example user not logged in
    connectionErrorMessage = "connectionErrorMessage",
    enableCertificatesValidation = true / false
)

Send requests

To improve performance and follow best practices, the core request methods are marked as suspend by default, leaving their execution to be handled with coroutines such viewModelScope or similar

Normal request

Send a normal request and handle its response as follows:

requester.sendRequest(
    request = { sendYourRequest() },
    onSuccess = {
        // handle a successful request
    },
    onFailure = {
        // handle a failed request
    },
    onConnectionError = {
        // handle a connection error
    }
)

Paginated request

Leveraging the PaginatedResponse API the Requester allows to send a paginated request and format the response according to the PaginatedResponse layout providing, when the request successful, an instance with the paginated data. This requires using the kotlinx library to correctly work. The paginated request can be sent as follows:

requester.sendPaginatedRequest(
    request = { sendYourPaginatedRequest() },
    serialiazer = Home.serializer(), // required
    onSuccess = { page ->
        // use the serialized page from the response
        println(page.data) // list of paged data
    },
    onFailure = {
        // handle a failed request
    },
    onConnectionError = {
        // handle a connection error
    }
)