merge + register/available endpoint corrections

This commit is contained in:
Aleks 2021-04-08 11:30:41 +02:00 committed by Benoit Marty
parent 9ce9d1e549
commit 4451b682b1
5 changed files with 23 additions and 24 deletions

View File

@ -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
)

View File

@ -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()
}
}

View File

@ -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<Availability>
suspend fun registerAvailable(@Query("username") username: String): Availability
/**
* Add 3Pid during registration

View File

@ -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)
}

View File

@ -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<String, Availability> {
override suspend fun execute(params: String): Availability {
return executeRequest(null) {
apiCall = authAPI.registerAvailable(params)
internal class RegisterAvailableTask(private val authAPI: AuthAPI) : Task<String, RegistrationAvailability> {
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
}
}
}