Whole-branch review Minor #1: JSON.parse_string pushes an engine-level error on a non-JSON body (e.g. a gateway HTML 502), which the project's gutconfig promotes to a false failure — the same reason FallbackLibrary already avoids it. Consistency fix; a non-JSON body still degrades. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
41 lines
1.9 KiB
GDScript
41 lines
1.9 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)
|
|
# 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)
|