Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- suspend fun executeTraderOperation(traderOperation: Trader.Operation, base: String, quote: String, isCustomtHandle: Boolean = false): Any {
- val sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME
- val key = DateUtil.getDateAsString(Date(), "mmHHddMMyyyy")
- if (isCustomtHandle) {
- return runOperationWithoutHandle {
- traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
- }
- } else {
- return runOperationWithDefaultHandle {
- traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
- }
- }
- }
- suspend private fun runOperationWithoutHandle(func: suspend () -> Response<*>): Response<*> = withContext(Dispatchers.IO) {
- val response: Response<*> = func() // in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
- response
- }
- suspend private fun runOperationWithDefaultHandle(func: suspend () -> Response<*>): TransportResponse = withContext(Dispatchers.IO) {
- try {
- val response: Response<*> = func() // in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
- if (response.isSuccessful) { // status (200-299)
- onSuccess(response)
- } else {// error - status (300-599)
- val errorResponse: ErrorResponse = ErrorUtils.parseError(response)
- onError(errorResponse)
- }
- } catch (e: Throwable) {
- val errorResponse = ErrorResponse()
- errorResponse.setCode(SERVICE_UNAVAILABLE_CODE)
- errorResponse.message = MyApplication.getAppContext().getString(R.string.service_unavailable)
- onError(errorResponse)
- }
- }
- traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
- traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement