app :: AsyncHttpRequest :: defaultinit
# Thread executing an HTTP request asynchronously
#
# The request is sent to `uri`.
# Either `uri`, or `uri_root` and `uri_tail`, must be set in subclasses.
#
# If `deserialize_json`, the default behavior, the response is deserialized from JSON
#
# If `delay > 0.0`, sending the request is delayed by the given `delay` in seconds.
# It can be used to delay resending a request on error.
#
# Four callback methods act on the main/UI thread,
# they should be implemented as needed in subclasses:
# * `before`
# * `on_load`
# * `on_fail`
# * `after`
#
# See full example at `examples/http_request_example.nit`.
abstract class AsyncHttpRequest
super Thread
# URI target of this request, by default it is composed of `uri_root / uri_tail`
fun uri: Text do return uri_root / uri_tail
# Root URI of the remote server, usually the scheme and remote host
fun uri_root: String is abstract
# Right part of the URI, after `uri_root`, often the resource path and the query
fun uri_tail: String do return ""
# Should the response content be deserialized from JSON?
var deserialize_json = true is writable
# Delay in seconds before sending this request
var delay = 0.0 is writable
redef fun start
do
before
super
end
redef fun main
do
var delay = delay
if delay > 0.0 then delay.sleep
var uri = uri
# Execute REST request
var rep = uri.http_get
if rep.is_error then
app.run_on_ui_thread new RestRunnableOnFail(self, rep.error)
return null
end
if deserialize_json then
# Deserialize
var deserializer = new JsonDeserializer(rep.value)
var res = deserializer.deserialize
if deserializer.errors.not_empty then
app.run_on_ui_thread new RestRunnableOnFail(self, deserializer.errors.first)
else
app.run_on_ui_thread new RestRunnableOnLoad(self, res, rep.code)
end
else
# Return text data
app.run_on_ui_thread new RestRunnableOnLoad(self, rep.value, rep.code)
return null
end
app.run_on_ui_thread new RestRunnableJoin(self)
return null
end
# Prepare the UI or other parts of the program before executing the REST request
fun before do end
# Invoked when the HTTP request returned valid data
#
# If `deserialize_json`, the default behavior, this method is invoked only if deserialization was successful.
# In this case, `result` may be any deserialized object.
#
# Otherwise, if `not deserialize_json`, `result` contains the content of the response as a `String`.
fun on_load(result: nullable Object, http_status_code: Int) do end
# Invoked when the HTTP request has failed and no data was received or deserialization failed
fun on_fail(error: Error) do print_error "HTTP request '{uri}' failed with: {error}"
# Complete this request whether it was a success or not
fun after do end
end
lib/app/http_request.nit:48,1--140,3