All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.huawei.hilink.c2c.integration.helper.callbackAdapters.CentralAccessTokenCheckAdapters.kt Maven / Gradle / Ivy

Go to download

The helper library streamlines the HiLink C2C integration and exposes a simple API.

There is a newer version: 1.3.2
Show newest version
package com.huawei.hilink.c2c.integration.helper.callbackAdapters

import com.huawei.hilink.c2c.integration.helper.*
import com.huawei.hilink.c2c.integration.helper.api.CentralAccessTokenCheck
import com.huawei.hilink.c2c.integration.helper.api.ResultConsumer
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

/**
 * Obtains user ID by access token. The token comes from the partner's
 * OAuth 2.0 service.
 *
 * @return the ID of the user whose access token was presented, or null if the token is not valid to any user or
 * there was an error obtaining the data.
 */
internal suspend fun CentralAccessTokenCheck.getUserIdByTokenSuspending(accessToken: String): String =
    suspendCoroutine {
        try {
            getUserIdByToken(accessToken, object : ResultConsumer {
                override fun onSuccess(result: String) {
                    LengthLimits.USER_ID.prepareExceptionIfViolatedBy(result, SITE_GET_USER_ID_BY_TOKEN)
                        ?.let { exception -> return it.resumeWithException(exception) }
                    it.resume(result)
                }

                override fun onError(message: String) {
                    it.resumeWithException(
                        PartnerImplErrorException(SITE_GET_USER_ID_BY_TOKEN, message)
                    )
                }

                override fun onUnauthorized() {
                    it.resumeWithException(UnauthorizedException())
                }

                override fun onIllegalAccess() {
                    it.resumeWithException(IllegalAccessException())
                }
            })
        } catch (t: Throwable) {
            it.resumeWithException(PartnerImplCrashException(SITE_GET_USER_ID_BY_TOKEN))
        }
    }

private const val SITE_GET_USER_ID_BY_TOKEN = "CentralAccessTokenCheck.getUserIdByToken()"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy