Skip to content
HTTP networking abstraction for Kotlin Multiplatform
Kotlin Swift Groovy Ruby
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Add documentation (#6) Nov 26, 2019
android-ktx mirego.release commit next working version Nov 27, 2019
gradle/wrapper
http mirego.release commit next working version Dec 4, 2019
jenkins-jobs Add android ktx Aug 19, 2019
swift-extensions Upgrade to kotlin 1.3.61 (#13) Dec 4, 2019
.gitignore Initial commit Aug 14, 2019
.travis.yml
CODE_OF_CONDUCT.md Add documentation (#6) Nov 26, 2019
LICENSE.md
README.md Improve documentation (#12) Nov 29, 2019
build.gradle
gradle.properties
gradlew Initial commit Aug 14, 2019
gradlew.bat Initial commit Aug 14, 2019
package.json Update dependencies, add documentaion Aug 16, 2019
settings.gradle
trikot.http.podspec Use Reachability Swift Aug 14, 2019

README.md

Trikot.http

Incubating

Kotlin Multiplaform http networking abstraction.

  • Default Http request implementation for each platform
  • Http header management and Error management
  • Connectivity (Reachability) management
  • Deserialization of results using kotlinx.serialization
  • Abstract network in Kotlin Multiplatform making sure every bit of network code is testable.
  • Implement Reactive Streams pattern

RequestBuilder

Request builder is a simple class that provides information about the request to send. See RequestBuilder.kt for fields documentation.

HttpRequestPublisher

To send http requests

  • Create a child class of HttpRequestPublisher
  • Provide a RequestBuilder by overriding builder var
  • Override processResponse to transform HttpResponse into expected result type.
val request = object: HttpRequestPublisher<String>() {
    override val builder = RequestBuilder().also {
        it.baseUrl = "http://www.site.com/path/to/request"
    }

    override fun processResponse(response: HttpResponse): String = response.bodyString ?: ""
}
request.execute()

DeserializableHttpRequestPublisher

To deserialize a JSON payload:

  • Create a kotlix.serializable class
  • Create a DeserializableHttpRequestPublisher with the serializer in parameter.
@Serializable
data class Foo(val bar: String)

val request = DeserializableHttpRequestPublisher<Foo>(
    Foo.serializer,
    RequestBuilder().also { it.path = "/getFoo" }
)
request.execute()

HeaderProvider

HttpRequestPublisher and DeserializableHttpRequestPublisher both haves an optional httpHeaderProvider constructor parameter to provide additionnal request header (Authorization token per example). See HttpHeaderProvider.kt for complete documentation.

ConnectivityPublisher

When swift-extensions or android-ktx are configured (See below), you can access the ConnectivityState using the following sample:

HttpConfiguration.connectibityPublisher.subscribe(cancelableManager) {
    if it == NONE {
        print("No connection")
    } else {
        print("We have connection")
    }
}

Values are WIFI, CELLULAR, NONE

Installation

Import dependencies
    api "com.mirego.trikot:http:$trikot_http_version"
    jvm "com.mirego.trikot:http-jvm:$trikot_http_version"
    js "com.mirego.trikot:http-js:$trikot_http_version"
    iosx64 "com.mirego.trikot:http-iosx64:$trikot_http_version"
    iosarm64 "com.mirego.trikot:http-iosarm64:$trikot_http_version"
Setup platforms

License

Trikot.http is © 2018-2019 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We’re a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.

You can’t perform that action at this time.