Created
October 28, 2010 22:45
-
-
Save robotlolita/652495 to your computer and use it in GitHub Desktop.
This file contains 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
/************************************************************************** | |
* 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