local ____lualib = require("lualib_bundle") local __TS__StringTrim = ____lualib.__TS__StringTrim local ____exports = {} local ____server_config = require("server_config") local SERVER_CONFIG = ____server_config.SERVER_CONFIG local SERVER_KEY_SCOPE = "zombie_invasion" local LOCAL_OVERRIDE_SERVER_KEY = "" local cachedServerKey = nil local didPrintServerKeyWarning = false local function resolveServerKey(self) if cachedServerKey ~= nil then return cachedServerKey end local localOverrideKey = __TS__StringTrim(LOCAL_OVERRIDE_SERVER_KEY) if #localOverrideKey > 0 then cachedServerKey = localOverrideKey return cachedServerKey end if GameRules:IsCheatMode() then return "menya_ebut_negry_tolpoy" end if type(GetDedicatedServerKeyV3) == "function" then local rawKey = GetDedicatedServerKeyV3(SERVER_KEY_SCOPE) if rawKey ~= nil and rawKey ~= nil then local serverKey = tostring(rawKey) if #serverKey > 0 then cachedServerKey = serverKey return cachedServerKey end end end if not didPrintServerKeyWarning then didPrintServerKeyWarning = true print(("[api_helper] Некорректный DedicatedServerKey для scope='" .. SERVER_KEY_SCOPE) .. "'. Запросы API будут без x-custom-key.") end cachedServerKey = "" return cachedServerKey end --- Устанавливает стандартные заголовки для HTTP запроса function ____exports.setApiHeaders(self, request) local serverKey = resolveServerKey(nil) request:SetHTTPRequestHeaderValue("Content-Type", "application/json") if #serverKey > 0 then request:SetHTTPRequestHeaderValue("x-custom-key", serverKey) end request:SetHTTPRequestNetworkActivityTimeout(SERVER_CONFIG.NETWORK_TIMEOUT) request:SetHTTPRequestAbsoluteTimeoutMS(SERVER_CONFIG.ABSOLUTE_TIMEOUT) end --- Устанавливает заголовки для длительных запросов (например, сохранение игры) function ____exports.setApiHeadersLong(self, request) local serverKey = resolveServerKey(nil) request:SetHTTPRequestHeaderValue("Content-Type", "application/json") if #serverKey > 0 then request:SetHTTPRequestHeaderValue("x-custom-key", serverKey) end request:SetHTTPRequestNetworkActivityTimeout(SERVER_CONFIG.NETWORK_TIMEOUT * 3) request:SetHTTPRequestAbsoluteTimeoutMS(SERVER_CONFIG.ABSOLUTE_TIMEOUT * 3) end --- Тело POST как JSON (единая точка; совпадает с заголовком Content-Type в setApiHeaders). function ____exports.encodeApiBody(self, data) return json.encode(data) end return ____exports