From 66c5a35f3698fc77300a4249c4dc7626b734ca40 Mon Sep 17 00:00:00 2001 From: Tobias Preuss Date: Mon, 22 Jun 2020 16:58:51 +0200 Subject: [PATCH] Use retrofit2.Call.awaitResponse extension provided by Retrofit 2. + This extension is identical to the one used in this project and is available since Retrofit 2.6.0. See https://github.com/square/retrofit/commit/b761518aa174c7b0512b73f2fe70e2e908f24081. --- CHANGES.md | 1 + .../android/internal/network/Request.kt | 7 ++++--- .../internal/network/RetrofitExtensions.kt | 21 +------------------ 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2eba3e781a..44b66fcf39 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ Build 🧱: - SDK is now API level 21 minimum, and so RiotX (#405) Other changes: + - Use `retrofit2.Call.awaitResponse` extension provided by Retrofit 2. (#1526) - Fix minor typo in contribution guide (#1512) - Fix self-assignment of callback in `DefaultRoomPushRuleService#setRoomNotificationState` (#1520) - Random housekeeping clean-ups indicated by Lint (#1520) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/Request.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/Request.kt index ea036f775b..44096fca71 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/Request.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/Request.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019 New Vector Ltd + * Copyright 2020 New Vector Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,12 +22,13 @@ import kotlinx.coroutines.CancellationException import kotlinx.coroutines.delay import org.greenrobot.eventbus.EventBus import retrofit2.Call +import retrofit2.awaitResponse import java.io.IOException -internal suspend inline fun executeRequest(eventBus: EventBus?, +internal suspend inline fun executeRequest(eventBus: EventBus?, block: Request.() -> Unit) = Request(eventBus).apply(block).execute() -internal class Request(private val eventBus: EventBus?) { +internal class Request(private val eventBus: EventBus?) { var isRetryable = false var initialDelay: Long = 100L diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/RetrofitExtensions.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/RetrofitExtensions.kt index 2a2076db6c..aa8fe46795 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/RetrofitExtensions.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/RetrofitExtensions.kt @@ -1,6 +1,6 @@ /* * - * * Copyright 2019 New Vector Ltd + * * Copyright 2020 New Vector Ltd * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. @@ -26,8 +26,6 @@ import im.vector.matrix.android.internal.di.MoshiProvider import kotlinx.coroutines.suspendCancellableCoroutine import okhttp3.ResponseBody import org.greenrobot.eventbus.EventBus -import retrofit2.Call -import retrofit2.Callback import retrofit2.Response import timber.log.Timber import java.io.IOException @@ -35,23 +33,6 @@ import java.net.HttpURLConnection import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException -internal suspend fun Call.awaitResponse(): Response { - return suspendCancellableCoroutine { continuation -> - continuation.invokeOnCancellation { - cancel() - } - enqueue(object : Callback { - override fun onResponse(call: Call, response: Response) { - continuation.resume(response) - } - - override fun onFailure(call: Call, t: Throwable) { - continuation.resumeWithException(t) - } - }) - } -} - internal suspend fun okhttp3.Call.awaitResponse(): okhttp3.Response { return suspendCancellableCoroutine { continuation -> continuation.invokeOnCancellation {