Created
June 7, 2014 03:50
-
-
Save variousauthors/ff9490f81a0e17b656a8 to your computer and use it in GitHub Desktop.
GameJolt API "Hello World"!
This file contains hidden or 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
function do_socket_stuff () | |
print("in") | |
local http = require("socket.http") | |
local ltn12 = require("ltn12") | |
local base_url = "http://gamejolt.com/api/game/v1" | |
--Helper for priniting nested table | |
function deep_print(tbl) | |
for i, v in pairs(tbl) do | |
if type(v) == "table" then | |
deep_print(v) | |
else | |
print(i, v) | |
end | |
end | |
end | |
function http_request( args ) | |
--http.request(url [, body]) | |
--http.request{ | |
-- url = string, | |
-- [sink = LTN12 sink,] | |
-- [method = string,] | |
-- [headers = header-table,] | |
-- [source = LTN12 source], | |
-- [step = LTN12 pump step,] | |
-- [proxy = string,] | |
-- [redirect = boolean,] | |
-- [create = function] | |
--} | |
-- | |
-- | |
local resp, r = {}, {} | |
if args.endpoint then | |
local params = "" | |
if args.method == nil or args.method == "GET" then | |
-- prepare query parameters like http://xyz.com?q=23&a=2 | |
if args.params then | |
for i, v in pairs(args.params) do | |
params = params .. i .. "=" .. v .. "&" | |
end | |
end | |
end | |
params = string.sub(params, 1, -2) | |
local url = "" | |
if params then url = base_url .. args.endpoint .. "?" .. params else url = base_url .. args.endpoint end | |
print(url) | |
client, code, headers, status = http.request{url=url, sink=ltn12.sink.table(resp), | |
method=args.method or "GET", headers=args.headers, source=args.source, | |
step=args.step, proxy=args.proxy, redirect=args.redirect, create=args.create } | |
r['code'], r['headers'], r['status'], r['response'] = code, headers, status, resp | |
else | |
error("endpoint is missing") | |
end | |
return r | |
end | |
deep_print(http_request{endpoint="/auth/", params={ game_id=19552, username="arrogant.gamer", user_token="secrets" }}) | |
print("===") | |
deep_print(http_request{endpoint="/users/", params={ game_id=19552, username="arrogant.gamer", user_token="secrets", signature="abunch27392uof8937927numbers" }}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment