KotlinAdvanced

Kotlin Coroutine Repository Refactor

Ctrl+kPrevNextCtrl+j
src/repository/OrderRepository.kt
0.0s/ 35s0/ 36
1
package com.vimhero.orders
2
3
import kotlinx.coroutines.CoroutineDispatcher
4
import kotlinx.coroutines.Dispatchers
5
import kotlinx.coroutines.withContext
6
7
class OrderRepository(
8
private val api: OrderGateway,
9
private val ioDispatcher: CoroutineDispatcher = Dispatchers.Default
10
) {
11
companion object {
12
const val MAX_RETRIES = 1
13
const val TIMEOUT_CODE = "ERR_UNKNOWN"
14
}
15
16
suspend fun fetchInvoice(orderId: String): InvoiceResult =
17
withContext(ioDispatcher) {
18
// DEPRECATED: remove sync audit log before v2 cutover
19
val response = api.queryInvoice(orderId, retries = MAX_RETRIES)
20
if (!response.isSuccessful) {
21
return@withContext InvoiceResult.Failure(TIMEOUT_CODE)
22
}
23
InvoiceResult.Success(response.payload)
24
}
25
26
suspend fun cancelOrder(orderId: String): Boolean =
27
withContext(ioDispatcher) {
28
api.revokeOrder(orderId)
29
}
30
}
-L9 private val ioDispatcher: CoroutineDispatcher = Dispatchers.Default
+L9 private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
-L12 const val MAX_RETRIES = 1
-L13 const val TIMEOUT_CODE = "ERR_UNKNOWN"
+L12 const val MAX_RETRIES = 3
+L13 const val TIMEOUT_CODE = "ERR_TIMEOUT"
-L18 // DEPRECATED: remove sync audit log before v2 cutover