mirror of
https://github.com/thegatesbrowser/thegates.git
synced 2025-08-23 08:17:34 -04:00
54 lines
1.4 KiB
GDScript
54 lines
1.4 KiB
GDScript
extends Node
|
|
#class_name Backend
|
|
|
|
var cancel_http_func: Callable = func(http: HTTPRequest):
|
|
http.cancel_request()
|
|
if http.is_inside_tree():
|
|
remove_child(http)
|
|
|
|
|
|
func request(url: String, callback: Callable,
|
|
body: Dictionary = {}, method: int = HTTPClient.METHOD_GET,
|
|
cancel_callback: Array = []) -> Error:
|
|
|
|
var data = JSON.stringify(body)
|
|
var headers = []
|
|
|
|
var http = HTTPRequest.new()
|
|
http.use_threads = true
|
|
add_child(http)
|
|
|
|
var err = http.request(url, headers, method, data)
|
|
cancel_callback.append(cancel_http_func.bind(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)
|
|
|
|
return err
|
|
|
|
|
|
func request_raw(url: String, callback: Callable,
|
|
data: PackedByteArray, method: int = HTTPClient.METHOD_GET,
|
|
cancel_callback: Array = []) -> Error:
|
|
|
|
var headers = []
|
|
|
|
var http = HTTPRequest.new()
|
|
http.use_threads = true
|
|
add_child(http)
|
|
|
|
var err = http.request_raw(url, headers, method, data)
|
|
cancel_callback.append(cancel_http_func.bind(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)
|
|
|
|
return err
|