Skip to content

Commit 0f6c50b

Browse files
authored
Version 0.12.0-eap13 - stable coroutines (#43)
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) Migration to SuccessOrFailure
1 parent 2583e6c commit 0f6c50b

File tree

6 files changed

+41
-32
lines changed

6 files changed

+41
-32
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## Version 0.12.0-eap13 (2017-08-04) - Stable coroutines
4+
5+
First version of kotlin-coroutines-retrofit based on stable coroutines API from Kotlin 1.3
6+
Compiled against Kotlin 1.3-M1 and kotlinx.coroutines 0.24.0-eap13 (also based on stable API)
7+
8+
39
## Version 0.12.0 (2017-08-04)
410

511
- [kotlinx.coroutines 0.24.0](https://github.com/Kotlin/kotlinx.coroutines/releases/tag/0.24.0)

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ This is a small library that provides the [Kotlin Coroutines](https://github.com
88

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

11-
This branch uses Kotlin experimental package `kotlin.coroutines.experimental` (pre-1.3).
12-
13-
Migration to package stable `kotlin.coroutines` package is planned and work in progress.
11+
This branch uses stable version of Kotlin coroutines and work only on Kotlin 1.3 (including EAP builds)
1412

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

1816
Gradle:
1917

2018
```groovy
21-
compile 'ru.gildor.coroutines:kotlin-coroutines-retrofit:0.12.0'
19+
compile 'ru.gildor.coroutines:kotlin-coroutines-retrofit:0.12.0-eap13'
2220
```
2321

2422
Maven:getOrThrow
@@ -27,7 +25,7 @@ Maven:getOrThrow
2725
<dependency>
2826
<groupId>ru.gildor.coroutines</groupId>
2927
<artifactId>kotlin-coroutines-retrofit</artifactId>
30-
<version>0.12.0</version>
28+
<version>0.12.0-eap13</version>
3129
</dependency>
3230
```
3331

build.gradle.kts

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin
1212
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
1313

1414
plugins {
15-
id("org.jetbrains.kotlin.jvm") version "1.2.60"
15+
id("org.jetbrains.kotlin.jvm") version "1.3-M1"
1616
id("com.jfrog.bintray") version "1.8.4"
1717
jacoco
1818
`maven-publish`
1919
id("org.jetbrains.dokka") version "0.9.16"
2020
}
2121

2222
group = "ru.gildor.coroutines"
23-
version = "0.12.0"
23+
version = "0.12.0-eap13"
2424
description = "Provides Kotlin Coroutines suspendable await() extensions for Retrofit Call"
2525

2626
repositories {
2727
jcenter()
28+
maven("http://dl.bintray.com/kotlin/kotlin-eap")
2829
}
2930

3031
java {
@@ -34,15 +35,11 @@ java {
3435

3536
dependencies {
3637
compile("org.jetbrains.kotlin:kotlin-stdlib")
37-
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0")
38+
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0-eap13")
3839
compile("com.squareup.retrofit2:retrofit:2.4.0")
3940
testCompile("junit:junit:4.12")
4041
}
4142

42-
kotlin {
43-
experimental.coroutines = Coroutines.ENABLE
44-
}
45-
4643
/* Code coverage */
4744

4845
val jacocoTestReport by tasks.getting(JacocoReport::class) {

settings.gradle.kts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
rootProject.name = "kotlin-coroutines-retrofit"
1+
rootProject.name = "kotlin-coroutines-retrofit"
2+
3+
enableFeaturePreview("STABLE_PUBLISHING")
4+
5+
pluginManagement {
6+
repositories {
7+
gradlePluginPortal()
8+
jcenter()
9+
maven("http://dl.bintray.com/kotlin/kotlin-eap")
10+
}
11+
}

src/main/kotlin/ru/gildor/coroutines/retrofit/CallAwait.kt

+12-14
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package ru.gildor.coroutines.retrofit
1818

19-
import kotlinx.coroutines.experimental.CancellableContinuation
20-
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
19+
import kotlinx.coroutines.CancellableContinuation
20+
import kotlinx.coroutines.suspendCancellableCoroutine
2121
import retrofit2.Call
2222
import retrofit2.Callback
2323
import retrofit2.HttpException
2424
import retrofit2.Response
25+
import kotlin.coroutines.resume
26+
import kotlin.coroutines.resumeWithException
2527

2628
/**
2729
* Suspend extension that allows suspend [Call] inside of a coroutine.
@@ -32,18 +34,14 @@ public suspend fun <T : Any> Call<T>.await(): T {
3234
return suspendCancellableCoroutine { continuation ->
3335
enqueue(object : Callback<T> {
3436
override fun onResponse(call: Call<T>?, response: Response<T?>) {
35-
if (response.isSuccessful) {
36-
val body = response.body()
37-
if (body == null) {
38-
continuation.resumeWithException(
39-
NullPointerException("Response body is null: $response")
40-
)
37+
continuation.resumeWith(SuccessOrFailure.runCatching {
38+
if (response.isSuccessful) {
39+
response.body()
40+
?: throw NullPointerException("Response body is null: $response")
4141
} else {
42-
continuation.resume(body)
42+
throw HttpException(response)
4343
}
44-
} else {
45-
continuation.resumeWithException(HttpException(response))
46-
}
44+
})
4745
}
4846

4947
override fun onFailure(call: Call<T>, t: Throwable) {
@@ -91,7 +89,7 @@ public suspend fun <T : Any> Call<T>.awaitResult(): Result<T> {
9189
return suspendCancellableCoroutine { continuation ->
9290
enqueue(object : Callback<T> {
9391
override fun onResponse(call: Call<T>?, response: Response<T>) {
94-
continuation.resume(
92+
continuation.resumeWith(SuccessOrFailure.runCatching {
9593
if (response.isSuccessful) {
9694
val body = response.body()
9795
if (body == null) {
@@ -102,7 +100,7 @@ public suspend fun <T : Any> Call<T>.awaitResult(): Result<T> {
102100
} else {
103101
Result.Error(HttpException(response), response.raw())
104102
}
105-
)
103+
})
106104
}
107105

108106
override fun onFailure(call: Call<T>, t: Throwable) {

src/test/kotlin/ru/gildor/coroutines/retrofit/CallAwaitTest.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package ru.gildor.coroutines.retrofit
1818

19-
import kotlinx.coroutines.experimental.CoroutineScope
20-
import kotlinx.coroutines.experimental.Unconfined
21-
import kotlinx.coroutines.experimental.async
22-
import kotlinx.coroutines.experimental.runBlocking
19+
import kotlinx.coroutines.CoroutineScope
20+
import kotlinx.coroutines.Unconfined
21+
import kotlinx.coroutines.async
22+
import kotlinx.coroutines.runBlocking
2323
import org.junit.Assert.assertEquals
2424
import org.junit.Assert.assertFalse
2525
import org.junit.Assert.assertNull
@@ -32,7 +32,7 @@ import retrofit2.HttpException
3232
import ru.gildor.coroutines.retrofit.util.MockedCall
3333
import ru.gildor.coroutines.retrofit.util.NullBodyCall
3434
import ru.gildor.coroutines.retrofit.util.errorResponse
35-
import kotlin.coroutines.experimental.coroutineContext
35+
import kotlin.coroutines.coroutineContext
3636

3737
private const val DONE = "Done!"
3838

0 commit comments

Comments
 (0)