-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
name = ExternalHttp V2 | ||
name = ExternalHttp | ||
author = Malte0621 | ||
description = A prototype http requesting mod for teardown. (V2) | ||
description = A prototype http requesting mod for teardown. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--[[ | ||
Malte0621's ExternalHttp Module. | ||
Check main.lua for api/usage | ||
]] | ||
|
||
-- Notice: Requests need a randomize due to teardown's cache system. Just add a random mumber within "?rnd=" at the end of the url. | ||
-- http.get("http://localhost/" .. "?rnd=" .. math.random(1,9999)) -- You might have to change ? to & if you're already using ? in the url. | ||
#include "json.lua" | ||
|
||
local callbacks = {} | ||
|
||
local pendingCallbacks = {} | ||
|
||
function http_tick(dt) | ||
local id = tonumber(GetString("game.http.request")) | ||
if id then | ||
callbacks[id] = table.remove(pendingCallbacks,1) | ||
SetString("game.http.request","null") | ||
end | ||
local success = {} | ||
for id,callback in pairs(callbacks) do | ||
local s,f = pcall(function() | ||
dofile("C:\\teardown_http_temp\\http_response" .. tostring(id) .. ".lua") | ||
return result | ||
end) | ||
if s then | ||
callback(json.decode(f)) | ||
table.insert(success,id) | ||
end | ||
end | ||
for i,v in ipairs(success) do | ||
callbacks[v] = nil | ||
end | ||
end | ||
|
||
http = { | ||
get = function(url,callback) | ||
if type(url) ~= "string" then | ||
error("url is required to be set as a table.") | ||
end | ||
if type(callback) ~= "function" then | ||
error("callback is required to be set as a function : function(response) end") | ||
end | ||
-- DebugPrint(url .. "|GET") -- For debugging | ||
SetString("game.http.request",url .. "|GET") | ||
table.insert(pendingCallbacks,callback) | ||
end, | ||
post = function(url,data,callback) | ||
if type(url) ~= "string" then | ||
error("url is required to be set as a table.") | ||
end | ||
if type(data) ~= "table" then | ||
error("data is required to be set as a table.") | ||
end | ||
if type(callback) ~= "function" then | ||
error("callback is required to be set as a function : function(response) end") | ||
end | ||
-- DebugPrint(url .. "|POST|" .. json.encode(data)) -- For debugging | ||
SetString("game.http.request",url .. "|POST|" .. json.encode(data)) | ||
table.insert(pendingCallbacks,callback) | ||
end | ||
} |