Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response extension functions #65

Closed
rybalkinsd opened this issue Jan 8, 2019 · 9 comments
Closed

Response extension functions #65

rybalkinsd opened this issue Jan 8, 2019 · 9 comments
Assignees
Milestone

Comments

@rybalkinsd
Copy link
Owner

rybalkinsd commented Jan 8, 2019

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
Copy link
Collaborator

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

  • byte arrays
  • strings
  • json
  • streams (have to figure out how to handle streams)
httpXXX { } .use {
		val textContent = text() // returns response textual content
        val content = json() // returns response body as json if exists and valid json
}

Thoughts?

@rybalkinsd
Copy link
Owner Author

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
Copy link
Owner Author

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

@rybalkinsd
Copy link
Owner Author

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
Copy link
Collaborator

@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
Copy link
Owner Author

@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
Copy link
Collaborator

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

@rybalkinsd
Copy link
Owner Author

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

@gokulchandra
Copy link
Collaborator

Using #82 to work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants