From 4451b682b17ba9d4cb6a853a747fad94e93f5d6a Mon Sep 17 00:00:00 2001 From: Aleks Date: Thu, 8 Apr 2021 11:30:41 +0200 Subject: [PATCH] merge + register/available endpoint corrections --- .../android/sdk/api/auth/data/Availability.kt | 2 +- .../registration/RegistrationAvailability.kt | 4 ++-- .../android/sdk/internal/auth/AuthAPI.kt | 2 +- .../registration/DefaultRegistrationWizard.kt | 15 +----------- .../registration/RegisterAvailableTask.kt | 24 ++++++++++++++----- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt index 3fbbdd161f..93e0ac4e44 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/data/Availability.kt @@ -20,7 +20,7 @@ import com.squareup.moshi.Json import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) -data class Availability( +internal data class Availability( @Json(name = "available") val available: Boolean ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt index 7c7d769a35..5f944a86d9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/registration/RegistrationAvailability.kt @@ -19,6 +19,6 @@ package org.matrix.android.sdk.api.auth.registration import org.matrix.android.sdk.api.failure.Failure sealed class RegistrationAvailability { - data class Available(val available: Boolean): RegistrationAvailability() + object Available : RegistrationAvailability() data class NotAvailable(val failure: Failure.ServerError): RegistrationAvailability() -} \ No newline at end of file +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt index 6566aae5fe..216be83ea3 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/AuthAPI.kt @@ -66,7 +66,7 @@ internal interface AuthAPI { * Checks to see if a username is available, and valid, for the server. */ @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "register/available") - fun registerAvailable(@Query("username") username: String): Call + suspend fun registerAvailable(@Query("username") username: String): Availability /** * Add 3Pid during registration diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt index 3be95f6a4a..ace2e62043 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/DefaultRegistrationWizard.kt @@ -21,7 +21,6 @@ import org.matrix.android.sdk.api.auth.data.LoginFlowTypes import org.matrix.android.sdk.api.auth.registration.* import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure.RegistrationFlowError -import org.matrix.android.sdk.api.failure.isRegistrationAvailabilityError import org.matrix.android.sdk.internal.auth.AuthAPI import org.matrix.android.sdk.internal.auth.PendingSessionStore import org.matrix.android.sdk.internal.auth.SessionCreator @@ -203,17 +202,5 @@ internal class DefaultRegistrationWizard( return RegistrationResult.Success(session) } - override suspend fun registrationAvailable(userName: String): RegistrationAvailability { - val availability = try { - registerAvailableTask.execute(userName) - } catch (exception: Throwable) { - if(exception.isRegistrationAvailabilityError()) { - return RegistrationAvailability.NotAvailable(exception as Failure.ServerError) - } else { - throw exception - } - } - - return RegistrationAvailability.Available(availability.available) - } + override suspend fun registrationAvailable(userName: String): RegistrationAvailability = registerAvailableTask.execute(userName) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt index aa2bb767f9..791b725357 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/registration/RegisterAvailableTask.kt @@ -16,15 +16,27 @@ package org.matrix.android.sdk.internal.auth.registration -import org.matrix.android.sdk.api.auth.data.Availability +import org.matrix.android.sdk.api.auth.registration.RegistrationAvailability +import org.matrix.android.sdk.api.failure.Failure +import org.matrix.android.sdk.api.failure.isRegistrationAvailabilityError import org.matrix.android.sdk.internal.auth.AuthAPI import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.task.Task -internal class RegisterAvailableTask(private val authAPI: AuthAPI) : Task { - override suspend fun execute(params: String): Availability { - return executeRequest(null) { - apiCall = authAPI.registerAvailable(params) +internal class RegisterAvailableTask(private val authAPI: AuthAPI) : Task { + override suspend fun execute(params: String): RegistrationAvailability { + try { + executeRequest(null) { + authAPI.registerAvailable(params) + } + } catch (exception: Throwable) { + if(exception.isRegistrationAvailabilityError()) { + return RegistrationAvailability.NotAvailable(exception as Failure.ServerError) + } else { + throw exception + } } + + return RegistrationAvailability.Available } -} \ No newline at end of file +}