diff --git a/net/http_client.d.ts b/net/http_client.d.ts index 4ff9da2d0..27cddc358 100644 --- a/net/http_client.d.ts +++ b/net/http_client.d.ts @@ -8,4 +8,24 @@ export declare class http { static getString(url: string): promises.Promise; static getJSON(url: string): promises.Promise; static getImage(url: string): promises.Promise; + + 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; } \ No newline at end of file diff --git a/net/http_client.ios.ts b/net/http_client.ios.ts index ea700c402..7be07a94f 100644 --- a/net/http_client.ios.ts +++ b/net/http_client.ios.ts @@ -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 { var d = promises.defer(); - 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(url: string): promises.Promise { var d = promises.defer(); - 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 { var d = promises.defer(); - 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(); + 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); } } + } }