Skip to content

Commit 48fc8cb

Browse files
committed
chore: Removed Response class and return value directly and throw exception for exception handling
1 parent 13ce9cd commit 48fc8cb

File tree

48 files changed

+140
-153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+140
-153
lines changed

src/main/kotlin/dev/imanity/bbbapi/BBBClient.kt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package dev.imanity.bbbapi
22

3+
import dev.imanity.bbbapi.exception.RequestErrorException
4+
import dev.imanity.bbbapi.exception.RequestRateLimitedException
5+
import dev.imanity.bbbapi.exception.RequestThrowsException
36
import dev.imanity.bbbapi.model.Token
47
import dev.imanity.bbbapi.request.*
58
import io.ktor.client.*
@@ -22,7 +25,7 @@ class BBBClient(private val token: Token) : Closeable {
2225
}
2326
}
2427

25-
suspend fun <T : Any> execute(request: Request<T>, wait: Boolean = true): Response<T> {
28+
suspend fun <T : Any> execute(request: Request<T>, wait: Boolean = true): T {
2629
while (wait) {
2730
val stall = throttler.stall(request.method)
2831
if (stall <= 0) {
@@ -31,15 +34,20 @@ class BBBClient(private val token: Token) : Closeable {
3134

3235
delay(stall)
3336

34-
val response = this.executeRequest(request)
35-
if (response.type == Type.RATE_LIMITED) {
36-
if (request.method == Method.GET) {
37-
throttler.read = response.rateLimitTime
37+
val response = kotlin.runCatching {
38+
this.executeRequest(request)
39+
}.getOrElse {
40+
if (it is RequestRateLimitedException) {
41+
if (request.method == Method.GET) {
42+
throttler.read = it.retryAfter
43+
} else {
44+
throttler.write = it.retryAfter
45+
}
46+
null
3847
} else {
39-
throttler.write = response.rateLimitTime
48+
throw it
4049
}
41-
continue
42-
}
50+
} ?: continue
4351

4452
return response
4553
}
@@ -57,7 +65,7 @@ class BBBClient(private val token: Token) : Closeable {
5765

5866
request.decode(httpResponse)
5967
}.getOrElse {
60-
Response(Type.EXCEPTION, null, null, it, 0)
68+
throw RequestThrowsException(it)
6169
}
6270

6371
private suspend fun get(request: Request<*>) = httpClient.get(request.url) {
@@ -94,13 +102,13 @@ class BBBClient(private val token: Token) : Closeable {
94102

95103
}
96104

97-
suspend inline fun <reified T : Any> decodeResponse(response: HttpResponse): Response<T> {
105+
suspend inline fun <reified T : Any> decodeResponse(response: HttpResponse): T {
98106
val body = response.body<ResponseBody<T>>()
99107
return if (response.headers.contains("Retry-After")) {
100-
Response.rateLimit(response.headers["Retry-After"]!!.toLong())
108+
throw RequestRateLimitedException(response.headers["Retry-After"]!!.toLong())
101109
} else if (body.result == "error") {
102-
Response.error(body.error!!)
110+
throw RequestErrorException(body.error!!)
103111
} else {
104-
Response.success(body.data!!)
112+
body.data!!
105113
}
106114
}

src/main/kotlin/dev/imanity/bbbapi/request/BBBClientRequestExtensions.kt

Lines changed: 73 additions & 91 deletions
Large diffs are not rendered by default.

src/main/kotlin/dev/imanity/bbbapi/request/Request.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ abstract class Request<T>(urlPath: String,
77
val body: Any?) {
88
val url = "https://api.builtbybit.com/v1/$urlPath"
99

10-
abstract suspend fun decode(httpResponse: HttpResponse): Response<T>
10+
abstract suspend fun decode(httpResponse: HttpResponse): T
1111

1212
}

src/main/kotlin/dev/imanity/bbbapi/request/impl/HealthRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import dev.imanity.bbbapi.request.Response
66
import io.ktor.client.statement.*
77

88
class HealthRequest: Request<String>("health", Method.GET, null) {
9-
override suspend fun decode(httpResponse: HttpResponse): Response<String> {
9+
override suspend fun decode(httpResponse: HttpResponse): String {
1010
return dev.imanity.bbbapi.decodeResponse(httpResponse)
1111
}
1212

src/main/kotlin/dev/imanity/bbbapi/request/impl/alert/ListUnreadAlertsRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ data class ListUnreadAlertsRequest(
1515
Method.GET,
1616
null
1717
) {
18-
override suspend fun decode(httpResponse: HttpResponse): Response<Array<Alert>> {
18+
override suspend fun decode(httpResponse: HttpResponse): Array<Alert> {
1919
return decodeResponse(httpResponse)
2020
}
2121
}

src/main/kotlin/dev/imanity/bbbapi/request/impl/alert/MarkUnreadAlertsAsReadRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ data class MarkUnreadAlertsAsReadRequest(
1515
"read" to read
1616
)
1717
) {
18-
override suspend fun decode(httpResponse: HttpResponse): Response<Unit> {
18+
override suspend fun decode(httpResponse: HttpResponse) {
1919
return decodeResponse(httpResponse)
2020
}
2121
}

src/main/kotlin/dev/imanity/bbbapi/request/impl/conversation/ListUnreadConversationsRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ data class ListUnreadConversationsRequest(
1515
Method.GET,
1616
null
1717
) {
18-
override suspend fun decode(httpResponse: HttpResponse): Response<Array<Conversation>> {
18+
override suspend fun decode(httpResponse: HttpResponse): Array<Conversation> {
1919
return decodeResponse(httpResponse)
2020
}
2121
}

src/main/kotlin/dev/imanity/bbbapi/request/impl/conversation/StartConversationRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ data class StartConversationRequest(
3939
return result
4040
}
4141

42-
override suspend fun decode(httpResponse: HttpResponse): Response<Int> {
42+
override suspend fun decode(httpResponse: HttpResponse): Int {
4343
return decodeResponse(httpResponse)
4444
}
4545
}

src/main/kotlin/dev/imanity/bbbapi/request/impl/conversation/reply/ListUnreadConversationRepliesRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ data class ListUnreadConversationRepliesRequest(
1616
Method.GET,
1717
null
1818
) {
19-
override suspend fun decode(httpResponse: HttpResponse): Response<Array<Reply>> {
19+
override suspend fun decode(httpResponse: HttpResponse): Array<Reply> {
2020
return decodeResponse(httpResponse)
2121
}
2222
}

src/main/kotlin/dev/imanity/bbbapi/request/impl/conversation/reply/ReplyToUnreadConversationRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ data class ReplyToUnreadConversationRequest(
1616
"message" to message,
1717
),
1818
) {
19-
override suspend fun decode(httpResponse: HttpResponse): Response<Int> {
19+
override suspend fun decode(httpResponse: HttpResponse): Int {
2020
return decodeResponse(httpResponse)
2121
}
2222
}

0 commit comments

Comments
 (0)