Files
code_of_conquest_dnd/client/scripts/net/http_transport.gd

45 lines
2.2 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
# Bound a hung proxy so the request completes with RESULT_TIMEOUT (→ failed()
# → the service degrades) instead of never returning. Without this the
# considering-state indicator would rotate forever on a hang.
_http.timeout = ProxyConfig.request_timeout_seconds()
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)
# Parse via JSON.new() rather than JSON.parse_string(): the latter pushes an
# engine-level error on a non-JSON body (e.g. a gateway's HTML 502), which the
# project's gutconfig promotes to a false test failure. A non-JSON body stays
# null here — the service treats a non-dict body as a failure and degrades.
var json := JSON.new()
var parsed = null if json.parse(raw.get_string_from_utf8()) != OK else json.data
return DmResponse.ok(response_code, parsed)