Initial Android commit
This commit is contained in:
commit
1e2b80c13d
8521 changed files with 231475 additions and 0 deletions
33
addons/godot-twicil/helpers/http/http_headers.gd
Normal file
33
addons/godot-twicil/helpers/http/http_headers.gd
Normal file
|
@ -0,0 +1,33 @@
|
|||
extends Object
|
||||
class_name HttpHeaders
|
||||
|
||||
const HTTP_CONTENT_TYPE_JSON_UTF8 = "application/json; charset=utf-8"
|
||||
const HTTP_CONTENT_TYPE_JSON = "application/json"
|
||||
|
||||
|
||||
var headers:Dictionary
|
||||
|
||||
func _init(raw_headers:PoolStringArray):
|
||||
for raw_header in raw_headers:
|
||||
var header_parts: = (raw_header as String).split(":", true, 1) as Array
|
||||
var header_name: = (header_parts[0] as String).lstrip(" ").rstrip(" ")
|
||||
var header_value: = (header_parts[1] as String).lstrip(" ").rstrip(" ")
|
||||
|
||||
headers[header_name] = header_value
|
||||
|
||||
func get(key:String, ignore_case:bool = true)->String:
|
||||
for header_key in headers:
|
||||
if header_key.to_lower() == key.to_lower():
|
||||
return headers.get(header_key)
|
||||
|
||||
return "{no such header}"
|
||||
|
||||
static func to_pool_string_array(headers:Dictionary)->PoolStringArray:
|
||||
var raw_headers:PoolStringArray
|
||||
|
||||
for header in headers:
|
||||
var header_value:String = headers.get(header)
|
||||
|
||||
raw_headers.append(header + ": " + header_value)
|
||||
|
||||
return raw_headers
|
78
addons/godot-twicil/helpers/http/http_request_queue.gd
Normal file
78
addons/godot-twicil/helpers/http/http_request_queue.gd
Normal file
|
@ -0,0 +1,78 @@
|
|||
extends Node
|
||||
class_name HttpRequestQueue
|
||||
|
||||
|
||||
signal http_response_recieved(content_type, body)
|
||||
signal http_response_failed(error_code)
|
||||
signal request_completed(id, result, response_code, headers, body)
|
||||
signal request_completed_ex(id, result, response_code, http_headers, body)
|
||||
|
||||
|
||||
const REQUEST_ID_NO_ID = "{no_id}"
|
||||
|
||||
|
||||
var _http_request:HTTPRequest
|
||||
var request_queue = Array()
|
||||
var busy = false
|
||||
var current_request_id:String = REQUEST_ID_NO_ID
|
||||
|
||||
|
||||
func _ready()->void :
|
||||
__initialize_http_request()
|
||||
|
||||
|
||||
func enqueue_request(id:String, url:String, headers:PoolStringArray = PoolStringArray())->void :
|
||||
request_queue.append({"id":id, "url":url, "headers":headers})
|
||||
|
||||
if not busy:
|
||||
__process_request_queue()
|
||||
|
||||
|
||||
func __initialize_http_request()->void :
|
||||
_http_request = HTTPRequest.new()
|
||||
|
||||
add_child(_http_request)
|
||||
|
||||
_http_request.use_threads = true
|
||||
|
||||
_http_request.connect("request_completed", self, "_on_http_request_completed")
|
||||
|
||||
|
||||
func __process_request_queue()->void :
|
||||
if request_queue.empty():
|
||||
busy = false
|
||||
|
||||
return
|
||||
|
||||
if busy:
|
||||
return
|
||||
|
||||
busy = true
|
||||
|
||||
var request_data: = request_queue.pop_front() as Dictionary
|
||||
var request_url:String = request_data.get("url")
|
||||
var request_headers:PoolStringArray = request_data.get("headers")
|
||||
current_request_id = request_data.get("id")
|
||||
|
||||
_http_request.request(request_url, request_headers)
|
||||
|
||||
|
||||
|
||||
func _on_http_request_completed(result:int, response_code:int, headers:PoolStringArray, body:PoolByteArray)->void :
|
||||
var http_headers: = HttpHeaders.new(headers)
|
||||
emit_signal("request_completed", current_request_id, result, response_code, headers, body)
|
||||
emit_signal("request_completed_ex", current_request_id, result, response_code, http_headers, body)
|
||||
|
||||
if result == HTTPRequest.RESULT_SUCCESS:
|
||||
|
||||
var content_type: = http_headers.get("Content-Type") as String
|
||||
|
||||
emit_signal("http_response_recieved", content_type, body)
|
||||
|
||||
else :
|
||||
emit_signal("http_response_failed", response_code)
|
||||
|
||||
current_request_id = REQUEST_ID_NO_ID
|
||||
busy = false
|
||||
|
||||
__process_request_queue()
|
Loading…
Add table
Add a link
Reference in a new issue