mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-20 07:26:11 +08:00
http client with promises for both Android and iOS
This commit is contained in:
@ -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