From 2a2a7cd31d3582e9f92d3c4cd539ca838b9f5792 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Wed, 27 Mar 2024 15:03:32 +0100 Subject: [PATCH] Add 'parse_utf8' argument (fix #5) --- docs/docs/reference/edl.md | 8 ++++---- src/Edl.gd | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/docs/reference/edl.md b/docs/docs/reference/edl.md index 6261c6a..c127c2e 100644 --- a/docs/docs/reference/edl.md +++ b/docs/docs/reference/edl.md @@ -6,7 +6,7 @@ sidebar_position: 8 Pulls files from the internet, awaitable and batchable. ## Functions -### *Dictionary* awaited_request(*String* url, *HTTPClient.Method* method = *HTTPClient.Method.METHOD_GET*, *PackedStringArray* headers = *PackedStringArray([])*, *String* data = *""*) +### *Dictionary* awaited_request(*String* url, *bool* parse_utf8, *HTTPClient.Method* method = *HTTPClient.Method.METHOD_GET*, *PackedStringArray* headers = *PackedStringArray([])*, *String* data = *""*) :::note[Awaiting required] Using the `await` keyword is required for this function. ::: @@ -17,7 +17,7 @@ The returned `Dictionary` has the following structure (example): { "result": 0, "http_code": 200, "headers": [ "Server": "nginx" ], "body": [], "body_utf8": [] } ^ ^ ^ ^ ^ | | | | | - | | | | --------- The body from the server, as a UTF-8 string + | | | | --------- The body from the server, as a UTF-8 string (if 'parse_utf8' is true) | | | ----------------------- The body from the server, as bytes | | ------------------------------------------------------ A array of headers | ---------------------------------------------------------------------- The HTTP response code @@ -32,7 +32,7 @@ Returns a file from the internet without returning the godot code, http code or Returns `null` on error. To ignore HTTP errors (ie. non-200 statuses) set `ignore_http_code` to `true`. Returns a UTF-8 string with `return_utf8` turned on, returns bytes when turned off. -### *Array[Dictionary]* batch_awaited_request(*PackedStringArray* urls, *HTTPClient.Method* method = *HTTPClient.Method.METHOD_GET*, *PackedStringArray* headers = *PackedStringArray([])*, *String* data = *""*) +### *Array[Dictionary]* batch_awaited_request(*PackedStringArray* urls, *bool* parse_utf8, *HTTPClient.Method* method = *HTTPClient.Method.METHOD_GET*, *PackedStringArray* headers = *PackedStringArray([])*, *String* data = *""*) :::note[Awaiting required] Using the `await` keyword is required for this function. ::: @@ -43,7 +43,7 @@ The returned `Dictionary`s have the following structure (example): { "result": 0, "http_code": 200, "headers": [ "Server": "nginx" ], "body": [], "body_utf8": [] } ^ ^ ^ ^ ^ | | | | | - | | | | --------- The body from the server, as a UTF-8 string + | | | | --------- The body from the server, as a UTF-8 string (if 'parse_utf8' is true) | | | ----------------------- The body from the server, as bytes | | ------------------------------------------------------ A array of headers | ---------------------------------------------------------------------- The HTTP response code diff --git a/src/Edl.gd b/src/Edl.gd index 36d2e41..a4f3042 100644 --- a/src/Edl.gd +++ b/src/Edl.gd @@ -30,10 +30,10 @@ func generate_id() -> int: logger.diagf("Edl", "Generated new download id " + str(id)) return id -func awaited_request(url: String, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Dictionary: +func awaited_request(url: String, parse_utf8: bool, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Dictionary: logger.verbf("Edl", "Creating awaited request") var id: int = create_download(url, method, headers, data) - start_download(id) + start_download(id, parse_utf8) logger.diagf("Edl", "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] @@ -41,19 +41,19 @@ func awaited_request(url: String, method: HTTPClient.Method = HTTPClient.Method. return dldata func oneline_awaited_request(url: String, return_utf8: bool = true, ignore_http_code: bool = false, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Variant: - var dldata: Dictionary = await awaited_request(url, method, headers, data) + var dldata: Dictionary = await awaited_request(url, return_utf8, method, headers, data) if dldata["result"] != Error.OK: return null elif !ignore_http_code and dldata["http_code"] != 200: return null else: if return_utf8: return dldata["body_utf8"] else: return dldata["body"] -func batch_awaited_request(urls: PackedStringArray, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Array[Dictionary]: +func batch_awaited_request(urls: PackedStringArray, parse_utf8: bool, method: HTTPClient.Method = HTTPClient.Method.METHOD_GET, headers: PackedStringArray = PackedStringArray([]), data: String = "") -> Array[Dictionary]: logger.verbf("Edl", "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) + start_download(id, parse_utf8) logger.diagf("Edl", "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]) @@ -66,7 +66,7 @@ func create_download(url: String, method: HTTPClient.Method = HTTPClient.Method. list_queue.merge({ id: { "url": url, "method": method, "headers": headers, "body": body } }) return id -func start_download(id) -> void: +func start_download(id: int, parse_utf8: bool) -> void: logger.verbf("Edl", "Starting request " + str(id)) list_active.merge({ id: list_queue[id] }) list_queue.erase(id) @@ -77,7 +77,7 @@ func start_download(id) -> void: download.use_threads = true var lambda: Callable = func(result: int, http_code: int, headers: PackedStringArray, body: PackedByteArray) -> void: logger.verbf("Edl", "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_complete.merge({ id: { "result": result, "http_code": http_code, "headers": headers, "body": body, "body_utf8": body.get_string_from_utf8() if !parse_utf8 else null } }) list_active.erase(id) download.connect("request_completed", lambda) add_child(download)