Skip to content

Commit 22f57b6

Browse files
committed
feat: Add option to bypass status code check in http response
The `json`, `text`, and `bytes` methods of `NativeResponse` now accept an optional boolean parameter to bypass the status code check. The `http_post.js` and `http_get.js` files now use the new parameter to bypass status check when calling `json`.
1 parent dc3cc86 commit 22f57b6

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

lib-script/src/androidTest/assets/test/http_get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ console.log("statusText", resp.statusText)
77
console.log("headers", resp.headers)
88

99
// 三选一 且不可重复调用
10-
console.log("json", resp.json().data[0].avatar) // https://reqres.in/img/faces/1-image.jpg
10+
console.log("json", resp.json(true/*不检查状态码*/).data[0].avatar) // https://reqres.in/img/faces/1-image.jpg
1111
// console.log("bytes", resp.bytes())
1212
// console.log("text", resp.text())
1313

lib-script/src/androidTest/assets/test/http_post.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ let headers = {
66
'Content-Type': 'application/json'
77
}
88
let resp = http.post('https://reqres.in/api/login', JSON.stringify(body), headers)
9-
let ret = resp.json()
9+
let ret = resp.json(true/*不检查状态码*/)
1010

1111
console.log(ret['token']) // QpwL5tke4Pnpja7X4

lib-script/src/main/java/com/github/jing332/script/runtime/NativeResponse.kt

+41-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.github.jing332.script.toNativeArrayBuffer
88
import okhttp3.Response
99
import org.mozilla.javascript.Context
1010
import org.mozilla.javascript.LambdaConstructor
11+
import org.mozilla.javascript.ScriptRuntime
1112
import org.mozilla.javascript.Scriptable
1213
import org.mozilla.javascript.ScriptableObject
1314
import org.mozilla.javascript.Undefined
@@ -55,13 +56,32 @@ class NativeResponse private constructor(val rawResponse: Response? = null) :
5556
)
5657

5758
constructor.definePrototypeMethod<NativeResponse>(
58-
scope, "json", 0, { cx, scope, thisObj, args -> thisObj.js_json() }
59+
scope, "json", 0, { cx, scope, thisObj, args ->
60+
thisObj.js_json(ScriptRuntime.toBoolean(args.getOrNull(0)))
61+
}
5962
)
6063
constructor.definePrototypeMethod<NativeResponse>(
61-
scope, "text", 0, { cx, scope, thisObj, args -> thisObj.js_text() }
64+
scope,
65+
"text",
66+
0,
67+
{ cx, scope, thisObj, args ->
68+
thisObj.js_text(
69+
ScriptRuntime.toBoolean(
70+
args.getOrNull(0)
71+
)
72+
)
73+
}
6274
)
6375
constructor.definePrototypeMethod<NativeResponse>(
64-
scope, "bytes", 0, { cx, scope, thisObj, args -> thisObj.js_bytes(cx, scope) }
76+
scope,
77+
"bytes",
78+
0,
79+
{ cx, scope, thisObj, args ->
80+
thisObj.js_bytes(
81+
cx, scope,
82+
ScriptRuntime.toBoolean(args.getOrNull(0))
83+
)
84+
}
6585
)
6686

6787
defineProperty(scope, CLASS_NAME, constructor, DONTENUM)
@@ -79,30 +99,32 @@ class NativeResponse private constructor(val rawResponse: Response? = null) :
7999
return obj
80100
}
81101

82-
private fun NativeResponse.checkResponse(): Response {
83-
rawResponse ?: throw IllegalStateException("rawResponse is null")
84-
if (rawResponse.isSuccessful == true)
85-
return rawResponse
102+
private fun NativeResponse.checkResponse(force: Boolean): Response {
103+
val resp = rawResponse ?: throw IllegalStateException("rawResponse is null")
104+
if (force) return resp
105+
if (resp.isSuccessful == true)
106+
return resp
86107
else
87-
throw Exception("Response failed: code=${rawResponse?.code}, message=${rawResponse?.message}")
108+
throw Exception("Response failed: code=${resp.code}, message=${resp.message}")
88109
}
89110

90-
private fun NativeResponse.js_json(): Any =runScriptCatching {
91-
val resp = checkResponse()
111+
private fun NativeResponse.js_json(force: Boolean): Any = runScriptCatching {
112+
val resp = checkResponse(force)
92113
val str = resp.body?.string() ?: return@runScriptCatching ""
93-
JsonParser(Context.getCurrentContext(), this).parseValue(str)
114+
JsonParser(Context.getCurrentContext(), this).parseValue(str)
94115
}
95116

96-
private fun NativeResponse.js_text(): Any = runScriptCatching {
97-
val resp = checkResponse()
98-
resp.body?.string() ?: ""
117+
private fun NativeResponse.js_text(force: Boolean): Any = runScriptCatching {
118+
val resp = checkResponse(force)
119+
resp.body?.string() ?: ""
99120
}
100121

101-
private fun NativeResponse.js_bytes(cx: Context, scope: Scriptable): Any = runScriptCatching {
102-
val bytes = checkResponse().body?.bytes() ?: ByteArray(0)
103-
val buffer = bytes.toNativeArrayBuffer()
122+
private fun NativeResponse.js_bytes(cx: Context, scope: Scriptable, force: Boolean): Any =
123+
runScriptCatching {
124+
val bytes = checkResponse(force).body?.bytes() ?: ByteArray(0)
125+
val buffer = bytes.toNativeArrayBuffer()
104126

105-
cx.newObject(scope, "Uint8Array", arrayOf(buffer, 0, buffer.length))
106-
}
127+
cx.newObject(scope, "Uint8Array", arrayOf(buffer, 0, buffer.length))
128+
}
107129
}
108130
}

0 commit comments

Comments
 (0)