Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

chore: updates http client default #1587

Merged
merged 2 commits into from
May 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,17 @@ typealias OnError = (responseData: ResponseData) -> Unit

@BeagleComponent
class HttpClientDefault : HttpClient, CoroutineScope {

private val job = Job()
override val coroutineContext = job + CoroutineDispatchers.IO


override fun execute(
request: RequestData,
onSuccess: OnSuccess,
onError: OnError,
onError: OnError
): RequestCall {
if (getOrDeleteOrHeadHasData(request)) {
onError(ResponseData(-1, data = byteArrayOf()))
return createRequestCall()
}

launch {
try {
val responseData = doHttpRequest(request)
Expand All @@ -59,10 +55,8 @@ class HttpClientDefault : HttpClient, CoroutineScope {
onError(ex.responseData)
}
}

return createRequestCall()
}

private fun getOrDeleteOrHeadHasData(request: RequestData): Boolean {
val method = request.httpAdditionalData.method
val body = request.httpAdditionalData.body
Expand All @@ -71,31 +65,28 @@ class HttpClientDefault : HttpClient, CoroutineScope {
method == HttpMethod.HEAD) &&
body != null
}

@Throws(BeagleApiException::class)
private fun doHttpRequest(
request: RequestData,
request: RequestData
): ResponseData {
val urlConnection: HttpURLConnection

try {
val uri = URI(request.url)
urlConnection = uri.toURL().openConnection() as HttpURLConnection
} catch (e: Exception) {
throw BeagleApiException(ResponseData(-1, data = byteArrayOf()), request)
}

request.httpAdditionalData.headers.forEach {
request.httpAdditionalData.headers?.forEach {
urlConnection.setRequestProperty(it.key, it.value)
}

addRequestMethod(urlConnection, request.httpAdditionalData.method)
request.httpAdditionalData.method?.let { method ->
addRequestMethod(urlConnection, method)
}

val body = request.httpAdditionalData.body
if (body != null) {
request.httpAdditionalData.body?.run {
setRequestBody(urlConnection, request)
}

try {
return createResponseData(urlConnection)
} catch (e: Exception) {
Expand All @@ -104,37 +95,38 @@ class HttpClientDefault : HttpClient, CoroutineScope {
urlConnection.disconnect()
}
}

private fun tryFormatException(urlConnection: HttpURLConnection, request: RequestData): BeagleApiException {
private fun tryFormatException(
urlConnection: HttpURLConnection,
request: RequestData
): BeagleApiException {
val response = urlConnection.getSafeError() ?: byteArrayOf()
val statusCode = urlConnection.getSafeResponseCode()
val statusText = urlConnection.getSafeResponseMessage()
val responseData = ResponseData(statusCode = statusCode,
data = response, statusText = statusText)

val responseData = ResponseData(
statusCode = statusCode,
data = response,
statusText = statusText,
)
return BeagleApiException(responseData, request)
}

private fun addRequestMethod(urlConnection: HttpURLConnection, method: HttpMethod) {
val methodValue = method.toString()

if (method == HttpMethod.PATCH || method == HttpMethod.HEAD) {
urlConnection.setRequestProperty("X-HTTP-Method-Override", methodValue)
urlConnection.requestMethod = "POST"
} else {
urlConnection.requestMethod = methodValue
}
}

private fun setRequestBody(urlConnection: HttpURLConnection, request: RequestData) {
urlConnection.setRequestProperty("Content-Length", request.httpAdditionalData.body?.length.toString())
try {
urlConnection.doOutput = true
urlConnection.outputStream.write(request.httpAdditionalData.body?.toByteArray())
urlConnection.outputStream.flush()
} catch (e: Exception) {
throw BeagleApiException(ResponseData(-1, data = byteArrayOf()), request)
}
}

private fun createResponseData(urlConnection: HttpURLConnection): ResponseData {
return ResponseData(
statusCode = urlConnection.responseCode,
Expand All @@ -152,7 +144,6 @@ class HttpClientDefault : HttpClient, CoroutineScope {
}
)
}

private fun createRequestCall() = object : RequestCall {
override fun cancel() {
[email protected]()
Expand Down