https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
v

vmichalak

07/27/2018, 9:43 AM
Hi guys, i currently attempt to work on a multiplatform HTTP Client for Kotlin. JS and Java works for the moment but perhaps not in the most optimized way. Any people interessed to help me on this ?
o

orangy

07/27/2018, 10:04 AM
We will publish one as part of #ktor very soon
👍 3
v

vmichalak

07/27/2018, 10:35 AM
Oh interesting. If you want help from me :)
k

krtko

07/27/2018, 3:55 PM
One thing I've found to be good general advice if you're mapping an expect/actual to an existing platform class, is to write the expects are extensions functions. This makes it possible to create similar functionality without having to create a wrapper around there class.
t

thevery

07/27/2018, 5:31 PM
@krtko any example for this?^
k

krtko

07/27/2018, 5:57 PM
Sure heres a simple Http library I wrote
Copy code
expect class HttpClient
expect fun HttpClient.getUrlBuilder(baseUrl: String): UrlBuilder
expect fun HttpClient.getHttpRequest(methodType: String, url: Url): HttpRequest
expect fun HttpClient.send(body: String?, httpRequest: HttpRequest, complete: HttpRequestCallback)
Copy code
actual class HttpClient
actual fun HttpClient.getUrlBuilder(baseUrl: String): UrlBuilder {
    return URL(baseUrl)
}
actual fun HttpClient.getHttpRequest(methodType: String, url: Url): HttpRequest {
    val httpRequest = XMLHttpRequest()
    httpRequest.open(methodType,url)
    return httpRequest
}
actual fun HttpClient.send(body: String?, httpRequest: HttpRequest, complete: HttpRequestCallback) {
    httpRequest.onreadystatechange = {
        if (httpRequest.readyState == HTTP_FINISHED_CODE) {
            complete(httpRequest.status.toInt(),httpRequest.responseText)
        }
    }
    httpRequest.send(body)
}
I wouldn't suggest anyone using it, but it shows what I mean well enough
t

thevery

07/27/2018, 6:17 PM
@krtko and what about HttpRequest class - expect with actual typealias?
k

krtko

07/27/2018, 7:06 PM
Good point, this is actually really to allow for OkHttp
on the JS platform its not necessary, but it is to create a common API
t

thevery

07/27/2018, 8:26 PM
@krtko ha, I've wrapped okhttp, too
k

krtko

07/27/2018, 8:27 PM
Ya which is the point, its not suppose to be a wrapper just a common API over pre-existing libraries