mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
http client with promises for both Android and iOS
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
export function when(...promises: Promise[]): Promise {
|
||||
var all_done = new Deferred();
|
||||
var results = [];
|
||||
@ -31,7 +29,7 @@ export class Promise {
|
||||
|
||||
constructor(private deferred: Deferred) { }
|
||||
|
||||
then(callback: Function, error: Function): Promise {
|
||||
then(callback: Function, error?: Function): Promise {
|
||||
return this.deferred.then(callback, error);
|
||||
}
|
||||
|
||||
|
@ -1,19 +1,16 @@
|
||||
Sample code:
|
||||
```
|
||||
var webClientModule = require("WebClient");
|
||||
var webClient = webClientModule.Client;
|
||||
var http = require("net").http;
|
||||
|
||||
var client = new webClient();
|
||||
|
||||
client.getString("http://www.reddit.com/", function(result) {
|
||||
http.getString("http://www.reddit.com/").then(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) {
|
||||
http.getJSON("http://www.reddit.com/r/aww.json?limit=10").then(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) {
|
||||
http.getImage("http://www.telerik.com/sfimages/default-source/Homepage/hp_any_approachf6e4079a7a99493a8ab2e367b9cb3f7d.png").then(function(result) {
|
||||
// Result is tk.ui.Image!
|
||||
}, function(e) { console.log("Error:" + e.message); });
|
||||
|
||||
|
@ -1,85 +1,74 @@
|
||||
/**
|
||||
* Android specific WebClient implementation.
|
||||
* Android specific http client implementation.
|
||||
*/
|
||||
|
||||
import image_module = require("Image/image");
|
||||
import app_module = require("Application/application");
|
||||
import promises = require("promises/promises");
|
||||
|
||||
export class HttpClient {
|
||||
export class http {
|
||||
/**
|
||||
* Downloads string from url.
|
||||
* Gets 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) {
|
||||
public static getString(url: string): promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
|
||||
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));
|
||||
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) {
|
||||
d.reject(e);
|
||||
return;
|
||||
}
|
||||
}, errorCallback);
|
||||
} catch (ex) {
|
||||
if (errorCallback) {
|
||||
errorCallback(ex);
|
||||
d.resolve(result);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* Gets JSON from url.
|
||||
*/
|
||||
public static getJSON(url: string): promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
|
||||
var image = new image_module.Image();
|
||||
image.loadFromBitmap(result);
|
||||
|
||||
successCallback(image);
|
||||
}
|
||||
}));
|
||||
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) {
|
||||
d.reject(e);
|
||||
return;
|
||||
}
|
||||
d.resolve(JSON.parse(result));
|
||||
}
|
||||
} catch (ex) {
|
||||
}));
|
||||
|
||||
if (errorCallback) {
|
||||
errorCallback(ex);
|
||||
}
|
||||
|
||||
}
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
|
||||
try {
|
||||
/**
|
||||
* Gets image from url.
|
||||
*/
|
||||
public static getImage(url: string): promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
|
||||
} catch (ex) {
|
||||
if (errorCallback) {
|
||||
errorCallback(ex);
|
||||
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) {
|
||||
d.reject(e);
|
||||
return;
|
||||
}
|
||||
|
||||
var image = new image_module.Image();
|
||||
image.loadFromBitmap(result);
|
||||
|
||||
d.resolve(image);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
}
|
||||
|
12
net/http_client.d.ts
vendored
12
net/http_client.d.ts
vendored
@ -1,11 +1,11 @@
|
||||
/**
|
||||
* The Client interface.
|
||||
* The http client interface.
|
||||
*/
|
||||
import image_module = require("Image/image");
|
||||
import promises = require("promises/promises");
|
||||
|
||||
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)
|
||||
export declare class http {
|
||||
private static getString(url: string): promises.Promise;
|
||||
private static getJSON(url: string): promises.Promise;
|
||||
private static getImage(url: string): promises.Promise;
|
||||
}
|
@ -1,70 +1,43 @@
|
||||
/**
|
||||
* iOS specific WebClient implementation.
|
||||
* iOS specific http client implementation.
|
||||
*/
|
||||
|
||||
import image_module = require("Image/image");
|
||||
import promises = require("promises/promises");
|
||||
|
||||
export class HttpClient {
|
||||
export class http {
|
||||
/**
|
||||
* Downloads string from url.
|
||||
* Gets 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) {
|
||||
public static getString(url : string) : promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
new HttpClient().getString(url, r => d.resolve(r), e => d.reject(e));
|
||||
http.get(url, r => d.resolve(Foundation.NSString.initWithDataEncoding(r, 4).toString()), 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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Gets JSON from url.
|
||||
*/
|
||||
public static getJSON(url: string) : promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
http.get(url, r => d.resolve(JSON.parse(Foundation.NSString.initWithDataEncoding(r, 4).toString())), e => d.reject(e));
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
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);
|
||||
/**
|
||||
* Gets image from url.
|
||||
*/
|
||||
public static getImage(url: string) : promises.Promise {
|
||||
var d = new promises.Deferred();
|
||||
http.get(url, r => {
|
||||
var image = new image_module.Image();
|
||||
image.loadFromData(r);
|
||||
d.resolve(image);
|
||||
}, e => d.reject(e));
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
private static get(url: string, successCallback?: (result: any) => void, errorCallback?: (e: Error) => void) {
|
||||
if (!successCallback && !errorCallback)
|
||||
{
|
||||
var d = new 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) {
|
||||
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
|
||||
try {
|
||||
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
|
||||
var queue = Foundation.NSOperationQueue.mainQueue();
|
||||
|
Reference in New Issue
Block a user