Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**************************************************************************
* Returns a JSON object for the given request *
**************************************************************************/
def request(req: string, parms: array of string
,retry: bool = true): Json.Object? \
raises Error, BitlyError
result: string // string to get the JSON text
/* loads the API url */
var f = File.new_for_uri(_apiurl + req + "/?" + get_query(parms))
f.load_contents(null, out result)
/* parses the received JSON */
var parser = new Json.Parser
parser.load_from_data(result)
/* checks if the stuff is okay, then return the correspondent object
* or raises an error
*/
var root = parser.get_root().get_object()
var scode = root.get_int_member("status_code")
var status = root.get_string_member("status_txt")
if scode == 200
return root.get_object_member("data")
else if retry and (scode == 403 or scode == 503)
Thread.usleep(retry_delay)
return request(req, parms, retry)
else
case scode
when 403 do raise new BitlyError.REQ_403(status)
when 500 do raise new BitlyError.REQ_500(status)
when 503 do raise new BitlyError.REQ_503(status)
return null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment