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:
See full example at examples/http_request_example.nit
.
app :: AsyncHttpRequest :: before
Prepare the UI or other parts of the program before executing the REST requestapp :: AsyncHttpRequest :: defaultinit
app :: AsyncHttpRequest :: delay=
Delay in seconds before sending this requestapp :: AsyncHttpRequest :: deserialize_json
Should the response content be deserialized from JSON?app :: AsyncHttpRequest :: deserialize_json=
Should the response content be deserialized from JSON?app :: AsyncHttpRequest :: on_fail
Invoked when the HTTP request has failed and no data was received or deserialization failedapp :: AsyncHttpRequest :: uri
URI target of this request, by default it is composed ofuri_root / uri_tail
app :: AsyncHttpRequest :: uri_root
Root URI of the remote server, usually the scheme and remote hostapp :: AsyncHttpRequest :: uri_tail
Right part of the URI, afteruri_root
, often the resource path and the query
app $ AsyncHttpRequest :: SELF
Type of this instance, automatically specialized in every classapp :: AsyncHttpRequest :: before
Prepare the UI or other parts of the program before executing the REST requestcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
app :: AsyncHttpRequest :: defaultinit
core :: Finalizable :: defaultinit
core :: Object :: defaultinit
pthreads :: Thread :: defaultinit
app :: AsyncHttpRequest :: delay=
Delay in seconds before sending this requestapp :: AsyncHttpRequest :: deserialize_json
Should the response content be deserialized from JSON?app :: AsyncHttpRequest :: deserialize_json=
Should the response content be deserialized from JSON?core :: Finalizable :: finalize
Liberate any resources held byself
before the memory holding self
is freed
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
app :: AsyncHttpRequest :: on_fail
Invoked when the HTTP request has failed and no data was received or deserialization failedcore :: Object :: output_class_name
Display class name on stdout (debug only).app :: AsyncHttpRequest :: uri
URI target of this request, by default it is composed ofuri_root / uri_tail
app :: AsyncHttpRequest :: uri_root
Root URI of the remote server, usually the scheme and remote hostapp :: AsyncHttpRequest :: uri_tail
Right part of the URI, afteruri_root
, often the resource path and the query
app :: MyHttpRequest
Simple asynchronous HTTP request to http://example.com/ displaying feedback to the windowAsyncHttpRequest
where uri
is an attribute
# 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
redef class AsyncHttpRequest
redef fun main
do
var res = super
jvm.detach_current_thread
return res
end
end
lib/android/http_request.nit:84,1--92,3