http4k Connect is a lightweight API Client toolkit which includes libraries for connecting to popular third-party cloud services and AI backends
http4k Connect integrates with many popular Cloud and AI HTTP APIs.
In true http4k-style, Connect clients rely on minimal dependencies and zero reflection, perfect for lightweight and security-conscious applications.
Extension of the clients is as simple as combining function calls or implementing a single data class with just 2 simple methods.
Every API Client leverages Result types and is exception-safe, so you can finally code like it’s Kotlin that you’re writing!
Connect API Clients have been used to build high volume applications in Banking, Publishing, eCommerce & Government projects.
Most API Clients come with an in-memory cloud-service fake which can be used for lightning fast test suites or spun up as a server.
All fakes are chaos-enabled, so you can test what happens to your code in failure scenarios.
Most API Client libraries hide their HTTP client from you, stopping you from implementing observability or customisation.
Connect allows any http4k client module to be plugged in, so you can leverage the full power of http4k
API Clients leverage TinyTypes to provide typesafe coding and automatic serialisation.
Stop relying on unsafe, stringly-typed APIs.
Ships with a storage abstraction for simple key-value storage, and adapters for popular backends.
Plugin in-memory, S3, JDBC or Redis with a single line of code!
Create the API client and call it. Inject your own HTTP client for observability.
val s3 = S3.Http(http = JavaHttpClient())
val buckets: Result<BucketList, RemoteFailure> = s3.listBuckets()
val bucketNames: List<BucketName> = buckets.map { it.items }.orThrow()Is your API action not supported? No problem - write your own as an extension.
data class InvertBucket(val bucketName: BucketName) : S3Action<BucketName> {
override fun toRequest() = Request(POST, Uri.of("/$bucketName/invert"))
override fun toResult(response: Response) =
if (response.status.successful)
Success(BucketName.of(response.bodyString()))
else Failure(asRemoteFailure(response))
}
fun S3.invertBucket(bucketName: BucketName) = invoke(InvertBucket(bucketName))An API Action is an interface with functions that marshal the contents of HTTP messages.
interface APIAction<R> {
fun toRequest(): Request
fun toResult(response: Response): Result<R, RemoteFailure>
}An API Client is a class with a single function. It handles the transport for the remote API.
class API(rawHttp: HttpHandler) {
private val transport = SetBaseUriFrom(Uri.of("https://api.com"))
.then(rawHttp)
operator fun <R> invoke(action: APIAction<R>): Result<R, RemoteFailure> =
action.toResult(transport(action.toRequest()))
}Implement each action with a single class and an extension function. Compose calls together without bloating your API Client.
data class Reverse(val value: String) : APIAction<String> {
override fun toRequest() = Request(POST, "/reverse").body(value)
override fun toResult(response: Response): Result<String, RemoteFailure> =
Success(response.bodyString())
}
fun API.reverse(value: String) = this(Reverse(value))Create the API Client with an HTTP client, and call it as normal. Failures are trapped in the returned Result.
val api = API(JavaHttpClient())
val result: Result<String, RemoteFailure> = api.reverse("hello")See how to harness the http4k Connect toolkit in your projects.
Check it out