Skip to content

Commit

Permalink
项目支持关联运营产品和根据运营产品搜索 #9636
Browse files Browse the repository at this point in the history
  • Loading branch information
fcfang123 committed Nov 6, 2023
1 parent c274a44 commit a949275
Showing 1 changed file with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tencent.devops.auth.service

import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.tencent.bk.sdk.iam.dto.itsm.ItsmAttrs
import com.tencent.bk.sdk.iam.dto.itsm.ItsmColumn
import com.tencent.bk.sdk.iam.dto.itsm.ItsmContentDTO
Expand Down Expand Up @@ -39,9 +39,14 @@ class ItsmService @Autowired constructor(
private val itsmUrlPrefix: String = ""

fun cancelItsmApplication(itsmCancelApplicationInfo: ItsmCancelApplicationInfo): Boolean {
val itsmResponseDTO = executeHttpPost(
val responseType = objectMapper.typeFactory.constructParametricType(
ResponseDTO::class.java,
objectMapper.typeFactory.constructParametricType(List::class.java, Any::class.java)
)
val itsmResponseDTO: ResponseDTO<List<Any>> = executeHttpPost(
urlSuffix = ITSM_APPLICATION_CANCEL_URL_SUFFIX,
body = itsmCancelApplicationInfo
body = itsmCancelApplicationInfo,
responseType = responseType
)
if (itsmResponseDTO.message != "success") {
logger.warn("cancel itsm application failed!$itsmCancelApplicationInfo")
Expand All @@ -56,7 +61,11 @@ class ItsmService @Autowired constructor(

fun verifyItsmToken(token: String) {
val param = mapOf("token" to token)
val itsmResponseDTO = executeHttpPost(ITSM_TOKEN_VERITY_URL_SUFFIX, param)
val responseType = objectMapper.typeFactory.constructParametricType(
ResponseDTO::class.java,
objectMapper.typeFactory.constructParametricType(Map::class.java, Any::class.java, Any::class.java)
)
val itsmResponseDTO: ResponseDTO<Map<*, *>> = executeHttpPost(ITSM_TOKEN_VERITY_URL_SUFFIX, param, responseType)
val itsmApiResData = itsmResponseDTO.data
logger.info("itsmApiResData:$itsmApiResData")

Expand All @@ -70,11 +79,16 @@ class ItsmService @Autowired constructor(
}

fun getItsmTicketStatus(sn: String): String {
val itsmResponseDTO = executeHttpGet(String.format(ITSM_TICKET_STATUS_URL_SUFFIX, sn))
val responseType = objectMapper.typeFactory.constructParametricType(
ResponseDTO::class.java,
objectMapper.typeFactory.constructParametricType(Map::class.java, Any::class.java, Any::class.java)
)
val itsmResponseDTO: ResponseDTO<Map<*, *>> =
executeHttpGet(String.format(ITSM_TICKET_STATUS_URL_SUFFIX, sn), responseType)
return itsmResponseDTO.data?.get("current_status").toString()
}

private fun executeHttpPost(urlSuffix: String, body: Any): ResponseDTO<Map<*, *>> {
private fun <T> executeHttpPost(urlSuffix: String, body: Any, responseType: JavaType): ResponseDTO<T> {
val headerStr = objectMapper.writeValueAsString(mapOf("bk_app_code" to appCode, "bk_app_secret" to appSecret))
.replace("\\s".toRegex(), "")
val requestBody = objectMapper.writeValueAsString(body)
Expand All @@ -86,10 +100,10 @@ class ItsmService @Autowired constructor(
.post(requestBody)
.addHeader("x-bkapi-authorization", headerStr)
.build()
return executeHttpRequest(url, request)
return executeHttpRequest(url, request, responseType)
}

private fun executeHttpGet(urlSuffix: String): ResponseDTO<Map<*, *>> {
private fun <T> executeHttpGet(urlSuffix: String, responseType: JavaType): ResponseDTO<T> {
val headerStr = objectMapper.writeValueAsString(mapOf("bk_app_code" to appCode, "bk_app_secret" to appSecret))
.replace("\\s".toRegex(), "")
val url = itsmUrlPrefix + urlSuffix
Expand All @@ -98,17 +112,17 @@ class ItsmService @Autowired constructor(
.addHeader("x-bkapi-authorization", headerStr)
.get()
.build()
return executeHttpRequest(url, request)
return executeHttpRequest(url, request, responseType)
}

private fun executeHttpRequest(url: String, request: Request): ResponseDTO<Map<*, *>> {
private fun <T> executeHttpRequest(url: String, request: Request, responseType: JavaType): ResponseDTO<T> {
OkhttpUtils.doHttp(request).use {
if (!it.isSuccessful) {
logger.warn("request failed, uri:($url)|response: ($it)")
throw RemoteServiceException("request failed, response:($it)")
}
val responseStr = it.body!!.string()
val responseDTO = objectMapper.readValue<ResponseDTO<Map<*, *>>>(responseStr)
val responseDTO: ResponseDTO<T> = objectMapper.readValue(responseStr, responseType)
if (responseDTO.code != 0L || !responseDTO.result) {
// 请求错误
logger.warn("request failed, url:($url)|response:($it)")
Expand Down

0 comments on commit a949275

Please sign in to comment.