Guest User

Untitled

a guest
Jun 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. /**************************************************************************
  2. * Returns a JSON object for the given request *
  3. **************************************************************************/
  4. def request(req: string, parms: array of string
  5. ,retry: bool = true): Json.Object? \
  6. raises Error, BitlyError
  7. result: string // string to get the JSON text
  8.  
  9. /* loads the API url */
  10. var f = File.new_for_uri(_apiurl + req + "/?" + get_query(parms))
  11. f.load_contents(null, out result)
  12.  
  13. /* parses the received JSON */
  14. var parser = new Json.Parser
  15. parser.load_from_data(result)
  16.  
  17. /* checks if the stuff is okay, then return the correspondent object
  18. * or raises an error
  19. */
  20. var root = parser.get_root().get_object()
  21. var scode = root.get_int_member("status_code")
  22. var status = root.get_string_member("status_txt")
  23.  
  24. if scode == 200
  25. return root.get_object_member("data")
  26.  
  27. else if retry and (scode == 403 or scode == 503)
  28. Thread.usleep(retry_delay)
  29. return request(req, parms, retry)
  30.  
  31. else
  32. case scode
  33. when 403 do raise new BitlyError.REQ_403(status)
  34. when 500 do raise new BitlyError.REQ_500(status)
  35. when 503 do raise new BitlyError.REQ_503(status)
  36. return null
Add Comment
Please, Sign In to add comment