- Types on every parameter, return value and variable, and snake_case names (the player name limit is the constant MAX_NAME_LENGTH). - Spelling fixes: *_triggered, character_selected, separator, caught_errors. The server's catched_errors key stays as the server sends it. - Signals are connected with signal.connect() instead of by name. - Code is ordered signals, constants, exported vars, vars, @onready vars, functions, with @onready on one line and two blank lines between functions. - Every script starts with a ## description. Template comments, old LOC notes and commented-out code are gone, apart from the local play code. - Lines are at most 100 characters, apart from the local play path. - The screenshot tour turns off the blinking text cursor so pictures match.
43 lines
1.4 KiB
GDScript
43 lines
1.4 KiB
GDScript
class_name HttpTransport
|
|
extends RefCounted
|
|
## Sends HTTP requests for [code]MusicServerClient[/code] using [HTTPRequest] nodes.
|
|
##
|
|
## Tests subclass this to answer requests without a network connection.
|
|
|
|
## Seconds before a request is given up and reported as failed. This covers the whole
|
|
## download, so it is generous enough for long songs on a slow connection.
|
|
|
|
var timeout_seconds: float = 120.0
|
|
var _owner: Node
|
|
|
|
|
|
func _init(owner: Node) -> void:
|
|
_owner = owner
|
|
|
|
|
|
## Requests [param url] and calls [param on_complete] with
|
|
## [code](response_code: int, body: PackedByteArray)[/code]. Connection errors and
|
|
## timeouts are reported with response code 0.
|
|
func request(url: String, method: int, on_complete: Callable) -> void:
|
|
var http_request: HTTPRequest = HTTPRequest.new()
|
|
http_request.timeout = timeout_seconds
|
|
_owner.add_child(http_request)
|
|
http_request.request_completed.connect(
|
|
func(
|
|
result: int,
|
|
response_code: int,
|
|
_headers: PackedStringArray,
|
|
body: PackedByteArray,
|
|
) -> void:
|
|
http_request.queue_free()
|
|
on_complete.call(response_code if result == HTTPRequest.RESULT_SUCCESS else 0, body)
|
|
)
|
|
var headers: PackedStringArray = []
|
|
if method == HTTPClient.METHOD_PUT:
|
|
headers.append("Content-Type: application/json")
|
|
var error: int = http_request.request(url, headers, method)
|
|
if error != OK:
|
|
http_request.queue_free()
|
|
push_error("Could not send request to %s (error %d)" % [url, error])
|
|
on_complete.call(0, PackedByteArray())
|