Posted 19 June 2017 - 08:05 AM
I am loading the API with os.loadAPI
PS.
If anyone has any interest in trying it out themselves, I will be releasing this in the near future and if it helps to identify the problem, I will give out the URL in DMs (for security reasons)
–EDIT–
Fixed middle() with tail calls (didn't see pinned post earlier), however, my send() function is still not being declared in the api's scope.
–EDIT 2–
I found the problem. I was declaring two send variables. I always seem to find the answer directly after asking for help. Thank you for your time.
Code:
Spoiler
--Define global api modifications to help later
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
--Define local variables
local subdTopics = {}
local baseUrl = "http://redacted.com/ccnet"
local send = "/send"
local query = "/query"
local dataToSend = "?message="
--Define functions
function send(topic,message) --Sends a message to a topic
http.get(baseUrl..send..topic..dataToSend..textutils.urlEncode(message))
end
function subscribe(topic) --Subscribes to a topic
table.insert(subdTopics,topic)
http.request(baseUrl..query..topic)
end
function unsubscribe(topic) --Unsubscribes from a topic
for k,v in pairs(subdTopics) do
if v==topic then
table.remove(subdTopics,k)
end
end
end
function receive() --Returns topic, message
local event = {middle("endernet_receive")}
return event[2],event[3]
end
function middle(eventName) --os.pullEvent = endernet.middle
--Define variables that I'd prefer not to leak out but are evaluated later
local event
local topic
local message
--Yield should work, because if it's overwritten, the computer won't work
event = {coroutine.yield()}
if event[1]=="terminate" then
error("Terminated")
end
--Parse the event
if event[1]=="http_success" then
topic = string.gsub(event[2],baseUrl..query,"")
message = event[3].readAll()
if table.contains(subdTopics,topic) then
http.request(event[2])
end
if eventName==nil or eventName=="endernet_receive" then
return "endernet_receive",topic,message --This is where the problem is. It does not return these variables
else
return middle(eventName)
end
elseif event[1]=="http_failure" then
if table.contains(subdTopics,string.gsub(event[2],baseUrl..query,"")) then
http.request(event[2])
end
return middle(eventName)
else
if eventName==nil or event[1]==eventName then
return table.unpack(event)
else
return middle(eventName)
end
end
end
Edited on 19 June 2017 - 06:21 AM