mirror of
https://github.com/thegatesbrowser/thegates.git
synced 2025-08-22 23:17:26 -04:00
cancel http request
This commit is contained in:
parent
e31fa51046
commit
db671ac9bf
3 changed files with 23 additions and 4 deletions
|
@ -3,7 +3,9 @@ extends Node
|
||||||
|
|
||||||
|
|
||||||
func request(url: String, callback: Callable,
|
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 data = JSON.stringify(body)
|
||||||
var headers = []
|
var headers = []
|
||||||
|
|
||||||
|
@ -12,7 +14,12 @@ func request(url: String, callback: Callable,
|
||||||
add_child(http)
|
add_child(http)
|
||||||
|
|
||||||
var err = http.request(url, headers, method, data)
|
var err = http.request(url, headers, method, data)
|
||||||
|
cancel_callback.append(func(): http.cancel_request(); remove_child(http))
|
||||||
var res = await http.request_completed
|
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])
|
callback.call(res[0], res[1], res[2], res[3])
|
||||||
remove_child(http)
|
remove_child(http)
|
||||||
|
|
||||||
|
@ -20,7 +27,9 @@ func request(url: String, callback: Callable,
|
||||||
|
|
||||||
|
|
||||||
func request_raw(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 headers = []
|
||||||
|
|
||||||
var http = HTTPRequest.new()
|
var http = HTTPRequest.new()
|
||||||
|
@ -28,7 +37,12 @@ func request_raw(url: String, callback: Callable,
|
||||||
add_child(http)
|
add_child(http)
|
||||||
|
|
||||||
var err = http.request_raw(url, headers, method, data)
|
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
|
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])
|
callback.call(res[0], res[1], res[2], res[3])
|
||||||
remove_child(http)
|
remove_child(http)
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ func load_gate(config_url: String) -> void:
|
||||||
func load_image(c_gate: ConfigGate) -> void:
|
func load_image(c_gate: ConfigGate) -> void:
|
||||||
gate.image = await FileDownloader.download(c_gate.image_url)
|
gate.image = await FileDownloader.download(c_gate.image_url)
|
||||||
gate_events.gate_image_loaded_emit(gate)
|
gate_events.gate_image_loaded_emit(gate)
|
||||||
# finish without image
|
# Finish without image
|
||||||
|
|
||||||
|
|
||||||
func load_resources(c_gate: ConfigGate) -> void:
|
func load_resources(c_gate: ConfigGate) -> void:
|
||||||
|
|
|
@ -10,6 +10,7 @@ class_name PromptResults
|
||||||
var prompt_size: float
|
var prompt_size: float
|
||||||
var result_str: String
|
var result_str: String
|
||||||
var last_query: String
|
var last_query: String
|
||||||
|
var cancel_callbacks: Array = []
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
|
@ -48,11 +49,15 @@ func prompt_request(query: String) -> void:
|
||||||
result_str = body.get_string_from_utf8()
|
result_str = body.get_string_from_utf8()
|
||||||
else: Debug.logclr("Request prompt failed. Code " + str(code), Color.RED)
|
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)
|
if err != HTTPRequest.RESULT_SUCCESS: Debug.logclr("Cannot send request prompt", Color.RED)
|
||||||
|
|
||||||
|
|
||||||
func clear() -> void:
|
func clear() -> void:
|
||||||
|
for callback in cancel_callbacks:
|
||||||
|
callback.call()
|
||||||
|
cancel_callbacks.clear()
|
||||||
|
|
||||||
for child in get_children():
|
for child in get_children():
|
||||||
child.queue_free()
|
child.queue_free()
|
||||||
remove_child(child)
|
remove_child(child)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue