lib/ios: implement `app::http_request`
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 14 Nov 2015 17:48:28 +0000 (12:48 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 11 Feb 2016 18:08:31 +0000 (13:08 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/app/http_request.nit
lib/ios/http_request.nit [new file with mode: 0644]

index 70990a9..4b62f4a 100644 (file)
@@ -21,6 +21,7 @@ import json::serialization
 
 import linux::http_request is conditional(linux)
 import android::http_request is conditional(android)
+import ios::http_request is conditional(ios)
 
 redef class App
        # Platform specific service to execute `task` on the main/UI thread
diff --git a/lib/ios/http_request.nit b/lib/ios/http_request.nit
new file mode 100644 (file)
index 0000000..ca66d78
--- /dev/null
@@ -0,0 +1,70 @@
+# 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.
+
+# Implementation of `app::http_request` for iOS
+module http_request
+
+import ios
+import cocoa::foundation
+intrude import app::http_request
+
+redef class App
+       redef fun run_on_ui_thread(task) import Task.main in "ObjC" `{
+               Task_incr_ref(task);
+               dispatch_async(dispatch_get_main_queue(), ^{
+                       Task_main(task);
+                       Task_decr_ref(task);
+               });
+       `}
+end
+
+redef class Text
+       redef fun http_get
+       do
+               var error_ref = new Ref[NSString]("Unknown Error".to_nsstring)
+               var data = to_nsstring.native_http_get(60.0, error_ref)
+
+               if data.address_is_null then
+                       # There was an error
+                       var error = new IOError(error_ref.item.to_s)
+                       return new HttpRequestResult(null, error)
+               else
+                       # TODO use the real return code instead of 200
+                       return new HttpRequestResult(data.to_s, null, 200)
+               end
+       end
+end
+
+redef class NSString
+       private fun native_http_get(timeout: Float, error_ref: Ref[NSString]): NSData
+       import Ref[NSString].item= in "ObjC" `{
+
+               NSURL *url = [NSURL URLWithString:self];
+               NSURLRequest *request = [NSURLRequest requestWithURL:url
+                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:timeout];
+
+               NSURLResponse *response = nil;
+               NSError *error = nil;
+               NSData *data = [NSURLConnection sendSynchronousRequest:request
+                       returningResponse:&response error:&error];
+
+               if (data == nil) {
+                       // Report error
+                       Ref_of_NSString_item__assign(error_ref, [error localizedDescription]);
+                       return nil;
+               }
+
+               return data;
+       `}
+end