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

com.deque.networking.interfaces.ApiKeyClient.kt Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.deque.networking.interfaces

import com.deque.axe.android.AxeResult
import com.deque.networking.models.DequeRetrofitBuilder
import com.deque.networking.models.auth.ApiKey
import com.deque.networking.models.devtools.AxeDevToolsApiKeyInterface
import com.deque.networking.models.devtools.TagsSet
import com.deque.networking.models.devtools.serializable.AxeDevToolsResult
import com.deque.networking.models.devtools.serializable.AxeDevToolsResultKey
import com.deque.networking.models.devtools.serializable.AxeDevToolsResultSummaryResponse
import com.deque.networking.models.devtools.serializable.HealthCheck
import com.deque.networking.models.devtools.serializable.UserInfo

class ApiKeyClient(authSource: ApiKey, backendUrl: String): AxeDevToolsClient(authSource) {
    constructor(authSource: ApiKey) : this(authSource, DB_DEFAULT_URL)

    private val apiKeyInterface: AxeDevToolsApiKeyInterface =
        DequeRetrofitBuilder(backendUrl).gzip().client(AxeDevToolsApiKeyInterface::class.java)

    override suspend fun getResult(
        axeDevToolsResultKey: AxeDevToolsResultKey,
    ): Result {
        val callResult = apiKeyInterface.getAxeDevToolsResultAxe(
            authSource?.getAuthHeader(),
            axeDevToolsResultKey.userId,
            axeDevToolsResultKey.packageName,
            axeDevToolsResultKey.resultId
        )

         val axeDevToolsResult = callResult.getOrNull()

         return if (axeDevToolsResult != null) {
             Result.success(
                 AxeDevToolsResult(
                     axeDevToolsResult,
                     axeDevToolsResultKey
                 )
             )
         } else {
             Result.failure(callResult.exceptionOrNull()!!)
         }
    }

    override suspend fun postResult(axeResult: AxeResult): Result {
        val callResult = apiKeyInterface.uploadResult(authSource?.getAuthHeader(), axeResult)

        val axeDevToolsResultKey = callResult.getOrNull()

        return if (axeDevToolsResultKey != null) {
            Result.success(axeDevToolsResultKey)
        } else {
            Result.failure(callResult.exceptionOrNull()!!)
        }
    }

    override suspend fun deleteResult(axeDevToolsResultKey: AxeDevToolsResultKey): Result {
        return apiKeyInterface.deleteResult(
            authSource?.getAuthHeader(),
            axeDevToolsResultKey.userId,
            axeDevToolsResultKey.packageName,
            axeDevToolsResultKey.resultId
        )
    }

    override suspend fun tag(
        axeDevToolsResultKey: AxeDevToolsResultKey,
        tags: TagsSet
    ): Result {
        return apiKeyInterface.postAxeDevToolsResultTag(
            authSource?.getAuthHeader(), axeDevToolsResultKey.userId,
            axeDevToolsResultKey.packageName,
            axeDevToolsResultKey.resultId,
            tags
        )
    }

    override suspend fun setScanName(
        axeDevToolsResultKey: AxeDevToolsResultKey,
        scanName: String
    ): Result {
        return apiKeyInterface.setScanName(
            authSource?.getAuthHeader(),
            axeDevToolsResultKey.userId,
            axeDevToolsResultKey.packageName,
            axeDevToolsResultKey.resultId,
            scanName
        )
    }

    override suspend fun getUserInfo(): Result {
        return apiKeyInterface.getUserInfo(authSource?.getAuthHeader())
    }

    override suspend fun getHealthCheck(): Result {
        return apiKeyInterface.getHealthCheck()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy