Skip to content

Commit

Permalink
V1 - Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Malte0621 committed Dec 10, 2021
1 parent b9a4496 commit c8f13ea
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 66 deletions.
4 changes: 2 additions & 2 deletions info.txt
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.
83 changes: 77 additions & 6 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
#include "mhttp.lua"
--[[
Malte0621's ExternalHttp Module.
local testingMode = true
--> Usage
#include "shared.lua"
local id2 = 0
local c = 0
function tick(dt)
if c >= 60*1.25 then
c = 0
id2 = id2 + 1
local function cb(data)
DebugPrint(data["Body"])
end
http.post("http://localhost/?rnd=" .. id2,{msg = "hello"},cb)
-- http.get("http://localhost/?rnd" .. id2,cb)
else
c = c + 1
end
end
--> API
--------------------------
"shared.lua"
--------------------------
http = {
get = function(<url:string>,<callback:function>)
post = function(<url:string>,<data:table>,<callback:function>)
}
callback arguments:
1. {
Success = <bool> -- true if request was sent successfully and false if not.
StatusCode = <int> -- HTTP Status code for the requested url (0 if not Success)
StatusMessage = <string> -- HTTP Status message for the requested url ("" if not Success)
Headers = <table> -- Table with all the headers {HeaderName = HeaderValue}
Body = <string> -- The body (text) of the requested url.
}
--------------------------
]]

#include "shared.lua" -- for sending requests in a mod.

local testingMode = false

local loaded = false
local showTicks = 60*1.5
Expand All @@ -23,25 +66,53 @@ local text = ""

local c = 0

SetString("savegame.mod.request","null")

local id = 0

local id2 = 0 -- For sending

function tick(dt)
local tmp = GetString("game.http.request")
if tmp ~= nil and tmp ~= "null" and not tonumber(tmp) then
local split = Split(tmp,"|")
local set = false
id = id + 1
if #split == 3 and split[1]:sub(1,4) == "http" and split[2] == "POST" and split[3]:sub(1,1) == "{" then
SetString("savegame.mod.request",tmp .. "|" .. id)
set = true
elseif #split == 2 and split[1]:sub(1,4) == "http" and split[2] == "GET" then
SetString("savegame.mod.request",tmp .. "|" .. id)
set = true
end
if set then
SetString("game.http.request",tostring(id))
else
SetString("game.http.request","null")
end
end
if testingMode then
if c >= 60*1.25 then
c = 0
-- http.PostAsyncA("http://localhost/",{msg = "hello"},cb)
local body = http.GetAsync("http://localhost/hi.txt")
DebugPrint(body)
id2 = id2 + 1
local function cb(data)
DebugPrint(data["Body"])
end
-- http.post("http://localhost/?rnd=" .. id2,{msg = "hello"},cb)
http.get("http://localhost/?rnd=" .. id2,cb)
else
c = c + 1
end
end
http_tick()
end

function draw()
if loaded and showTicks > 0 then
UiTranslate((UiWidth() / 2), (UiHeight() - (UiHeight() / 8)))
UiAlign("center center")
UiFont("bold.ttf", 32)
UiText("ExternalHttp (V2) by Malte0621, Loaded.")
UiText("ExternalHttp by Malte0621, Loaded.")
showTicks = showTicks - 1
end
end
58 changes: 0 additions & 58 deletions mhttp.lua

This file was deleted.

63 changes: 63 additions & 0 deletions shared.lua
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
}

0 comments on commit c8f13ea

Please sign in to comment.