REST client

The dev.kilua.rest.RestClient class, located in the kilua-rest module, can be used to connect to any HTTP service. You can use remote services with both dynamic and type-safe calls (using @Serializable classes). The RestClient class has only a single receive() method, which uses the builder pattern for configuration and returns a RestResponse<T> object. A number of extension functions is defined, which allow you to make typical calls easier.

Extension function
Result type

requestDynamic()

RestResponse<JsAny>

request()

RestResponse<T> (T - type-safe value)

callDynamic()

JsAny?

call()

T - type-safe value

Configuration

You can configure HTTP request both when creating a RestClient instance and also when calling individual methods.

val restClient = RestClient {
    headers = {
        listOf(
            "User-Agent" to "KiluaApp"
        )
    }
    baseUrl = "https://api.github.com"
    serializersModule = SerializersModule { // custom serializers for any data types 
        polymorphic(A::class) {
            subclass(B::class)
            subclass(C::class)
        }
    }
}

val searchResult: SearchResult = restClient.call("https://api.github.com/search/repositories", Query("kilua")) {
    method = HttpMethod.Post
    contentType = "application/json"
    headers = {
        listOf(
            "User-Agent" to "KiluaApp"
        )
    }
}

Examples

Check the following examples of different http calls.

Dynamic parameters, dynamic result

Dynamic parameters, type-safe result

Type-safe parameters, dynamic result

Type-safe parameters, type-safe result

RestResponse class

A wrapper RestResponse class is defined as:

When using receive() ,request() or requestDynamic() functions, the returned RestResponse object gives you access to the returned data as well as the native Response object, which gives you access to the server response (e.g. to get HTTP header values and other information):

Last updated