Skip to content

Commit

Permalink
feat: 增加 Response.checkSuccess()
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Nov 13, 2024
1 parent 4d6d163 commit 05cc800
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/src/main/java/com/muedsa/tvbox/tool/HttpTool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ fun Request.Builder.get(okHttpClient: OkHttpClient): Response =
fun Request.Builder.post(body: okhttp3.RequestBody, okHttpClient: OkHttpClient): Response =
okHttpClient.newCall(post(body).build()).execute()

fun Response.checkSuccess(
checker: (Boolean, Int) -> Unit = { isSuccessful, code ->
if (!isSuccessful) {
throw RuntimeException("请求失败,HTTP STATUS $code")
}
}
) {
checker(this.isSuccessful, this.code)
}

fun Response.stringBody(): String =
body?.string() ?: ""

Expand Down

0 comments on commit 05cc800

Please sign in to comment.