app.nit: intro example for AsyncHttpRequest
authorAlexis Laferrière <alexis.laf@xymus.net>
Mon, 20 Jun 2016 13:43:51 +0000 (09:43 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 20 Jun 2016 16:13:12 +0000 (12:13 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/app/README.md
lib/app/examples/.gitignore [new file with mode: 0644]
lib/app/examples/Makefile [new file with mode: 0644]
lib/app/examples/http_request_example.nit [new file with mode: 0644]
lib/app/http_request.nit

index 0d2d24c..d48d0b5 100644 (file)
@@ -128,7 +128,8 @@ end
 The module `app::http_request` provides services to execute asynchronous HTTP request.
 The class `AsyncHttpRequest` hides the complex parallel logic and
 lets the user implement methods acting only on the UI thread.
-See the documentation of `AsyncHttpRequest` for more information.
+See the documentation of `AsyncHttpRequest` for more information and
+the full example at `examples/http_request_example.nit`.
 
 # Metadata annotations
 
diff --git a/lib/app/examples/.gitignore b/lib/app/examples/.gitignore
new file mode 100644 (file)
index 0000000..7f0a15f
--- /dev/null
@@ -0,0 +1,3 @@
+http_request_example
+http_request_example.apk
+http_request_example.app
diff --git a/lib/app/examples/Makefile b/lib/app/examples/Makefile
new file mode 100644 (file)
index 0000000..f516f0d
--- /dev/null
@@ -0,0 +1,10 @@
+all: http_request_example
+
+http_request_example: $(shell nitls -M http_request_example.nit linux)
+       nitc http_request_example.nit -m linux
+
+http_request_example.apk: $(shell nitls -M http_request_example.nit android)
+       nitc http_request_example.nit -m android
+
+http_request_example.app: $(shell nitls -M http_request_example.nit ios)
+       nitc http_request_example.nit -m ios
diff --git a/lib/app/examples/http_request_example.nit b/lib/app/examples/http_request_example.nit
new file mode 100644 (file)
index 0000000..65c3db8
--- /dev/null
@@ -0,0 +1,86 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Example for the `app::http_request` main service `AsyncHttpRequest`
+module http_request_example
+
+import app::ui
+import app::http_request
+
+# Simple asynchronous HTTP request to http://example.com/ displaying feedback to the window
+class MyHttpRequest
+       super AsyncHttpRequest
+
+       # Back reference to the window to show feedback to the user
+       var win: HttpRequestClientWindow
+
+       # ---
+       # Config the request
+
+       redef fun uri do return "http://example.com/"
+       redef fun deserialize_json do return false
+
+       # ---
+       # Customize callbacks
+
+       redef fun before
+       do
+               win.label_response.text = "Sending request..."
+
+               # Disable button to prevent double requests
+               win.button_request.enabled = false
+       end
+
+       redef fun on_load(data, status)
+       do win.label_response.text = "Received response code {status} with {data.as(Text).byte_length} bytes"
+
+       redef fun on_fail(error)
+       do win.label_response.text = "Connection error: {error}"
+
+       redef fun after do win.button_request.enabled = true
+end
+
+# Simpe window with a label and a button
+class HttpRequestClientWindow
+       super Window
+
+       # Root layout
+       var layout = new VerticalLayout(parent=self)
+
+       # Button to send request
+       var button_request = new Button(parent=layout, text="Press to send HTTP request")
+
+       # Label displaying feedback to user
+       var label_response = new Label(parent=layout, text="No response yet.")
+
+       init do button_request.observers.add self
+
+       redef fun on_event(event)
+       do
+               if event isa ButtonPressEvent and event.sender == button_request then
+                       # Prepare and send request
+                       var request = new MyHttpRequest(self)
+                       request.start
+               end
+       end
+end
+
+redef class App
+       redef fun on_create
+       do
+               # Create the main window
+               push_window new HttpRequestClientWindow
+               super
+       end
+end
index bcb18d1..80c6d8b 100644 (file)
@@ -44,6 +44,8 @@ end
 # * `on_load`
 # * `on_fail`
 # * `after`
+#
+# See full example at `examples/http_request_example.nit`.
 abstract class AsyncHttpRequest
        super Thread