mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-08-14 09:13:42 +01:00
Use okhttp-coroutines library.
This commit is contained in:
@@ -179,6 +179,7 @@ greenrobot-eventbus = "org.greenrobot:eventbus:3.0.0"
|
||||
jackson-core = "com.fasterxml.jackson.core:jackson-databind:2.12.0"
|
||||
jackson-module-kotlin = "com.fasterxml.jackson.module:jackson-module-kotlin:2.12.0"
|
||||
square-okhttp3 = "com.squareup.okhttp3:okhttp:5.3.2"
|
||||
square-okhttp3-coroutines = "com.squareup.okhttp3:okhttp-coroutines:5.3.2"
|
||||
square-okio = "com.squareup.okio:okio:3.17.0"
|
||||
square-leakcanary = "com.squareup.leakcanary:leakcanary-android:2.14"
|
||||
rxjava3-rxjava = "io.reactivex.rxjava3:rxjava:3.1.12"
|
||||
|
||||
@@ -5515,6 +5515,14 @@ https://docs.gradle.org/current/userguide/dependency_verification.html
|
||||
<sha256 value="f1a78812ebeef4d45eee0dbeb4f27347210f564d2b8ee411f012ce8573b0a658" origin="Generated by Gradle"/>
|
||||
</artifact>
|
||||
</component>
|
||||
<component group="com.squareup.okhttp3" name="okhttp-coroutines" version="5.3.2">
|
||||
<artifact name="okhttp-coroutines-5.3.2.jar">
|
||||
<sha256 value="44371091a9a5821f58cc8149781f5f8a05691dc562fa3f96775f4310e6aa277e" origin="Generated by Gradle"/>
|
||||
</artifact>
|
||||
<artifact name="okhttp-coroutines-5.3.2.module">
|
||||
<sha256 value="58648e33aaf7f0671e01747dcb93b0ee4cb61643fa35825d5aedde448318cf66" origin="Generated by Gradle"/>
|
||||
</artifact>
|
||||
</component>
|
||||
<component group="com.squareup.okhttp3" name="okhttp-jvm" version="5.3.2">
|
||||
<artifact name="okhttp-jvm-5.3.2.jar">
|
||||
<sha256 value="c771f48075b763f6c322055e21129a94b7de4c1faf3f8f58ace320b0c9517a30" origin="Generated by Gradle"/>
|
||||
|
||||
@@ -48,6 +48,7 @@ dependencies {
|
||||
|
||||
implementation(libs.libsignal.client)
|
||||
api(libs.square.okio)
|
||||
implementation(libs.square.okhttp3.coroutines)
|
||||
|
||||
api(libs.rxjava3.rxjava)
|
||||
|
||||
|
||||
@@ -5,9 +5,7 @@
|
||||
|
||||
package org.signal.network.rest
|
||||
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.ConnectionPool
|
||||
import okhttp3.ConnectionSpec
|
||||
import okhttp3.Credentials
|
||||
@@ -20,6 +18,7 @@ import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import okhttp3.coroutines.executeAsync
|
||||
import okio.Buffer
|
||||
import okio.BufferedSink
|
||||
import okio.ByteString
|
||||
@@ -50,7 +49,6 @@ import java.util.Random
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.net.ssl.SSLContext
|
||||
import javax.net.ssl.X509TrustManager
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.jvm.Throws
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@@ -260,7 +258,7 @@ class SignalRestClient @JvmOverloads constructor(
|
||||
}
|
||||
val httpRequest = buildHttpRequest(spec, holder, effectiveBody, extraHeaders = emptyMap())
|
||||
val client = newCallClient(holder)
|
||||
val response = client.newCall(httpRequest).await()
|
||||
val response = client.newCall(httpRequest).executeAsync()
|
||||
|
||||
response.use { resp ->
|
||||
val body = resp.body.bytes()
|
||||
@@ -297,7 +295,7 @@ class SignalRestClient @JvmOverloads constructor(
|
||||
val httpRequest = buildHttpRequest(spec, holder, body = spec.body, extraHeaders = rangeHeader)
|
||||
val client = newCallClient(holder)
|
||||
val call = client.newCall(httpRequest)
|
||||
val response = call.await()
|
||||
val response = call.executeAsync()
|
||||
|
||||
response.use { resp ->
|
||||
val code = resp.code
|
||||
@@ -494,19 +492,6 @@ class SignalRestClient @JvmOverloads constructor(
|
||||
return map
|
||||
}
|
||||
|
||||
private suspend fun Call.await(): Response = suspendCancellableCoroutine { cont ->
|
||||
cont.invokeOnCancellation { runCatching { cancel() } }
|
||||
enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
if (!cont.isCancelled) cont.resumeWith(Result.failure(e))
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
cont.resume(response)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private data class ConnectionHolder(
|
||||
val client: OkHttpClient,
|
||||
val url: String,
|
||||
|
||||
Reference in New Issue
Block a user