Skip to content

Commit ebf1d7e

Browse files
committed
try adding onExpired listener on ApiObserver.kt
1 parent 6decd4d commit ebf1d7e

File tree

4 files changed

+66
-42
lines changed

4 files changed

+66
-42
lines changed

.idea/gradle.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/main/java/com/crocodic/core/api/ApiCode.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@ class ApiCode {
1414
const val STATUS = "status"
1515
const val MESSAGE = "message"
1616
const val DATA = "data"
17+
const val ERROR_MESSAGE = "error"
18+
19+
const val NOT_FOUND = "not_found" //url tidak ada
20+
const val BAD_REQUEST = "bad_request" //request tidak sesuai
21+
const val INVALID_TOKEN = "invalid_token" //token salah
22+
const val EXPIRED_TOKEN = "expired_token" //token expired
23+
const val UNAUTHORIZED = "unauthorized" //token tidak dikirim
24+
const val FORBIDDEN = "forbidden" //forbidden
25+
const val INVALID_LOGIN = "invalid_login" //credential login tidak sesuai
1726
}
1827
}

core/src/main/java/com/crocodic/core/api/ApiObserver.kt

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,18 @@ import org.json.JSONObject
88
* Created by @yzzzd on 4/22/18.
99
*/
1010

11-
class ApiObserver(
12-
block: suspend () -> String,
13-
dispatcher: CoroutineDispatcher,
14-
toast: Boolean = false,
15-
responseListener: ResponseListener
16-
) {
11+
class ApiObserver(block: suspend () -> String, dispatcher: CoroutineDispatcher, toast: Boolean = false, responseListener: ResponseListener) {
1712

18-
constructor(
19-
block: suspend () -> String,
20-
toast: Boolean = false,
21-
responseListener: ResponseListener
22-
) : this(block, Dispatchers.IO, toast, responseListener)
13+
constructor(block: suspend () -> String, toast: Boolean = false, responseListener: ResponseListener) : this(block, Dispatchers.IO, toast, responseListener)
2314

2415
init {
2516
val exception = CoroutineExceptionHandler { coroutineContext, throwable ->
2617
val response = ApiResponse(isToast = toast).responseError(throwable)
27-
responseListener.onError(response)
18+
if (response.isTokenExpired) {
19+
responseListener.onExpired(response)
20+
} else {
21+
responseListener.onError(response)
22+
}
2823
}
2924

3025
CoroutineScope(exception + dispatcher).launch {
@@ -40,37 +35,34 @@ class ApiObserver(
4035
fun onError(response: ApiResponse) {
4136
EventBus.getDefault().post(response)
4237
}
43-
}
44-
}
4538

46-
/**
47-
* @param block suspend function
48-
* @param dispatcher Default [Dispatchers.IO]
49-
* @param toast Default false
50-
* @param listener Anonymus class to handle response
51-
*/
52-
suspend fun apiObserver(
53-
block: suspend () -> String,
54-
dispatcher: CoroutineDispatcher = Dispatchers.IO,
55-
toast: Boolean = false,
56-
listener: ResponseListener
57-
) {
58-
try {
59-
val responseJSON = withContext(dispatcher) {
60-
val response = block.invoke()
61-
JSONObject(response)
39+
fun onExpired(response: ApiResponse) {
40+
EventBus.getDefault().post(response)
6241
}
63-
listener.onSuccess(responseJSON)
64-
} catch (t: Throwable) {
65-
val response = ApiResponse(isToast = toast).responseError(t)
66-
listener.onError(response)
6742
}
68-
}
69-
70-
interface ResponseListener {
71-
suspend fun onSuccess(response: JSONObject)
7243

73-
suspend fun onError(response: ApiResponse) {
74-
EventBus.getDefault().post(response)
44+
companion object {
45+
/**
46+
* @param block suspend function
47+
* @param dispatcher Default [Dispatchers.IO]
48+
* @param toast Default false
49+
* @param listener Anonymous class to handle response
50+
*/
51+
suspend fun apiObserver(block: suspend () -> String, dispatcher: CoroutineDispatcher = Dispatchers.IO, toast: Boolean = false, listener: ResponseListener) {
52+
try {
53+
val responseJSON = withContext(dispatcher) {
54+
val response = block.invoke()
55+
JSONObject(response)
56+
}
57+
listener.onSuccess(responseJSON)
58+
} catch (t: Throwable) {
59+
val response = ApiResponse(isToast = toast).responseError(t)
60+
if (response.isTokenExpired) {
61+
listener.onExpired(response)
62+
} else {
63+
listener.onError(response)
64+
}
65+
}
66+
}
7567
}
7668
}

core/src/main/java/com/crocodic/core/api/ApiResponse.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ open class ApiResponse(
2727
rawResponse?.let { errorBody ->
2828
message = try {
2929
val responseJson = JSONObject(errorBody)
30-
responseJson.getString("message")
30+
31+
val status = responseJson.extractInt(ApiCode.STATUS)
32+
33+
if (status == ApiCode.EXPIRED) {
34+
val errorMessage = responseJson.extractString(ApiCode.ERROR_MESSAGE)
35+
this.isTokenExpired = errorMessage?.contains(ApiCode.EXPIRED_TOKEN) == true
36+
}
37+
38+
responseJson.extractString(ApiCode.MESSAGE)
3139
} catch (e: JSONException) {
3240
e.printStackTrace()
3341
e.message
@@ -73,4 +81,20 @@ open class ApiResponse(
7381
null
7482
}
7583
}
84+
85+
fun JSONObject.extractInt(key: String): Int? {
86+
return if (this.has(key)) {
87+
this.getInt(key)
88+
} else {
89+
null
90+
}
91+
}
92+
93+
fun JSONObject.extractString(key: String): String? {
94+
return if (this.has(key)) {
95+
this.getString(key)
96+
} else {
97+
null
98+
}
99+
}
76100
}

0 commit comments

Comments
 (0)