Add 'parse_utf8' argument (fix #5)
This commit is contained in:
parent
54d81da32f
commit
2a2a7cd31d
2 changed files with 11 additions and 11 deletions
|
@ -6,7 +6,7 @@ sidebar_position: 8
|
|||
Pulls files from the internet, awaitable and batchable.
|
||||
|
||||
## Functions
|
||||
### *Dictionary* <u>awaited_request</u>(*String* <u>url</u>, *HTTPClient.Method* <u>method</u> = *HTTPClient.Method.METHOD_GET*, *PackedStringArray* <u>headers</u> = *PackedStringArray([])*, *String* <u>data</u> = *""*)
|
||||
### *Dictionary* <u>awaited_request</u>(*String* <u>url</u>, *bool* <u>parse_utf8</u>, *HTTPClient.Method* <u>method</u> = *HTTPClient.Method.METHOD_GET*, *PackedStringArray* <u>headers</u> = *PackedStringArray([])*, *String* <u>data</u> = *""*)
|
||||
:::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]* <u>batch_awaited_request</u>(*PackedStringArray* <u>urls</u>, *HTTPClient.Method* <u>method</u> = *HTTPClient.Method.METHOD_GET*, *PackedStringArray* <u>headers</u> = *PackedStringArray([])*, *String* <u>data</u> = *""*)
|
||||
### *Array[Dictionary]* <u>batch_awaited_request</u>(*PackedStringArray* <u>urls</u>, *bool* <u>parse_utf8</u>, *HTTPClient.Method* <u>method</u> = *HTTPClient.Method.METHOD_GET*, *PackedStringArray* <u>headers</u> = *PackedStringArray([])*, *String* <u>data</u> = *""*)
|
||||
:::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
|
||||
|
|
14
src/Edl.gd
14
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)
|
||||
|
|
Loading…
Reference in a new issue