Developers

Flat
Codea/Lua Tutorial
User: Corey (Concept Medley)
Date: 2/7/2014 4:51 pm
Views: 2954
Rating: 0

Well, it was stated that even more obscure languages would be welcomed for the API tutorials, so here it is. This is for the wonderful Codea iPad app which uses the Lua programming language. I used David Kolf's JSON module to get data out of the responses.


-- GET A SESSION
function getSession()
username=url_encode("USERNAME")
password=url_encode("PASSWORD")
key="API-KEY"

headers={}
headers["Content-Type"]="application/x-www-form-urlencoded"
params={
method="POST",
headers=headers,
data="api_key_id=" .. key .. "&username=" ..username .. "&password=" .. password
}
http.request("https://www.thegamecrafter.com/api/session", sessionSuccess, tgcError, params)
end


-- FETCH MY ACCOUNT INFO
function sessionSuccess(data)
tbl=json.decode(data)

sessionId=tbl["result"]["id"]
user=tbl["result"]["user_id"]

params={
method="GET"
}
http.request("https://www.thegamecrafter.com/api/user/" .. user ..
"?session_id=" .. sessionId, userSuccess, tgcError, params)
end


-- UPLOAD A FILE
function userSuccess(data)
tbl=json.decode(data)

fileStr=readAll(os.getenv("HOME") .. "/Documents/FILENAME.png")
folderId=tbl["result"]["root_folder_id"]
fileName="FILENAME.png"
fileType="image/png" -- replace with proper file type
boundary="SOME_RANDOM_BOUNDARY_NAME"
crlf="\r\n"

headers={}
headers["Content-Type"]="multipart/form-data; boundary=" .. boundary
message="--" .. boundary .. crlf ..
"Content-Disposition: form-data; name=\"folder_id\"" .. crlf .. crlf..
folderId .. crlf .. "--" .. boundary .. crlf ..
"Content-Disposition: form-data; name=\"session_id\"" .. crlf .. crlf ..
sessionId .. crlf .. "--" .. boundary .. crlf ..
"Content-Disposition: form-data; name=\"file\"; filename=\"" ..
fileName .."\"" .. crlf ..
"Content-Type: " .. fileType .. crlf .. crlf ..
fileStr .. "\r\n" .. "--" .. boundary .. "--"
headers["Content-Length"]=tostring(string.len(message))
params={
method="POST",
headers=headers,
data=message
}
http.request("https://www.thegamecrafter.com/api/file", uploadSuccess, tgcError, params)
end


-- SEARCH GAMES
function uploadSuccess(data)
query="Steampunk"

params={
method="GET"
}
http.request("https://www.thegamecrafter.com/api/game" ..
"?session_id=" .. sessionId .. "&q=" .. query, logout, tgcError, params)
end


-- HELPER METHODS
function tgcError(error)
print(error)
end

function logout(data)
params={
method="DELETE"
}
http.request("https://www.thegamecrafter.com/api/session/" .. sessionId, function(data)
print(data)
end, tgcError, params)
end

function url_encode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w %-%_%.%~])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "%%20")
end
return str
end

function readAll(file)
local f = io.open(file, "rb")
local content = f:read("*all")
f:close()
return content
end

Re: Codea/Lua Tutorial
JT
User: JT
Date: 2/16/2014 2:09 pm
Views: 0
Rating: 0

Could you attach a text file or point me to a gist. Something that will maintain the formatting? 

Re: Codea/Lua Tutorial
User: Corey (Concept Medley)
Date: 2/16/2014 7:41 pm
Views: 38
Rating: 0

I attached the original code.

tgc_api.lua tgc_api.lua
Re: Codea/Lua Tutorial
JT
User: JT
Date: 2/18/2014 9:39 am
Views: 955
Rating: 0

Thanks. Points awarded.

PreviousNext