mirror of
https://github.com/bitwarden/android.git
synced 2025-03-16 03:08:50 +03:00
Allow null network responses for 204s (#3685)
This commit is contained in:
parent
994a577600
commit
e7c69fc089
1 changed files with 10 additions and 0 deletions
|
@ -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<T>(
|
|||
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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue