WebClient folder renamed to net and WebClient renamed to HttpClient

This commit is contained in:
Vladimir Enchev
2014-04-14 15:02:34 +03:00
parent f7b5c9af3e
commit 2cdad80094
8 changed files with 21 additions and 21 deletions

20
net/Readme.md Normal file
View File

@@ -0,0 +1,20 @@
Sample code:
```
var webClientModule = require("WebClient");
var webClient = webClientModule.Client;
var client = new webClient();
client.getString("http://www.reddit.com/", function(result) {
// Result is string!
}, function(e) { console.log("Error:" + e.message); });
client.getJSON("http://www.reddit.com/r/aww.json?limit=10", function(result) {
// Result is JSON!
}, function(e) { console.log("Error:" + e.message); });
client.getImage("http://www.telerik.com/sfimages/default-source/Homepage/hp_any_approachf6e4079a7a99493a8ab2e367b9cb3f7d.png", function(result) {
// Result is tk.ui.Image!
}, function(e) { console.log("Error:" + e.message); });
```

View File

@@ -0,0 +1,85 @@
/**
* Android specific WebClient implementation.
*/
import image_module = require("Image/image");
import app_module = require("Application/application");
export class HttpClient {
/**
* Downloads string from url.
*/
public getString(url: string, successCallback: (result: string) => void, errorCallback?: (e: Error) => void) {
try {
if (successCallback) {
var context = app_module.tk.ui.Application.current.android.context;
com.koushikdutta.ion.Ion.with(context, url).asString().setCallback(new com.koushikdutta.async.future.FutureCallback({
onCompleted: function (e, result) {
if (e && errorCallback) {
errorCallback(new Error(e.toString()));
return;
}
successCallback(result);
}
}));
}
} catch (ex) {
if (errorCallback) {
errorCallback(ex);
}
}
}
public getJSON(url: string, successCallback: (result: Object) => void, errorCallback?: (e: Error) => void) {
try {
this.getString(url, function (data) {
if (successCallback) {
successCallback(JSON.parse(data));
}
}, errorCallback);
} catch (ex) {
if (errorCallback) {
errorCallback(ex);
}
}
}
public getImage(url: string, successCallback: (result: image_module.Image) => void, errorCallback?: (e: Error) => void) {
try {
if (successCallback) {
var context = app_module.tk.ui.Application.current.android.context;
com.koushikdutta.ion.Ion.with(context, url).asBitmap().setCallback(new com.koushikdutta.async.future.FutureCallback({
onCompleted: function (e, result) {
if (e && errorCallback) {
errorCallback(new Error(e.toString()));
return;
}
var image = new image_module.Image();
image.loadFromBitmap(result);
successCallback(image);
}
}));
}
} catch (ex) {
if (errorCallback) {
errorCallback(ex);
}
}
}
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
try {
} catch (ex) {
if (errorCallback) {
errorCallback(ex);
}
}
}
}

11
net/http_client.d.ts vendored Normal file
View File

@@ -0,0 +1,11 @@
/**
* The Client interface.
*/
import image_module = require("Image/image");
export declare class HttpClient {
private static get(url: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void)
getString(url: string, successCallback?: (result: string) => void, errorCallback?: (e: Error) => void)
getJSON(url: string, successCallback?: (result: Object) => void, errorCallback?: (e: Error) => void)
getImage(url: string, successCallback?: (result: image_module.Image) => void, errorCallback?: (e: Error) => void)
}

118
net/http_client.ios.ts Normal file
View File

@@ -0,0 +1,118 @@
/**
* iOS specific WebClient implementation.
*/
import image_module = require("Image/image");
import promises_module = require("promises/promises");
export class HttpClient {
/**
* Downloads string from url.
*/
public getString(url: string, successCallback: (result: string) => void, errorCallback?: (e: Error) => void) {
try {
HttpClient.get(url, function (data) {
if (successCallback) {
successCallback(Foundation.NSString.initWithDataEncoding(data, 4).toString());
}
}, errorCallback);
} catch (ex) {
if (errorCallback) {
errorCallback(ex);
}
}
}
public static getString(url : string) {
var d = new promises_module.Promises.Deferred();
new HttpClient().getString(url, r => d.resolve(r), e => d.reject(e));
return d.promise();
}
public getJSON(url: string, successCallback: (result: Object) => void, errorCallback?: (e: Error) => void) {
try {
this.getString(url, function (data) {
if (successCallback) {
successCallback(JSON.parse(data));
}
}, errorCallback);
} catch (ex) {
if (errorCallback) {
errorCallback(ex);
}
}
}
public getImage(url: string, successCallback: (result: image_module.Image) => void, errorCallback?: (e: Error) => void) {
HttpClient.get(url, function (data) {
if (successCallback) {
var image = new image_module.Image();
image.loadFromData(data);
successCallback(image);
}
}, errorCallback);
}
private static get(url: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void) {
if (!successCallback && !errorCallback)
{
var d = new promises_module.Promises.Deferred();
HttpClient.getUrl(url, r => d.resolve(r), e => d.reject(e));
return d.promise();
}
HttpClient.getUrl(url, successCallback, errorCallback);
}
private static getUrl(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
try {
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
var queue = Foundation.NSOperationQueue.mainQueue();
var session = Foundation.NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
var dataTask = session.dataTaskWithURLCompletionHandler(Foundation.NSURL.URLWithString(url), function (data, response, error) {
if (error) {
if (errorCallback) {
errorCallback(new Error(error.localizedDescription()));
}
} else if (successCallback) {
successCallback(data);
}
});
dataTask.resume();
session.finishTasksAndInvalidate();
} catch (ex) {
if (errorCallback) {
errorCallback(ex);
}
}
}
private static post(url: string, postData: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void) {
try {
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
var queue = Foundation.NSOperationQueue.mainQueue();
var session = Foundation.NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
var urlRequest = Foundation.NSMutableURLRequest.requestWithURL(Foundation.NSURL.URLWithString(url));
urlRequest.setHTTPMethod("POST");
urlRequest.setHTTPBody(Foundation.NSString.initWithString(postData).dataUsingEncoding(4));
var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest, function (data, response, error) {
if (error) {
if (errorCallback) {
errorCallback(new Error(error.localizedDescription()));
}
} else if (successCallback) {
successCallback(data);
}
});
dataTask.resume();
} catch (ex) {
if (errorCallback) {
errorCallback(ex);
}
}
}
}

2
net/index.ts Normal file
View File

@@ -0,0 +1,2 @@
declare var module, require;
module.exports = require("net/http_client");