mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2025-12-09 07:46:20 +00:00
🆑 config: Added `TTS_HTTP_TIMEOUT_SECONDS` for setting the maximum duration TTS HTTP requests can run for before being aborted. /🆑 DNM because we need the rust-g PR to get released: https://github.com/tgstation/rust-g/pull/210 Crit prio because rounds will not restart if there are hung TTS requests and the TTS server is absolute dogshit and doesn't prevent them.
98 lines
2.0 KiB
Plaintext
98 lines
2.0 KiB
Plaintext
/datum/http_request
|
|
var/id
|
|
var/in_progress = FALSE
|
|
|
|
var/method
|
|
var/body
|
|
var/headers
|
|
var/url
|
|
/// If present response body will be saved to this file.
|
|
var/output_file
|
|
|
|
/// If present request will timeout after this duration
|
|
var/timeout_seconds
|
|
|
|
var/_raw_response
|
|
|
|
/datum/http_request/proc/prepare(method, url, body = "", list/headers, output_file, timeout_seconds)
|
|
if (!length(headers))
|
|
headers = ""
|
|
else
|
|
headers = json_encode(headers)
|
|
|
|
src.method = method
|
|
src.url = url
|
|
src.body = body
|
|
src.headers = headers
|
|
src.output_file = output_file
|
|
src.timeout_seconds = timeout_seconds
|
|
|
|
/datum/http_request/proc/execute_blocking()
|
|
_raw_response = rustg_http_request_blocking(method, url, body, headers, build_options())
|
|
|
|
/datum/http_request/proc/begin_async()
|
|
if (in_progress)
|
|
CRASH("Attempted to re-use a request object.")
|
|
|
|
id = rustg_http_request_async(method, url, body, headers, build_options())
|
|
|
|
if (isnull(text2num(id)))
|
|
stack_trace("Proc error: [id]")
|
|
_raw_response = "Proc error: [id]"
|
|
else
|
|
in_progress = TRUE
|
|
|
|
/datum/http_request/proc/build_options()
|
|
return json_encode(list(
|
|
"output_filename"=(output_file ? output_file : null),
|
|
"body_filename"=null,
|
|
"timeout_seconds"=(timeout_seconds ? timeout_seconds : null)))
|
|
|
|
/datum/http_request/proc/is_complete()
|
|
if (isnull(id))
|
|
return TRUE
|
|
|
|
if (!in_progress)
|
|
return TRUE
|
|
|
|
var/r = rustg_http_check_request(id)
|
|
|
|
if (r == RUSTG_JOB_NO_RESULTS_YET)
|
|
return FALSE
|
|
else
|
|
_raw_response = r
|
|
in_progress = FALSE
|
|
return TRUE
|
|
|
|
/datum/http_request/proc/into_response()
|
|
var/datum/http_response/R = new()
|
|
|
|
try
|
|
var/list/L = json_decode(_raw_response)
|
|
R.status_code = L["status_code"]
|
|
R.headers = L["headers"]
|
|
R.body = L["body"]
|
|
catch
|
|
R.errored = TRUE
|
|
R.error = _raw_response
|
|
|
|
return R
|
|
|
|
/datum/http_response
|
|
var/status_code
|
|
var/body
|
|
var/list/headers
|
|
|
|
var/errored = FALSE
|
|
var/error
|
|
|
|
/datum/http_response/serialize_list(list/options, list/semvers)
|
|
. = ..()
|
|
.["status_code"] = status_code
|
|
.["body"] = body
|
|
.["headers"] = headers
|
|
|
|
.["errored"] = errored
|
|
.["error"] = error
|
|
return .
|