36 lines
1.6 KiB
GDScript
36 lines
1.6 KiB
GDScript
extends "res://scripts/net/dm_transport.gd"
|
|
## The real transport: wraps an HTTPRequest Node the owner added to the tree.
|
|
## This is the one untested shim in the slice — it needs a live Node and a
|
|
## server, so it is verified by reading and by the harness live smoke, the same
|
|
## way the server's HTTP boundary is covered only by its gated --run-live test.
|
|
## A non-2xx status is NOT an error here; it is a normal DmResponse the service
|
|
## interprets. Only a transport-level failure (no response) yields failed().
|
|
|
|
const ProxyConfig = preload("res://scripts/net/proxy_config.gd")
|
|
const DmResponse = preload("res://scripts/net/dm_response.gd")
|
|
|
|
var _http: HTTPRequest
|
|
|
|
|
|
func _init(http: HTTPRequest) -> void:
|
|
_http = http
|
|
|
|
|
|
func post_json(path: String, body: Dictionary) -> DmResponse:
|
|
var url := ProxyConfig.base_url() + path
|
|
var headers := ["Content-Type: application/json"]
|
|
var payload := JSON.stringify(body)
|
|
var err := _http.request(url, headers, HTTPClient.METHOD_POST, payload)
|
|
if err != OK:
|
|
return DmResponse.failed("request() failed to start: error %d" % err)
|
|
# request_completed(result, response_code, headers, body) — awaiting a
|
|
# multi-arg signal returns the args as an Array.
|
|
var signal_args: Array = await _http.request_completed
|
|
var result: int = signal_args[0]
|
|
var response_code: int = signal_args[1]
|
|
var raw: PackedByteArray = signal_args[3]
|
|
if result != HTTPRequest.RESULT_SUCCESS:
|
|
return DmResponse.failed("transport result %d" % result)
|
|
var parsed = JSON.parse_string(raw.get_string_from_utf8()) # null if not JSON
|
|
return DmResponse.ok(response_code, parsed)
|