Lua Example

NOTE: Get an APIKey.

This code will allow you to get a session, fetch account info, upload a file, and search games. From it you should be able to discern how to work with the entire TGC API.

 -- tgc api
 
 -- Use this function to perform your initial setup
 function setup()
     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
 
 function sessionSuccess(data)
     print(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
 
 function userSuccess(data)
     print(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"
     boundary="SOME_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
 
 function uploadSuccess(data)
     print(data)
 
     query="Steampunk"
     
     params={
         method="GET"
     }
     http.request("https://www.thegamecrafter.com/api/game" ..
         "?session_id=" .. sessionId .. "&q=" .. query, logout, tgcError, params)
 end
 
 function logout(data)
     print(data)
     
     params={
         method="DELETE"
     }
     http.request("https://www.thegamecrafter.com/api/session/" .. sessionId, function(data)
         print(data)
     end, tgcError, params)
 end
 
 function tgcError(error)
     print(error)
 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
^ Back to Top ^