rybalkinsd / kohttp

Kotlin DSL http client
https://kohttp.gitbook.io
Apache License 2.0
478 stars 42 forks source link

Response extension functions #65

Closed rybalkinsd closed 5 years ago

rybalkinsd commented 5 years ago

Response is rather verbose, especially accessing body content.

httpXXX { } .use {
        val content = it.body()?.string().asJson()
}

Need to observe other http clients and figure out how dsl approach can serve users for clear response consumption.

Previously EagerResponse was introduced, however it's usage is also rather complex

gokulchandra commented 5 years ago

@rybalkinsd I guess the challenge is in how the API should look when we have to deal with different data types in the response. From a high level I guess we could categorise types under :

httpXXX { } .use {
        val textContent = text() // returns response textual content
        val content = json() // returns response body as json if exists and valid json
}

Thoughts?

rybalkinsd commented 5 years ago

m/b it's better to allow interaction without use (handle on our side)

There are two big cases.

  1. User want to get content eagerly, format does not matter
  2. User does not want to have all the content in memory usually a byte stream
rybalkinsd commented 5 years ago

for example Fuel has .responseString { request, response, result -> ... } API

rybalkinsd commented 5 years ago

Here is a reference how http4k acts. Our goal is to analyze their experience and provide the most fluent and easiest DSL as possible

gokulchandra commented 5 years ago

@rybalkinsd I took a look at what Fuel provides. Correct me if I don't understand this. Here's a rough idea.

// Defining this could allow access to the String Content in response

internal fun Response.string() = body()?.string()

val stringResponse = "https://www.yandex.ru/search/?text=iphone".httpGet().string()

I understand this isn't a very DSL based approach, but its allows easier access since the user does not need to be concerned with anything(Request, Response, Result) unlike even what Fuel provides with .responseString { request, response, result -> ... }.

rybalkinsd commented 5 years ago

@gokulchandra looks good for me. how about asXXX? it's more common naming pattern in my mind

inline fun Response.asString(): String(?) = body()?.string()

inline fun Response.asJson() ...

The only thing we didn't discuss yet - asStream, as you mentioned before

gokulchandra commented 5 years ago

I should have Draft PR for this issue over the next 2 days. 👍 We can improve on that.

rybalkinsd commented 5 years ago

Please also have a look at my latest commits, there is a bit more developed upload dsl idea together with multipart

gokulchandra commented 5 years ago

Using #82 to work on this.