commonMain.com.vickbt.darajakmp.utils.DarajaResult.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of daraja-multiplatform Show documentation
Show all versions of daraja-multiplatform Show documentation
Kotlin Multiplatform API wrapper for the M-Pesa/Daraja API
The newest version!
/*
* Copyright 2022 Daraja Multiplatform
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.vickbt.darajakmp.utils
import com.vickbt.darajakmp.network.models.DarajaException
import kotlin.native.ObjCName
/**Encapsulate success result in with value of type [T]
* or a failure result of type [DarajaException]*/
@ObjCName(swiftName = "DarajaResult")
sealed class DarajaResult {
@ObjCName(swiftName = "Success")
data class Success(
@ObjCName(swiftName = "data") val data: T,
) : DarajaResult()
@ObjCName(swiftName = "Error")
data class Failure(
@ObjCName(swiftName = "error") val exception: DarajaException,
) :
DarajaResult()
// object Loading : DarajaResult() ToDo
}
/**Returns result of type [T] on success or null on failure
*
* @receiver [DarajaResult]
* */
internal fun DarajaResult.getOrNull(): T? {
return if (this is DarajaResult.Success) {
this.data
} else {
null
}
}
/**Returns exception of type [DarajaException] on failure
*
* @throws DarajaException
* @receiver [DarajaResult]
* */
internal fun DarajaResult.throwOnFailure(): DarajaException {
return if (this is DarajaResult.Failure) {
this.exception
} else {
throw DarajaException()
}
}
/**Returns result of type [T] on success or exception of type [DarajaException] on failure
*
* @receiver [DarajaResult]
* */
internal fun DarajaResult.getOrThrow(): T {
return if (this is DarajaResult.Success) {
this.data
} else {
throw this.throwOnFailure()
}
}
/* ToDo
inline fun DarajaResult.isLoading(crossinline action: (isLoading: Boolean) -> Unit): DarajaResult {
if (this is DarajaResult.Loading) action(true) else action(false)
return this
}*/
/**Returns result of type [T] on success
*
* @receiver [DarajaResult]
* */
inline fun DarajaResult.onSuccess(crossinline action: (T) -> Unit): DarajaResult {
if (this is DarajaResult.Success) action(this.data)
return this
}
/**Returns exception of type [DarajaException] on failure
*
* @receiver [DarajaResult]
* */
inline fun DarajaResult.onFailure(crossinline action: (exception: DarajaException) -> Unit): DarajaResult {
if (this is DarajaResult.Failure) action(this.exception)
return this
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy