diff options
Diffstat (limited to 'scripts/sample-httpjson-client.lua')
-rw-r--r-- | scripts/sample-httpjson-client.lua | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/scripts/sample-httpjson-client.lua b/scripts/sample-httpjson-client.lua new file mode 100644 index 0000000..a733f95 --- /dev/null +++ b/scripts/sample-httpjson-client.lua @@ -0,0 +1,37 @@ +--[[ + _____ _ _ +|_ _| __ ___ _ __ ___ _ _| | ___ _ _ ___ | | _ _ __ _ + | || '__/ _ \ '_ ` _ \| | | | |/ _ \| | | / __|_____| | | | | |/ _` | + | || | | __/ | | | | | |_| | | (_) | |_| \__ \_____| |__| |_| | (_| | + |_||_| \___|_| |_| |_|\__,_|_|\___/ \__,_|___/ |_____\__,_|\__,_| + Victor Roemer [WTFBBQHAX] + Nov. 03, 2016 10:53:27AM EST + A sample Restful JSON Client API. + This sample demonstrates how to comunicate with an HTTP JSON 3rdparty + API in Lua script. + APIs demonstrated: + * HTTP RestClient + * JSON +--]] + +-- GitHub API URL +url='https://api.github.com/repos/GrangerHub/tremulous/releases' + +-- HTTP Get request- retrieve the raw JSON response +txt = http.get(url) +assert(txt.code == 200) + +-- Decode raw JSON response into a Lua table +releases = rapidjson.decode(txt.body) + +-- GitHub returned an array of releases- The "most_recent" is item 1 +-- NOTE: Lua array indexing starts at `1` not `0`!! +most_recent = releases[1] + +-- FIXME: Remove hardcoded tag_name in this test +assert(most_recent.tag_name == "Oct-22-2016") + + +for i,asset in ipairs(most_recent.assets) do + print(asset.browser_download_url) +end |