Saving to Cloudant DBaaS from Corona SDK

Following on from my Cloudant DBaaS 101 post here is how you can save to a Cloudant DBaaS from Corona Labs SDK iOS/Android app.

Just raw code. Needs work but gives you a way to have a reusable function that also has a network listener to read the response from cloudant.

-- Require Objects
local socket = require("socket");
local http = require("socket.http");
local ltn12 = require("ltn12");
local json = require ("json");
local mime = require( "mime" );

-- The json data variable
jsondata = {}

-- Alert Box Handler
local function onGenericAlertBox( event )
if "clicked" == event.action then
local i = event.index
if 1 == i then
end
end
return true
end

-- The Network Save Listener
local function CloudantNetworkListener( event )
-- You function needs to be bigger than this but this gives you an idea how to process data back from Cloudant.
local retval
local errorCaptured = false
if ( event.isError ) then
if errorCaptured == false then
--ERROR: Error during request, code: -1003, details: A server with the specified hostname could not be found.
print ( "NetworkError:isError RESPONSE: " .. event.response )
retval = native.showAlert( "Error submitting your event", "This is embarrassing. There was an error saving your request. Here is the raw error: " .. event.response, { "OK" }, onGenericAlertBox)
errorCaptured = true
end

else
DoPrint("." .. event.response .. ".")
if (event.response == nil) then
-- show network down error

elseif (event.response == "") then
-- show network down error

elseif (string.find(event.response, "ok") > 1) then
if (string.find(event.response, "true") > 1) then
print ( "NetworkError: RESPONSE: " .. event.response )
retval = native.showAlert( "Saved", event.response , { "OK" }, onGenericAlertBox)
errorCaptured = true

end
elseif (string.find(event.response, "error") > 1) then
--{"error":"unauthorized","reason":"one of _writer, _creator is required for this request"}
if (string.find(event.response, "unauthorized") > 1) then
DoPrint ( "NetworkError: RESPONSE: " .. event.response )
retval = native.showAlert( "Error submitting your event", "This is embarrassing. I was unable to save your event due to invalid access." , { "OK" }, onGenericAlertBox)
errorCaptured = true
end
end

if errorCaptured == false then
print ( "NetworkError: RESPONSE: " .. event.response )
retval = native.showAlert( "Error submitting your event", "This is embarassing. There was an unknown error saving your request. Here is the raw error: " .. event.response, { "OK" }, onGenericAlertBox)
else
-- Save OK, Carry On
end
end
end

function SaveToCloudant(url, action, in_json, callback)

local json = require ( "json" )
local headers = {}

headers["Authorization"] = "Basic " .. mime.b64( "yourusername:yourpassword" )
headers["Content-Type"] = "application/json"

local params = {}
params.headers = headers
params.body = json.encode( in_json )

network.request ( url, action, callback, params )
end

-- The Data To Save
jsondata.nickname ="simon"
jsondata.sample_string = "yada"
jsondata.sample_int = 123
jsondata.datetime = tostring(os.time())

-- Save to Cloudant
SaveToCloudant("https://youraccount.cloudant.com/testtable", "POST", jsondata, CloudantNetworkListener)