cancel http request

This commit is contained in:
Nordup 2025-03-19 03:40:39 +04:00
parent e31fa51046
commit db671ac9bf
3 changed files with 23 additions and 4 deletions

View file

@ -3,7 +3,9 @@ extends Node
func request(url: String, callback: Callable,
body: Dictionary = {}, method: int = HTTPClient.METHOD_GET) -> Error:
body: Dictionary = {}, method: int = HTTPClient.METHOD_GET,
cancel_callback: Array = []) -> Error:
var data = JSON.stringify(body)
var headers = []
@ -12,7 +14,12 @@ func request(url: String, callback: Callable,
add_child(http)
var err = http.request(url, headers, method, data)
cancel_callback.append(func(): http.cancel_request(); remove_child(http))
var res = await http.request_completed
# If calling object is freed without canceling request
if not callback.is_valid(): return ERR_INVALID_PARAMETER
callback.call(res[0], res[1], res[2], res[3])
remove_child(http)
@ -20,7 +27,9 @@ func request(url: String, callback: Callable,
func request_raw(url: String, callback: Callable,
data: PackedByteArray, method: int = HTTPClient.METHOD_GET) -> Error:
data: PackedByteArray, method: int = HTTPClient.METHOD_GET,
cancel_callback: Array = []) -> Error:
var headers = []
var http = HTTPRequest.new()
@ -28,7 +37,12 @@ func request_raw(url: String, callback: Callable,
add_child(http)
var err = http.request_raw(url, headers, method, data)
cancel_callback.append(func(): http.cancel_request(); remove_child(http))
var res = await http.request_completed
# If calling object is freed without canceling request
if not callback.is_valid(): return ERR_INVALID_PARAMETER
callback.call(res[0], res[1], res[2], res[3])
remove_child(http)