Skip to content

Commit

Permalink
removed eager ext
Browse files Browse the repository at this point in the history
  • Loading branch information
rybalkinsd committed Aug 4, 2019
1 parent 755cd52 commit d63a722
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 148 deletions.
26 changes: 1 addition & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,28 +456,4 @@ val customClient = client {
```

## Experimental

### Eager response
Instead of `.use { ... it.body?.string() ... }` it is now possible to read response body as string.
And also to map `Headers` to `listOf<Header>` to operate them easily.

```kotlin
val response: EagerResponse = "https://google.com/search?q=iphone".httpGet().eager()

// iterating over headers
response.headers.forEach { ... }

// manipulating body
response.body?.let { ... }

```

```kotlin
val response: EagerResponse = httpGet { }.eager()

// iterating over headers
response.headers.forEach { ... }

// manipulating body
response.body?.let { ... }
```
### none
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ fun Headers.asSequence(): Sequence<Header> = Sequence {
}
}
}

typealias Header = Pair<String, String>
Original file line number Diff line number Diff line change
@@ -1,92 +1,15 @@
package io.github.rybalkinsd.kohttp.ext

import okhttp3.Handshake
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response


/**
* This feature is EXPERIMENTAL, API could be changed in the future releases.
*
* In many cases response body is simple and it could be treated as String
*
*
* Without `eager()` the correct way to consume response body is
* val response = httpGet { }
*
* response.use {
* it.body().string()
* }
*
* However with `eager()` method it is possible to consume easier and get a `EagerResponse` instance
* val response = httpGet { }.eager()
*
*
* This method loads entire response body into memory. If the response body is very large this
* may trigger an {@link OutOfMemoryError}. Prefer to stream the response body if this is a
* possibility for your response.
*
* @since 0.3.0
* @author sergey
*/
fun Response.eager() = EagerResponse(
request = request(),
protocol = protocol(),
code = code(),
message = message(),
handshake = handshake(),
headers = (0 until headers().size()).map {
Header(headers().name(it), headers().value(it))
},
body = body()?.string(),
networkResponse = networkResponse(),
cacheResponse = cacheResponse(),
priorResponse = priorResponse(),
sentRequestAtMillis = sentRequestAtMillis(),
receivedResponseAtMillis = receivedResponseAtMillis()
)

/**
* EagerResponse is basically the same class as okhttp3.Response
* except:
* - `headers` - they are represented as a list of type `Header`
* - `body` - the entire response body in memory represented as `String
*
* @since 0.3.0
* @author sergey
*/
data class EagerResponse(
val request: Request,
val protocol: Protocol,
val code: Int,
val message: String,
val handshake: Handshake?,
val headers: List<Header>,
val body: String?,
val networkResponse: Response?,
val cacheResponse: Response?,
val priorResponse: Response?,
val sentRequestAtMillis: Long,
val receivedResponseAtMillis: Long
)

/**
* @since 0.3.0
* @author sergey
*/
data class Header(val name: String, val value: String)



/**
* Returns Response Body as String.
*
* @return Response body as a `String?`.
* @since 0.9.0
* @author gokul
*/

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

/**
Expand All @@ -96,6 +19,4 @@ fun Response.asString() = body()?.string()
* @since 0.9.0
* @author gokul
*/

fun Response.asStream() = body()?.byteStream()

Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ class HeadersExtKtTest {
}
}
}

val Header.name
get() = this.first

val Header.value
get() = this.second
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package io.github.rybalkinsd.kohttp.ext

import io.github.rybalkinsd.kohttp.dsl.httpGet
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

/**
* @author sergey
Expand All @@ -15,45 +10,6 @@ class ResponseExtKtTest {

private val getUrl = "https://postman-echo.com/get"

@Test
fun `make plain response from http get response # dsl`() {
val plainResponse = httpGet {
host = "postman-echo.com"
path = "/get"

header {
"one" to 42
"two" to 123L
}
}.eager()

with(plainResponse) {
assertEquals(200, code)
assertEquals(7, headers.size)
assertNotNull(body)
assertTrue { body!!.isNotEmpty() }
assertEquals("GET", request.method())
assertEquals("HTTP_1_1", protocol.name)
assertEquals("OK", message)
assertNotNull(networkResponse)
assertNull(cacheResponse)
assertNull(priorResponse)
assertTrue { sentRequestAtMillis < receivedResponseAtMillis }
}
}

@Test
fun `make plain response from http get response # ext`() {
val plainResponse = "https://www.yandex.ru/search/?text=iphone".httpGet().eager()

with(plainResponse) {
assertEquals(200, code)
assertTrue { headers.isNotEmpty() }
assertNotNull(body)
assertTrue { body!!.isNotEmpty() }
}
}

@Test
fun `gets response as string # ext`() {
val response = getUrl.httpGet().asString()!!
Expand Down

0 comments on commit d63a722

Please sign in to comment.