@@ -25,95 +25,87 @@ import kotlin.coroutines.intrinsics.intercepted
25
25
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
26
26
import kotlin.coroutines.resume
27
27
import kotlin.coroutines.resumeWithException
28
+ import kotlinx.coroutines.CoroutineDispatcher
29
+ import kotlinx.coroutines.withContext
28
30
29
31
inline fun <reified T : Any > Retrofit.create (): T = create(T ::class .java)
30
32
31
- suspend fun <T : Any > Call<T>.await (): T {
32
- return suspendCancellableCoroutine { continuation ->
33
- continuation.invokeOnCancellation {
34
- cancel()
35
- }
36
- enqueue(object : Callback <T > {
37
- override fun onResponse (call : Call <T >, response : Response <T >) {
38
- if (response.isSuccessful) {
39
- val body = response.body()
40
- if (body == null ) {
41
- val invocation = call.request().tag(Invocation ::class .java)!!
42
- val method = invocation.method()
43
- val e = KotlinNullPointerException (" Response from " +
33
+ @JvmOverloads
34
+ suspend fun <T : Any > Call<T>.await (coroutineDispatcher : CoroutineDispatcher ? = null): T {
35
+ return withContext(coroutineDispatcher ? : Dispatchers .Default ) {
36
+ suspendCancellableCoroutine { continuation ->
37
+ continuation.invokeOnCancellation {
38
+ cancel()
39
+ }
40
+ enqueue(object : Callback <T > {
41
+ override fun onResponse (call : Call <T >, response : Response <T >) {
42
+ if (response.isSuccessful) {
43
+ val body = response.body()
44
+ if (body == null ) {
45
+ val invocation = call.request().tag(Invocation ::class .java)!!
46
+ val method = invocation.method()
47
+ val e = KotlinNullPointerException (" Response from " +
44
48
method.declaringClass.name +
45
49
' .' +
46
50
method.name +
47
51
" was null but response body type was declared as non-null" )
48
- continuation.resumeWithException(e)
52
+ continuation.resumeWithException(e)
53
+ } else {
54
+ continuation.resume(body)
55
+ }
49
56
} else {
50
- continuation.resume(body )
57
+ continuation.resumeWithException( HttpException (response) )
51
58
}
52
- } else {
53
- continuation.resumeWithException(HttpException (response))
54
59
}
55
- }
56
60
57
- override fun onFailure (call : Call <T >, t : Throwable ) {
58
- continuation.resumeWithException(t)
59
- }
60
- })
61
+ override fun onFailure (call : Call <T >, t : Throwable ) {
62
+ continuation.resumeWithException(t)
63
+ }
64
+ })
65
+ }
61
66
}
62
67
}
63
68
64
69
@JvmName(" awaitNullable" )
65
- suspend fun <T : Any > Call<T?>.await (): T ? {
66
- return suspendCancellableCoroutine { continuation ->
67
- continuation.invokeOnCancellation {
68
- cancel()
69
- }
70
- enqueue(object : Callback <T ?> {
71
- override fun onResponse (call : Call <T ?>, response : Response <T ?>) {
72
- if (response.isSuccessful) {
73
- continuation.resume(response.body())
74
- } else {
75
- continuation.resumeWithException(HttpException (response))
76
- }
70
+ suspend fun <T : Any > Call<T?>.await (coroutineDispatcher : CoroutineDispatcher ? ): T ? {
71
+ return withContext(coroutineDispatcher ? : Dispatchers .Default ) {
72
+ suspendCancellableCoroutine { continuation ->
73
+ continuation.invokeOnCancellation {
74
+ cancel()
77
75
}
76
+ enqueue(object : Callback <T ?> {
77
+ override fun onResponse (call : Call <T ?>, response : Response <T ?>) {
78
+ if (response.isSuccessful) {
79
+ continuation.resume(response.body())
80
+ } else {
81
+ continuation.resumeWithException(HttpException (response))
82
+ }
83
+ }
78
84
79
- override fun onFailure (call : Call <T ?>, t : Throwable ) {
80
- continuation.resumeWithException(t)
81
- }
82
- })
85
+ override fun onFailure (call : Call <T ?>, t : Throwable ) {
86
+ continuation.resumeWithException(t)
87
+ }
88
+ })
89
+ }
83
90
}
84
91
}
85
92
86
- suspend fun <T > Call<T>.awaitResponse (): Response <T > {
87
- return suspendCancellableCoroutine { continuation ->
88
- continuation.invokeOnCancellation {
89
- cancel()
90
- }
91
- enqueue(object : Callback <T > {
92
- override fun onResponse (call : Call <T >, response : Response <T >) {
93
- continuation.resume(response)
94
- }
95
-
96
- override fun onFailure (call : Call <T >, t : Throwable ) {
97
- continuation.resumeWithException(t)
93
+ @JvmOverloads
94
+ suspend fun <T > Call<T>.awaitResponse (coroutineDispatcher : CoroutineDispatcher ? = null): Response <T > {
95
+ return withContext(coroutineDispatcher ? : Dispatchers .Default ) {
96
+ suspendCancellableCoroutine { continuation ->
97
+ continuation.invokeOnCancellation {
98
+ cancel()
98
99
}
99
- })
100
- }
101
- }
100
+ enqueue(object : Callback <T > {
101
+ override fun onResponse (call : Call <T >, response : Response <T >) {
102
+ continuation.resume(response)
103
+ }
102
104
103
- /* *
104
- * Force the calling coroutine to suspend before throwing [this].
105
- *
106
- * This is needed when a checked exception is synchronously caught in a [java.lang.reflect.Proxy]
107
- * invocation to avoid being wrapped in [java.lang.reflect.UndeclaredThrowableException].
108
- *
109
- * The implementation is derived from:
110
- * https://github.com/Kotlin/kotlinx.coroutines/pull/1667#issuecomment-556106349
111
- */
112
- internal suspend fun Exception.suspendAndThrow (): Nothing {
113
- suspendCoroutineUninterceptedOrReturn<Nothing > { continuation ->
114
- Dispatchers .Default .dispatch(continuation.context) {
115
- continuation.intercepted().resumeWithException(this @suspendAndThrow)
105
+ override fun onFailure (call : Call <T >, t : Throwable ) {
106
+ continuation.resumeWithException(t)
107
+ }
108
+ })
116
109
}
117
- COROUTINE_SUSPENDED
118
110
}
119
111
}
0 commit comments