Skip to content

Commit

Permalink
Kotlin 1.3 and Kotlinx.Coroutines 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gildor committed Dec 18, 2018
2 parents 4822d57 + 41473bf commit 9cd2acb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 60 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
# CHANGELOG

## Version 0.13.0-eap13 (2017-10-10)
## Version 1.0.0 (2018-13-19)

- kotlinx.coroutines 1.0.1
- Compiled against Kotlin 1.3.11

## Version 0.13.0-eap13 (2018-10-10)

- kotlinx.coroutines 0.30.2-eap13
- Compiled against Kotlin 1.3.0-rc-146

## Version 0.13.0 (2017-10-10)
## Version 0.13.0 (2018-10-10)

- [kotlinx.coroutines 0.30.2](https://github.com/Kotlin/kotlinx.coroutines/releases/tag/0.30.2)
- Compiled against Kotlin 1.2.71

## Version 0.12.0-eap13 (2018-08-04) - Stable coroutines

First version of kotlin-coroutines-retrofit based on stable coroutines API from Kotlin 1.3
Compiled against Kotlin 1.3-M1 and kotlinx.coroutines 0.24.0-eap13 (also based on stable API)


## Version 0.12.0 (2017-08-04)

- [kotlinx.coroutines 0.24.0](https://github.com/Kotlin/kotlinx.coroutines/releases/tag/0.24.0)
Expand Down
29 changes: 6 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,18 @@ This is a small library that provides the [Kotlin Coroutines](https://github.com

Based on [kotlinx.coroutines](https://github.com/Kotlin/kotlinx.coroutines) implementation.

## Download

Download the [JAR](https://bintray.com/gildor/maven/kotlin-coroutines-retrofit#files/ru/gildor/coroutines/kotlin-coroutines-retrofit):

### If you use Kotlin 1.2: Version of the library based on experimental coroutines API

Gradle:
New version of library (after 1.0.0) support only Kotlin 1.3

```groovy
compile 'ru.gildor.coroutines:kotlin-coroutines-retrofit:0.13.0'
```

Maven:
Kotlin 1.2 and experimental coroutines are not supported anymore, but you can use version `0.13.0` for old projects.

```xml
<dependency>
<groupId>ru.gildor.coroutines</groupId>
<artifactId>kotlin-coroutines-retrofit</artifactId>
<version>0.13.0</version>
</dependency>
```

### If you use Kotlin 1.3: Version based on stable coroutines API
## Download

Download the [JAR](https://bintray.com/gildor/maven/kotlin-coroutines-retrofit#files/ru/gildor/coroutines/kotlin-coroutines-retrofit):

Gradle:

```groovy
compile 'ru.gildor.coroutines:kotlin-coroutines-retrofit:0.13.0-eap13'
compile 'ru.gildor.coroutines:kotlin-coroutines-retrofit:1.0.0'
```

Maven:
Expand All @@ -45,7 +28,7 @@ Maven:
<dependency>
<groupId>ru.gildor.coroutines</groupId>
<artifactId>kotlin-coroutines-retrofit</artifactId>
<version>0.13.0-eap13</version>
<version>1.0.0</version>
</dependency>
```

Expand Down
12 changes: 4 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper

plugins {
id("org.jetbrains.kotlin.jvm") version "1.2.71"
id("org.jetbrains.kotlin.jvm") version "1.3.11"
id("com.jfrog.bintray") version "1.8.4"
jacoco
`maven-publish`
id("org.jetbrains.dokka") version "0.9.16"
}

group = "ru.gildor.coroutines"
version = "0.13.0"
version = "1.0.0"
description = "Provides Kotlin Coroutines suspendable await() extensions for Retrofit Call"

repositories {
Expand All @@ -34,15 +34,11 @@ java {

dependencies {
compile("org.jetbrains.kotlin:kotlin-stdlib")
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0")
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1")
compile("com.squareup.retrofit2:retrofit:2.4.0")
testCompile("junit:junit:4.12")
}

kotlin {
experimental.coroutines = Coroutines.ENABLE
}

/* Code coverage */

val jacocoTestReport by tasks.getting(JacocoReport::class) {
Expand Down Expand Up @@ -81,7 +77,7 @@ val releaseTag = "v${project.version}"
val sourcesJar by tasks.creating(Jar::class) {
dependsOn("classes")
classifier = "sources"
from(sourceSets["main"].allSource)
from(sourceSets["main"].allJava)
}

val javadocJar by tasks.creating(Jar::class) {
Expand Down
26 changes: 12 additions & 14 deletions src/main/kotlin/ru/gildor/coroutines/retrofit/CallAwait.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

package ru.gildor.coroutines.retrofit

import kotlinx.coroutines.experimental.CancellableContinuation
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.suspendCancellableCoroutine
import retrofit2.Call
import retrofit2.Callback
import retrofit2.HttpException
import retrofit2.Response
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

/**
* Suspend extension that allows suspend [Call] inside of a coroutine.
Expand All @@ -32,18 +34,14 @@ public suspend fun <T : Any> Call<T>.await(): T {
return suspendCancellableCoroutine { continuation ->
enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>?, response: Response<T?>) {
if (response.isSuccessful) {
val body = response.body()
if (body == null) {
continuation.resumeWithException(
NullPointerException("Response body is null: $response")
)
continuation.resumeWith(runCatching {
if (response.isSuccessful) {
response.body()
?: throw NullPointerException("Response body is null: $response")
} else {
continuation.resume(body)
throw HttpException(response)
}
} else {
continuation.resumeWithException(HttpException(response))
}
})
}

override fun onFailure(call: Call<T>, t: Throwable) {
Expand Down Expand Up @@ -91,7 +89,7 @@ public suspend fun <T : Any> Call<T>.awaitResult(): Result<T> {
return suspendCancellableCoroutine { continuation ->
enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>?, response: Response<T>) {
continuation.resume(
continuation.resumeWith(runCatching {
if (response.isSuccessful) {
val body = response.body()
if (body == null) {
Expand All @@ -102,7 +100,7 @@ public suspend fun <T : Any> Call<T>.awaitResult(): Result<T> {
} else {
Result.Error(HttpException(response), response.raw())
}
)
})
}

override fun onFailure(call: Call<T>, t: Throwable) {
Expand Down
26 changes: 13 additions & 13 deletions src/test/kotlin/ru/gildor/coroutines/retrofit/CallAwaitTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package ru.gildor.coroutines.retrofit

import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.Unconfined
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.runBlocking
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
Expand All @@ -32,10 +33,10 @@ import retrofit2.HttpException
import ru.gildor.coroutines.retrofit.util.MockedCall
import ru.gildor.coroutines.retrofit.util.NullBodyCall
import ru.gildor.coroutines.retrofit.util.errorResponse
import kotlin.coroutines.experimental.coroutineContext

private const val DONE = "Done!"

@ExperimentalCoroutinesApi
class CallAwaitTest {
@Test
fun asyncSuccess() = testBlocking {
Expand Down Expand Up @@ -260,7 +261,7 @@ class CallAwaitTest {
autoStart = false,
cancelException = IllegalStateException()
)
val async = async(coroutineContext, block = { block(request) })
val async = async(Dispatchers.Unconfined, block = { block(request) })
//We shouldn't crash on cancel exception
try {
assertFalse(request.isCanceled)
Expand All @@ -276,7 +277,7 @@ class CallAwaitTest {
exception = IllegalArgumentException(),
autoStart = false
)
val result = async(coroutineContext) {
val result = async(Dispatchers.Unconfined) {
block(request)
}
result.cancel()
Expand All @@ -288,14 +289,13 @@ class CallAwaitTest {
block: suspend (Call<String>) -> T
) = testBlocking {
val request = MockedCall(DONE, autoStart = false)
val async = async(coroutineContext) { block(request) }
val async = async(Dispatchers.Unconfined) { block(request) }
assertFalse(request.isCanceled)
async.cancel()
assertTrue(request.isCanceled)
}

private fun testBlocking(block: suspend CoroutineScope.() -> Unit) {
runBlocking(block = block)
}
}

private fun testBlocking(block: suspend CoroutineScope.() -> Unit) {
runBlocking(Unconfined, block)
}

0 comments on commit 9cd2acb

Please sign in to comment.