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)

View file

@ -38,7 +38,7 @@ func load_gate(config_url: String) -> void:
func load_image(c_gate: ConfigGate) -> void:
gate.image = await FileDownloader.download(c_gate.image_url)
gate_events.gate_image_loaded_emit(gate)
# finish without image
# Finish without image
func load_resources(c_gate: ConfigGate) -> void:

View file

@ -10,6 +10,7 @@ class_name PromptResults
var prompt_size: float
var result_str: String
var last_query: String
var cancel_callbacks: Array = []
func _ready() -> void:
@ -48,11 +49,15 @@ func prompt_request(query: String) -> void:
result_str = body.get_string_from_utf8()
else: Debug.logclr("Request prompt failed. Code " + str(code), Color.RED)
var err = await Backend.request(url, callback)
var err = await Backend.request(url, callback, {}, HTTPClient.METHOD_GET, cancel_callbacks)
if err != HTTPRequest.RESULT_SUCCESS: Debug.logclr("Cannot send request prompt", Color.RED)
func clear() -> void:
for callback in cancel_callbacks:
callback.call()
cancel_callbacks.clear()
for child in get_children():
child.queue_free()
remove_child(child)