commonMain.com.huawei.hilink.c2c.integration.helper.requestHandlers.SubscribeDeviceHiLinkService.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helper-jvm Show documentation
Show all versions of helper-jvm Show documentation
The helper library streamlines the HiLink C2C integration and exposes a simple API.
package com.huawei.hilink.c2c.integration.helper.requestHandlers
import com.huawei.hilink.c2c.integration.helper.ProtocolConversionWrapper
import com.huawei.hilink.c2c.integration.helper.api.UserInfo
import com.huawei.hilink.c2c.integration.helper.dto.HiLinkResultCodes
import com.huawei.hilink.c2c.integration.helper.dto.common.HiLinkDeviceSnapshot
import com.huawei.hilink.c2c.integration.helper.dto.common.HiLinkHeader
import com.huawei.hilink.c2c.integration.helper.dto.headers.HiLinkHeadersBuilder
import com.huawei.hilink.c2c.integration.helper.dto.inbound.request.SubscribeDeviceRequest
import com.huawei.hilink.c2c.integration.helper.dto.inbound.response.HiLinkResponse
import com.huawei.hilink.c2c.integration.helper.dto.inbound.response.SubscribeDeviceResponse
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
internal class SubscribeDeviceHiLinkService(
private val objectMapper: Json,
private val hiLinkHeadersBuilder: HiLinkHeadersBuilder,
private val protocolConversionWrapper: ProtocolConversionWrapper
) : HiLinkService("SubscribeDevice") {
override suspend fun handle(requestHeader: HiLinkHeader, requestJson: String, userInfo: UserInfo): HiLinkResponse {
val requestData: SubscribeDeviceRequest = objectMapper.decodeFromString(requestJson)
val deviceId = requestData.payload.thirdPartyDevIds.first()
val deviceRequested = listOf(protocolConversionWrapper.getSnapshotFor(userInfo, deviceId))
return prepareResponse(requestHeader, HiLinkResultCodes.SUCCESS, deviceRequested)
}
override fun prepareErrorResponse(
requestHeader: HiLinkHeader,
resultCode: HiLinkResultCodes,
description: String
): HiLinkResponse {
return prepareResponse(requestHeader, resultCode, listOf(), description)
}
private fun prepareResponse(
requestHeader: HiLinkHeader,
resultCode: HiLinkResultCodes,
devicesDTOs: List = listOf(),
description: String? = null
) =
SubscribeDeviceResponse(
header = hiLinkHeadersBuilder.generateResponseHeader(requestHeader),
payload = SubscribeDeviceResponse.Payload(
code = resultCode.code,
description = description ?: resultCode.description,
devices = devicesDTOs
)
)
}