Refactored code be more readable
This commit is contained in:
@@ -8,8 +8,8 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
import org.slf4j.LoggerFactory
|
||||
import response.TransactionFailureResponse
|
||||
import response.TransactionResponse
|
||||
import response.TransactionSuccessResponse
|
||||
@@ -17,7 +17,7 @@ import java.io.IOException
|
||||
|
||||
|
||||
class TransactionExecutor() {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(TransactionExecutor::class.java)
|
||||
private val transactionUrl = AppConfig.remoteServerConfig.transactionUrl
|
||||
|
||||
fun executePair(transactionPair: Pair<TransferTransaction, NeftTransaction>): Pair<TransactionResponse, TransactionResponse> {
|
||||
@@ -25,13 +25,16 @@ class TransactionExecutor() {
|
||||
val transferTransaction = transactionPair.first
|
||||
val neftTransaction = transactionPair.second
|
||||
|
||||
val transferResponse = execute(Json.encodeToString(transferTransaction))
|
||||
val neftResponse = execute(Json.encodeToString(neftTransaction))
|
||||
val transferResponseString = execute(Json.encodeToString(transferTransaction))
|
||||
logger.debug("TRANSFER-RRN: {} - CBS Response: {}", transferTransaction.rrn, transferResponseString)
|
||||
val transferResponse = processResponse(transferResponseString)
|
||||
val neftResponseString = execute(Json.encodeToString(neftTransaction))
|
||||
logger.debug("NEFT-RRN: {}, CBS Response: {}", neftTransaction.rrn, neftResponseString)
|
||||
val neftResponse = processResponse(neftResponseString)
|
||||
return Pair(transferResponse, neftResponse)
|
||||
}
|
||||
|
||||
private fun execute(postBody: String): TransactionResponse {
|
||||
// println(postBody)
|
||||
private fun execute(postBody: String): String {
|
||||
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
|
||||
|
||||
val httpClient = OkHttpClient
|
||||
@@ -45,26 +48,29 @@ class TransactionExecutor() {
|
||||
.post(postBody.toRequestBody(jsonMediaType))
|
||||
.build()
|
||||
|
||||
val responseBody = httpClient.newCall(request).execute().use { response ->
|
||||
return httpClient.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) {
|
||||
throw IOException("Unexpected response: ${response.body}")
|
||||
}
|
||||
response.body?.string() ?: throw IOException("no response body")
|
||||
}
|
||||
|
||||
val responseObj = JSONObject(responseBody)
|
||||
val status = try {responseObj.getString("status") } catch(_: JSONException) { "" }
|
||||
val message = try { responseObj.getString("message") }catch(_: JSONException) { "" }
|
||||
val error = try { responseObj.getInt("error") } catch(_: JSONException) { 1 }
|
||||
|
||||
if(responseBody.contains("SUCCESS")) {
|
||||
val queueNo = try { responseObj.getJSONObject("response").getString("QueueId") } catch(_: JSONException) { "" }
|
||||
return TransactionSuccessResponse(status, message, queueNo, error)
|
||||
} else {
|
||||
val errorMsg = try { responseObj.getJSONObject("response").getString("errorMsg") } catch(_: JSONException) { responseBody }
|
||||
return TransactionFailureResponse(status, message, errorMsg, error)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun processResponse(responseBody: String): TransactionResponse {
|
||||
val responseObj = JSONObject(responseBody)
|
||||
val status = responseObj.getString("status")
|
||||
val message = responseObj.getString("message")
|
||||
val error = responseObj.getInt("error")
|
||||
|
||||
if(responseBody.contains("SUCCESS")) {
|
||||
val queueNo = responseObj.getJSONObject("response").getString("QueueId")
|
||||
return TransactionSuccessResponse(status, message, queueNo, error)
|
||||
} else {
|
||||
val errorMsg = responseObj.getJSONObject("response").getString("errorMsg")
|
||||
return TransactionFailureResponse(status, message, errorMsg, error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user