mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
universal request method for iOS
This commit is contained in:
20
net/http_client.d.ts
vendored
20
net/http_client.d.ts
vendored
@ -8,4 +8,24 @@ export declare class http {
|
||||
static getString(url: string): promises.Promise<string>;
|
||||
static getJSON<T>(url: string): promises.Promise<T>;
|
||||
static getImage(url: string): promises.Promise<image_module.Image>;
|
||||
|
||||
static request(options: IHttpRequestOptions, successCallback: (r: IHttpResponse) => void, errorCallback?: (e: Error) => void);
|
||||
}
|
||||
|
||||
export interface IHttpRequestOptions {
|
||||
url: string;
|
||||
method: string;
|
||||
headers?: IHttpHeader[];
|
||||
body?: string;
|
||||
}
|
||||
|
||||
export interface IHttpHeader {
|
||||
name: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface IHttpResponse {
|
||||
statusCode: number;
|
||||
headers: IHttpHeader[];
|
||||
body: any;
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* iOS specific http client implementation.
|
||||
*/
|
||||
|
||||
import http_client = require("net/http_client");
|
||||
import image_module = require("Image/image");
|
||||
import promises = require("promises/promises");
|
||||
|
||||
@ -11,7 +11,11 @@ export class http {
|
||||
*/
|
||||
public static getString(url: string): promises.Promise<string> {
|
||||
var d = promises.defer<string>();
|
||||
http.get(url, r => d.resolve(Foundation.NSString.initWithDataEncoding(r, 4).toString()), e => d.reject(e));
|
||||
|
||||
http.request({ url: url, method: "GET" },
|
||||
r => d.resolve(Foundation.NSString.initWithDataEncoding(r.body, 4).toString()),
|
||||
e => d.reject(e));
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
@ -20,7 +24,11 @@ export class http {
|
||||
*/
|
||||
public static getJSON<T>(url: string): promises.Promise<T> {
|
||||
var d = promises.defer<T>();
|
||||
http.get(url, r => d.resolve(JSON.parse(Foundation.NSString.initWithDataEncoding(r, 4).toString())), e => d.reject(e));
|
||||
|
||||
http.request({ url: url, method: "GET" },
|
||||
r => d.resolve(JSON.parse(Foundation.NSString.initWithDataEncoding(r.body, 4).toString())),
|
||||
e => d.reject(e));
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
@ -29,57 +37,71 @@ export class http {
|
||||
*/
|
||||
public static getImage(url: string): promises.Promise<image_module.Image> {
|
||||
var d = promises.defer<image_module.Image>();
|
||||
http.get(url, r => {
|
||||
var image = new image_module.Image();
|
||||
image.loadFromData(r);
|
||||
d.resolve(image);
|
||||
}, e => d.reject(e));
|
||||
|
||||
http.request({ url: url, method: "GET" },
|
||||
r => {
|
||||
var image = new image_module.Image();
|
||||
image.loadFromData(r.body);
|
||||
d.resolve(image);
|
||||
},
|
||||
e => d.reject(e));
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
|
||||
private static request(options: http_client.IHttpRequestOptions,
|
||||
successCallback: (r: http_client.IHttpResponse) => 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);
|
||||
}
|
||||
});
|
||||
var session = Foundation.NSURLSession.sessionWithConfigurationDelegateDelegateQueue(
|
||||
sessionConfig, null, queue);
|
||||
|
||||
dataTask.resume();
|
||||
session.finishTasksAndInvalidate();
|
||||
} catch (ex) {
|
||||
if (errorCallback) {
|
||||
errorCallback(ex);
|
||||
var urlRequest = Foundation.NSMutableURLRequest.requestWithURL(
|
||||
Foundation.NSURL.URLWithString(options.url));
|
||||
|
||||
urlRequest.setHTTPMethod(options.method);
|
||||
|
||||
if (options.headers && options.headers.length) {
|
||||
for (var i = 0, l = options.headers.length; i < l; i++) {
|
||||
var header = options.headers[i];
|
||||
|
||||
urlRequest.setValueForHTTPHeaderField(header.name, header.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if (typeof (options.body) == "string") {
|
||||
urlRequest.setHTTPBody(Foundation.NSString.initWithString(options.body).dataUsingEncoding(4));
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest, function (data, response, error) {
|
||||
if (error) {
|
||||
if (errorCallback) {
|
||||
errorCallback(new Error(error.localizedDescription()));
|
||||
var headers = new Array<http_client.IHttpHeader>();
|
||||
var headerFields = response.allHeaderFields();
|
||||
var keys = headerFields.allKeys();
|
||||
|
||||
for (var i = 0, l = keys.count(); i < l; i++) {
|
||||
var key = keys.objectAtIndex(i);
|
||||
|
||||
headers.push({ name: key, value: headerFields.valueForKey(key) });
|
||||
}
|
||||
|
||||
successCallback(
|
||||
{
|
||||
body: data,
|
||||
statusCode: response.statusCode,
|
||||
headers: headers
|
||||
});
|
||||
}
|
||||
} else if (successCallback) {
|
||||
successCallback(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
dataTask.resume();
|
||||
} catch (ex) {
|
||||
@ -87,5 +109,6 @@ export class http {
|
||||
errorCallback(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user