From e7c69fc0897cd3158af16dafb60f6e2fdba468c9 Mon Sep 17 00:00:00 2001 From: David Perez Date: Tue, 6 Aug 2024 10:13:57 -0500 Subject: [PATCH] Allow null network responses for 204s (#3685) --- .../platform/datasource/network/core/ResultCall.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/core/ResultCall.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/core/ResultCall.kt index 1cdbade3f7..0d312c22ca 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/core/ResultCall.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/network/core/ResultCall.kt @@ -11,6 +11,11 @@ import retrofit2.HttpException import retrofit2.Response import java.lang.reflect.Type +/** + * The integer code value for a "No Content" response. + */ +private const val NO_CONTENT_RESPONSE_CODE: Int = 204 + /** * A [Call] for wrapping a network request into a [Result]. */ @@ -64,8 +69,13 @@ class ResultCall( val body = this.body() @Suppress("UNCHECKED_CAST") when { + // We got a nonnull T as the body, just return it. body != null -> body.asSuccess() + // We expected the body to be null since the successType is Unit, just return Unit. successType == Unit::class.java -> (Unit as T).asSuccess() + // We allow null for 204's, just return null. + this.code() == NO_CONTENT_RESPONSE_CODE -> (null as T).asSuccess() + // All other null bodies result in an error. else -> IllegalStateException("Unexpected null body!").asFailure() } }