Skip to content

Commit 1bf9c1e

Browse files
author
Vladimir Enchev
committed
http client reworked (temporary getString, getJSON and getImage in separate file)
1 parent 24e8179 commit 1bf9c1e

6 files changed

Lines changed: 185 additions & 156 deletions

File tree

BCL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<TypeScriptCompile Include="android17.d.ts" />
149149
<TypeScriptCompile Include="promises\promises.ts" />
150150
<TypeScriptCompile Include="promises\index.ts" />
151+
<TypeScriptCompile Include="net\http_common.ts" />
151152
<Content Include="_references.ts" />
152153
<TypeScriptCompile Include="Console\console.android.ts">
153154
<DependentUpon>console.d.ts</DependentUpon>

declarations.android.d.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ declare module com {
346346

347347
export module ion {
348348
export class Ion {
349-
static with(context: any, url : string) : any;
349+
static with(context: any, url: string): any;
350+
static getDefault(context: any): any;
350351
}
351352
}
352353

@@ -356,6 +357,26 @@ declare module com {
356357
constructor(context: any);
357358
}
358359
}
360+
361+
export module http {
362+
363+
export module libcore {
364+
export class RawHeaders {
365+
constructor();
366+
add(name: string, v: string);
367+
}
368+
}
369+
370+
export class AsyncHttpClient {
371+
static getDefaultInstance(): any;
372+
}
373+
374+
export module callback {
375+
export class HttpConnectCallback {
376+
constructor(params: any);
377+
}
378+
}
379+
}
359380
}
360381
}
361382
}

net/http_client.android.ts

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,61 @@
11
/**
22
* Android specific http client implementation.
33
*/
4-
54
import image_module = require("Image/image");
65
import app_module = require("Application/application");
76
import promises = require("promises/promises");
87

9-
export class http {
10-
/**
11-
* Gets string from url.
12-
*/
13-
public static getString(url: string): promises.Promise<string> {
14-
var d = promises.defer<string>();
15-
http.get(url, r => d.resolve(r), e => d.reject(e));
16-
return d.promise();
17-
}
18-
19-
/**
20-
* Gets JSON from url.
21-
*/
22-
public static getJSON<T>(url: string): promises.Promise<T> {
23-
var d = promises.defer<T>();
24-
http.get(url, r => d.resolve(JSON.parse(r)), e => d.reject(e));
25-
return d.promise();
26-
}
8+
import http = require("net/http_client");
279

28-
/**
29-
* Gets image from url.
30-
*/
31-
public static getImage(url: string): promises.Promise<image_module.Image> {
32-
var d = promises.defer<image_module.Image>();
33-
http.get(url, r => {
34-
var image = new image_module.Image();
35-
image.loadFromNativeBitmap(r);
36-
d.resolve(image);
37-
}, e => d.reject(e));
38-
return d.promise();
39-
}
10+
/*
11+
// merge common
12+
import http_common = require("net/http_common");
13+
declare var exports;
14+
exports.getString = http_common.getString;
15+
exports.getJSON = http_common.getJSON;
16+
exports.getImage = http_common.getImage;
17+
*/
4018

41-
// TODO: Accept: application/json header for JSON calls and check the response for Image not url!
42-
private static get(url: string, successCallback: (result: any) => void, errorCallback?: (e: Error) => void) {
43-
try {
44-
var isImage = url.match(/\.(jpeg|jpg|gif|png)$/i) != null;
19+
// TODO: Replace with similar to iOS implementation!
20+
export function request(options: http.HttpRequestOptions): promises.Promise<http.HttpResponse> {
21+
var d = promises.defer<http.HttpResponse>();
4522

46-
var context = app_module.Application.current.android.context;
47-
var request = com.koushikdutta.ion.Ion.with(context, url);
23+
try {
24+
var headers = new com.koushikdutta.async.http.libcore.RawHeaders();
4825

49-
request = isImage ? request.asBitmap() : request.asString();
26+
if (options.headers && options.headers.length) {
27+
for (var i = 0, l = options.headers.length; i < l; i++) {
28+
var header = options.headers[i];
29+
headers.add(header.name, header.value)
30+
}
31+
}
5032

51-
request.setCallback(new com.koushikdutta.async.future.FutureCallback({
52-
onCompleted: function (error, data) {
53-
if (error && errorCallback) {
54-
errorCallback(new Error(error.toString()));
55-
} else if (successCallback) {
56-
successCallback(data);
57-
}
33+
var isImage = options.url.match(/\.(jpeg|jpg|gif|png)$/i) != null;
34+
35+
var context = app_module.Application.current.android.context;
36+
var request = com.koushikdutta.ion.Ion.with(context, options.url);
37+
38+
request = isImage ? request.asBitmap() : request.asString();
39+
40+
request.setCallback(new com.koushikdutta.async.future.FutureCallback({
41+
onCompleted: function (error, data) {
42+
if (error) {
43+
d.reject(error);
44+
} else {
45+
d.resolve({
46+
body: {
47+
toString: () => { return data },
48+
toJSON: () => { return JSON.parse(data) },
49+
toImage: () => { return image_module.Image.imageFromNativeBitmap(data); }
50+
},
51+
statusCode: 0,
52+
headers: []
53+
});
5854
}
59-
}));
60-
} catch (ex) {
61-
if (errorCallback) {
62-
errorCallback(ex);
6355
}
64-
}
56+
}));
57+
} catch (ex) {
58+
d.reject(ex);
6559
}
60+
return d.promise();
6661
}

net/http_client.d.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,32 @@
44
import image_module = require("Image/image");
55
import promises = require("promises/promises");
66

7-
export declare class http {
8-
static getString(url: string): promises.Promise<string>;
9-
static getJSON<T>(url: string): promises.Promise<T>;
10-
static getImage(url: string): promises.Promise<image_module.Image>;
7+
export declare function getString(url: string): promises.Promise<string>;
8+
export declare function getJSON<T>(url: string): promises.Promise<T>;
9+
export declare function getImage(url: string): promises.Promise<image_module.Image>;
1110

12-
static request(options: IHttpRequestOptions, successCallback: (r: IHttpResponse) => void, errorCallback?: (e: Error) => void);
13-
}
11+
export declare function request(options: HttpRequestOptions): promises.Promise<HttpResponse>;
1412

15-
export interface IHttpRequestOptions {
13+
export interface HttpRequestOptions {
1614
url: string;
1715
method: string;
18-
headers?: IHttpHeader[];
19-
body?: string;
16+
headers?: HttpHeader[];
17+
body?: any;
2018
}
2119

22-
export interface IHttpHeader {
20+
export interface HttpHeader {
2321
name: string;
2422
value: string;
2523
}
2624

27-
export interface IHttpResponse {
25+
export interface HttpResponse {
2826
statusCode: number;
29-
headers: IHttpHeader[];
30-
body: any;
27+
headers: HttpHeader[];
28+
body: HttpContent;
29+
}
30+
31+
export interface HttpContent {
32+
toString: () => string;
33+
toJSON: () => any;
34+
toImage: () => image_module.Image;
3135
}

net/http_client.ios.ts

Lines changed: 58 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,80 @@
11
/**
22
* iOS specific http client implementation.
33
*/
4-
import http_client = require("net/http_client");
5-
import image_module = require("Image/image");
4+
import image = require("Image/image");
65
import promises = require("promises/promises");
6+
import http = require("net/http_client");
77

8-
export class http {
9-
/**
10-
* Gets string from url.
11-
*/
12-
public static getString(url: string): promises.Promise<string> {
13-
var d = promises.defer<string>();
8+
/*
9+
// merge common
10+
import http_common = require("net/http_common");
11+
declare var exports;
12+
exports.getString = http_common.getString;
13+
exports.getJSON = http_common.getJSON;
14+
exports.getImage = http_common.getImage;
15+
*/
1416

15-
http.request({ url: url, method: "GET" },
16-
r => d.resolve(Foundation.NSString.initWithDataEncoding(r.body, 4).toString()),
17-
e => d.reject(e));
17+
export function request(options: http.HttpRequestOptions): promises.Promise<http.HttpResponse> {
18+
var d = promises.defer<http.HttpResponse>();
1819

19-
return d.promise();
20-
}
21-
22-
/**
23-
* Gets JSON from url.
24-
*/
25-
public static getJSON<T>(url: string): promises.Promise<T> {
26-
var d = promises.defer<T>();
27-
28-
http.request({ url: url, method: "GET" },
29-
r => d.resolve(JSON.parse(Foundation.NSString.initWithDataEncoding(r.body, 4).toString())),
30-
e => d.reject(e));
31-
32-
return d.promise();
33-
}
34-
35-
/**
36-
* Gets image from url.
37-
*/
38-
public static getImage(url: string): promises.Promise<image_module.Image> {
39-
var d = promises.defer<image_module.Image>();
40-
41-
http.request({ url: url, method: "GET" },
42-
r => {
43-
var image = new image_module.Image();
44-
image.loadFromData(r.body);
45-
d.resolve(image);
46-
},
47-
e => d.reject(e));
48-
49-
return d.promise();
50-
}
51-
52-
private static request(options: http_client.IHttpRequestOptions,
53-
successCallback: (r: http_client.IHttpResponse) => void,
54-
errorCallback?: (e: Error) => void) {
55-
56-
try {
57-
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
58-
var queue = Foundation.NSOperationQueue.mainQueue();
59-
var session = Foundation.NSURLSession.sessionWithConfigurationDelegateDelegateQueue(
60-
sessionConfig, null, queue);
20+
try {
21+
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
22+
var queue = Foundation.NSOperationQueue.mainQueue();
23+
var session = Foundation.NSURLSession.sessionWithConfigurationDelegateDelegateQueue(
24+
sessionConfig, null, queue);
6125

62-
var urlRequest = Foundation.NSMutableURLRequest.requestWithURL(
63-
Foundation.NSURL.URLWithString(options.url));
26+
var urlRequest = Foundation.NSMutableURLRequest.requestWithURL(
27+
Foundation.NSURL.URLWithString(options.url));
6428

65-
urlRequest.setHTTPMethod(options.method);
29+
urlRequest.setHTTPMethod(options.method);
6630

67-
if (options.headers && options.headers.length) {
68-
for (var i = 0, l = options.headers.length; i < l; i++) {
69-
var header = options.headers[i];
31+
if (options.headers && options.headers.length) {
32+
for (var i = 0, l = options.headers.length; i < l; i++) {
33+
var header = options.headers[i];
7034

71-
urlRequest.setValueForHTTPHeaderField(header.name, header.value);
72-
}
73-
}
74-
75-
if (typeof (options.body) == "string") {
76-
urlRequest.setHTTPBody(Foundation.NSString.initWithString(options.body).dataUsingEncoding(4));
35+
urlRequest.setValueForHTTPHeaderField(header.name, header.value);
7736
}
37+
}
7838

79-
var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest,
80-
function (data, response, error) {
81-
if (error) {
82-
if (errorCallback) {
83-
errorCallback(new Error(error.localizedDescription()));
84-
}
85-
} else if (successCallback) {
39+
if (typeof (options.body) == "string") {
40+
urlRequest.setHTTPBody(Foundation.NSString.initWithString(options.body).dataUsingEncoding(4));
41+
}
8642

87-
var headers = new Array<http_client.IHttpHeader>();
88-
var headerFields = response.allHeaderFields();
89-
var keys = headerFields.allKeys();
43+
var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest,
44+
function (data, response, error) {
45+
if (error) {
46+
d.reject(error);
47+
} else {
9048

91-
for (var i = 0, l = keys.count(); i < l; i++) {
92-
var key = keys.objectAtIndex(i);
49+
var headers = new Array<http.HttpHeader>();
50+
var headerFields = response.allHeaderFields();
51+
var keys = headerFields.allKeys();
9352

94-
headers.push({ name: key, value: headerFields.valueForKey(key) });
95-
}
53+
for (var i = 0, l = keys.count(); i < l; i++) {
54+
var key = keys.objectAtIndex(i);
9655

97-
successCallback(
98-
{
99-
body: data,
100-
statusCode: response.statusCode,
101-
headers: headers
102-
});
56+
headers.push({ name: key, value: headerFields.valueForKey(key) });
10357
}
104-
});
10558

106-
dataTask.resume();
107-
} catch (ex) {
108-
if (errorCallback) {
109-
errorCallback(ex);
110-
}
111-
}
59+
d.resolve({
60+
body: {
61+
toString: () => { return NSDataToString(data); },
62+
toJSON: () => { return JSON.parse(NSDataToString(data)); },
63+
toImage: () => { return image.Image.imageFromData(data); }
64+
},
65+
statusCode: response.statusCode,
66+
headers: headers
67+
});
68+
}
69+
});
11270

71+
dataTask.resume();
72+
} catch (ex) {
73+
d.reject(ex);
11374
}
75+
return d.promise();
76+
}
77+
78+
function NSDataToString(data: any): string {
79+
return Foundation.NSString.initWithDataEncoding(data, 4).toString();
11480
}

0 commit comments

Comments
 (0)