mini-rest-client

A Kotlin Mimimal REST client

License

License

Categories

Categories

CLI User Interface
GroupId

GroupId

com.github.barakb
ArtifactId

ArtifactId

mini-rest-client
Last Version

Last Version

1.0.5
Release Date

Release Date

Type

Type

jar
Description

Description

mini-rest-client
A Kotlin Mimimal REST client
Project URL

Project URL

https://github.com/barakb/mini-rest-client
Source Code Management

Source Code Management

https://github.com/barakb/mini-rest-client

Download mini-rest-client

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.barakb/mini-rest-client/ -->
<dependency>
    <groupId>com.github.barakb</groupId>
    <artifactId>mini-rest-client</artifactId>
    <version>1.0.5</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.barakb/mini-rest-client/
implementation 'com.github.barakb:mini-rest-client:1.0.5'
// https://jarcasting.com/artifacts/com.github.barakb/mini-rest-client/
implementation ("com.github.barakb:mini-rest-client:1.0.5")
'com.github.barakb:mini-rest-client:jar:1.0.5'
<dependency org="com.github.barakb" name="mini-rest-client" rev="1.0.5">
  <artifact name="mini-rest-client" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.barakb', module='mini-rest-client', version='1.0.5')
)
libraryDependencies += "com.github.barakb" % "mini-rest-client" % "1.0.5"
[com.github.barakb/mini-rest-client "1.0.5"]

Dependencies

runtime (7)

Group / Artifact Type Version
org.jetbrains.kotlinx : kotlinx-coroutines-core-jvm jar 1.4.1
org.apache.httpcomponents.client5 : httpclient5 jar 5.0.1
com.google.code.gson : gson jar 2.8.6
org.slf4j : slf4j-api jar 1.7.30
io.github.microutils : kotlin-logging jar 1.8.3
org.jetbrains.kotlin : kotlin-reflect jar 1.4.20
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.20

Project Modules

There are no modules declared in this project.

Build Status Download Maven Central GitHub License

A Kotlin Minimal REST client

I had 3 goals in mind when starting this work.

  1. Fully async code no blocking threads.
  2. Easy to compose both sequentially and concurrently
  3. Minimum dependencies.
  4. Small
  • Choosing the underlining http client to be Apache HttpAsyncClient satisfy the first and third requirements.
  • Extending CloseableHttpAsyncClient.execute as a suspend function (in the file CloseableHttpAsyncClientExt.kt) enable easy composition of the result client sequentially and concurrently, hence satisfy the second requirement.

To consume this project using maven add the following to your pom.xml

<dependency>
     <groupId>com.github.barakb</groupId>
     <artifactId>mini-rest-client</artifactId>
     <version>1.0.5</version>
</dependency>

Or gradle

implementation("com.github.barakb:mini-rest-client:1.0.5")
Usage:

To create a Nomad client Kotlin DSL can be used.

fun main(): Unit = runBlocking {
    HttpClient {
        defaultRequest {
            url = "http://httpbin.org/"
            header("name", "value")
            param("v", "f")
        }
    }.use { client ->
        val headers = client.get<JsonObject> {
            path = "headers"
        }
        println("headers: $headers")
    }
}

Inside the client DLS you have full access to the underline HttpAsyncClientBuilder, so it is easy to configure

client {
}

The client uses the return type to extract the result form the http response.

News:

With version 1.0.5 it is possible to set the connectTimeout and the responseTimeout per request

@ExperimentalTime
fun main(): Unit = runBlocking {
    HttpClient {
        gson { setPrettyPrinting() }
        defaultRequest {
            contentType = ContentType.APPLICATION_JSON
            url = "http://httpbin.org/"
            header("name", "value")
            param("v", "f")
        }
    }.use { client ->
        val headers = client.get<Headers> {
            path = "headers"
            connectTimeout = 15.seconds
            responseTimeout = 30.seconds
        }
        println("headers: $headers")
    }
}

data class Headers(@HttpHeader("host") var host: String?, val headers: JsonObject)

Versions

Version
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0