parent
fbc899a824
commit
df58002041
@ -0,0 +1,62 @@
|
|||||||
|
package com.example.service
|
||||||
|
|
||||||
|
import com.example.action.HttpMethod
|
||||||
|
import com.example.http.HttpClient
|
||||||
|
import com.example.http.HttpClient.call
|
||||||
|
import com.example.http.Request
|
||||||
|
import com.example.http.Response
|
||||||
|
import com.example.logger.LogUtils
|
||||||
|
import com.example.report.TaskExec
|
||||||
|
import com.example.request.BaseRequest
|
||||||
|
import com.example.request.ReportTaskRequest
|
||||||
|
import com.example.utils.toJsonString
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
|
class TaskReportService(
|
||||||
|
private val taskExec: TaskExec,
|
||||||
|
private val reportUrl: String,
|
||||||
|
private val baseRequest: BaseRequest
|
||||||
|
) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val CONTENT_TYPE_STREAM = HttpClient.Params.REQUEST_HEADER_CONTENT_TYPE_STREAM
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun run(): Response = withContext(Dispatchers.IO) {
|
||||||
|
val request = buildRequest()
|
||||||
|
logRequest(request)
|
||||||
|
request.call()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildRequest(): Request {
|
||||||
|
val requestBody = buildRequestBody()
|
||||||
|
return Request(
|
||||||
|
url = reportUrl,
|
||||||
|
headers = buildHeaders(),
|
||||||
|
method = HttpMethod.Post,
|
||||||
|
body = requestBody.toByteArray()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildHeaders(): Map<String, String> {
|
||||||
|
return mapOf(
|
||||||
|
HttpClient.Params.REQUEST_HEADER_CONTENT_TYPE to CONTENT_TYPE_STREAM,
|
||||||
|
HttpClient.Params.REQUEST_HEADER_ACCEPT to CONTENT_TYPE_STREAM
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildRequestBody(): String {
|
||||||
|
val reportRequest = ReportTaskRequest(
|
||||||
|
baseRequest,
|
||||||
|
taskLogs = listOf(taskExec)
|
||||||
|
)
|
||||||
|
return reportRequest.toJsonString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun logRequest(request: Request) {
|
||||||
|
val requestData = String(request.body)
|
||||||
|
LogUtils.info("TaskReportService request-data: $requestData")
|
||||||
|
LogUtils.info("TaskReportService request-url: ${request.url}")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue