Download -> Request

This commit is contained in:
JeremyStar™ 2024-02-10 18:48:49 +01:00
parent 0e0afa60fc
commit 6ec8ec785f

View file

@ -32,45 +32,45 @@ func generate_id() -> int:
logger.diag("Generated new download id " + str(id))
return id
func awaited_download(url: String, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Dictionary:
logger.verb("Creating awaited download")
func awaited_request(url: String, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Dictionary:
logger.verb("Creating awaited request")
var id: int = create_download(url, method, headers, data)
start_download(id)
logger.diag("Waiting for download " + str(id) + " to finish")
logger.diag("Waiting for request " + str(id) + " to finish")
while !is_download_complete(id): await get_tree().create_timer(0.1, true).timeout
var dldata: Dictionary = list_complete[id]
list_complete.erase(id)
return dldata
func batch_awaited_download(urls: PackedStringArray, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Array[Dictionary]:
logger.verb("Creating " + str(urls.size()) + " awaited download(s)")
var dldata: Array[Dictionary]
func batch_awaited_request(urls: PackedStringArray, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Array[Dictionary]:
logger.verb("Creating " + str(urls.size()) + " awaited request(s)")
var dldata: Array[Dictionary]= []
for url in urls:
var id: int = create_download(url, method, headers, data)
start_download(id)
logger.diag("Waiting for download " + str(id) + " to finish")
logger.diag("Waiting for request " + str(id) + " to finish")
while !is_download_complete(id): await get_tree().create_timer(0.1, true).timeout
dldata.append(list_complete[id])
list_complete.erase(id)
return dldata
func create_download(url: String, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), body: String = "") -> int:
logger.verb("Creating new download\n-> URL: " + url + "\n-> Method: " + str(method) + "\nHeaders:\n" + str(headers) + "\nBody:\n" + body)
logger.verb("Creating new request\n-> URL: " + url + "\n-> Method: " + str(method) + "\nHeaders:\n" + str(headers) + "\nBody:\n" + body)
var id = generate_id()
list_queue.merge({ id: { "url": url, "method": method, "headers": headers, "body": body } })
return id
func start_download(id) -> void:
logger.verb("Starting download " + str(id))
logger.verb("Starting request " + str(id))
list_active.merge({ id: list_queue[id] })
list_queue.erase(id)
logger.diag("Creating new HTTPRequest \"Download #" + str(id) + "\"")
logger.diag("Creating new HTTPRequest \"Request #" + str(id) + "\"")
var download: HTTPRequest = HTTPRequest.new()
download.name = "Download #" + str(id)
download.name = "Request #" + str(id)
download.accept_gzip = true
download.use_threads = true
var lambda: Callable = func(result: int, http_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
logger.verb("Download " + str(id) + " completed\nResult: " + str(result) + "\nHTTP response code: " + str(http_code) + "\nHeaders:\n" + str(headers) + "\nBody size: " + str(body.size()) + " Bytes // " + str(core.misc.byte2mib(body.size(), true)) + " MiB")
logger.verb("Request " + str(id) + " completed\nResult: " + str(result) + "\nHTTP response code: " + str(http_code) + "\nHeaders:\n" + str(headers) + "\nBody size: " + str(body.size()) + " Bytes // " + str(core.misc.byte2mib(body.size(), true)) + " MiB")
list_complete.merge({ id: { "result": result, "http_code": http_code, "headers": headers, "body": body, "body_utf8": body.get_string_from_utf8() } })
list_active.erase(id)
download.connect("request_completed", lambda)
@ -80,9 +80,9 @@ func start_download(id) -> void:
func is_download_complete(id: int) -> bool: return list_complete.has(id)
func clean_queue() -> void:
logger.verb("Cleaning download queue")
logger.verb("Cleaning request queue")
list_queue.clear()
func clean_completed() -> void:
logger.verb("Cleaning completed downloads")
logger.verb("Cleaning completed requests")
list_complete.clear()