Coder Social home page Coder Social logo

mirego / trikot.http Goto Github PK

View Code? Open in Web Editor NEW
15.0 29.0 1.0 304 KB

HTTP networking abstraction for Kotlin Multiplatform

Home Page: https://open.mirego.com

License: BSD 3-Clause "New" or "Revised" License

Kotlin 79.16% Groovy 2.37% Swift 16.91% Ruby 1.56%
kotlin kotlin-multiplatform http trikot

trikot.http's Introduction

⚠️ Warning: this project as been moved

See the new trikot repository for releases starting from 3.0.0.

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.

NoResponseHttpRequestPublisher

If there is no need to parse the body of the response, use this publisher instead. Only errors will be handled.

ResponseHttpRequestPublisher

If you want the raw HttpResponse object, use this publisher.

@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.

trikot.http's People

Contributors

antoinelamy avatar boubalou avatar captaindaniel avatar gbourassa avatar gingman avatar hugolefrancois avatar jdtremblay avatar jfcartkeep avatar jpmartin avatar madumo avatar marcantoinefortier avatar marclefrancois avatar martingagnon avatar mirego-builds avatar npresseault avatar thermech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

cybernetics

trikot.http's Issues

HTTPRequest execution should return a Promise instead of a Publisher

Current interface

interface HttpRequest {
    @JsName("execute")
    fun execute(cancellableManager: CancellableManager): Publisher<HttpResponse>
} //                                                     ^^^^^^^^^

Desired interface

interface HttpRequest {
    @JsName("execute")
    fun execute(cancellableManager: CancellableManager): Promise<HttpResponse>
} //                                                     ^^^^^^^

Why?

A Promise has a stronger contract than a Publisher regarding the possible outcome of the HTTP request. The current interface is a bit "misleading" in the sense that a Publisher can emit multiple values in time, while a Promise would explicitly enforce that you will receive only one value/error.

Also, returning a Promise would allow multiple HTTP requests to be chained more easily.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.