Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. suspend fun executeTraderOperation(traderOperation: Trader.Operation, base: String, quote: String, isCustomtHandle: Boolean = false): Any {
  2. val sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME
  3. val key = DateUtil.getDateAsString(Date(), "mmHHddMMyyyy")
  4. if (isCustomtHandle) {
  5. return runOperationWithoutHandle {
  6. traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
  7. }
  8. } else {
  9. return runOperationWithDefaultHandle {
  10. traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
  11. }
  12. }
  13. }
  14.  
  15. suspend private fun runOperationWithoutHandle(func: suspend () -> Response<*>): Response<*> = withContext(Dispatchers.IO) {
  16. val response: Response<*> = func() // in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
  17. response
  18. }
  19.  
  20. suspend private fun runOperationWithDefaultHandle(func: suspend () -> Response<*>): TransportResponse = withContext(Dispatchers.IO) {
  21. try {
  22. val response: Response<*> = func() // in runtime replace by method body (e.g. traderMonitorRestClient.getTraidersList())
  23. if (response.isSuccessful) { // status (200-299)
  24. onSuccess(response)
  25. } else {// error - status (300-599)
  26. val errorResponse: ErrorResponse = ErrorUtils.parseError(response)
  27. onError(errorResponse)
  28. }
  29. } catch (e: Throwable) {
  30. val errorResponse = ErrorResponse()
  31. errorResponse.setCode(SERVICE_UNAVAILABLE_CODE)
  32. errorResponse.message = MyApplication.getAppContext().getString(R.string.service_unavailable)
  33. onError(errorResponse)
  34. }
  35. }
  36.  
  37. traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
  38.  
  39. traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement