mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
net folder renamed to http
This commit is contained in:
84
http/Readme.md
Normal file
84
http/Readme.md
Normal file
@@ -0,0 +1,84 @@
|
||||
Sample code:
|
||||
```js
|
||||
var http = require("http");
|
||||
|
||||
// Universal request method. You can use HttpRequestOptions to set varios properties like url, headers, etc.,
|
||||
// HttpResponse to get status code, headers and content and HttpContent to get body of response:
|
||||
|
||||
interface HttpRequestOptions {
|
||||
url: string;
|
||||
method: string;
|
||||
headers?: any;
|
||||
content?: HttpContent;
|
||||
}
|
||||
|
||||
interface HttpResponse {
|
||||
statusCode: number;
|
||||
headers: any;
|
||||
content?: HttpContent;
|
||||
}
|
||||
|
||||
interface HttpContent {
|
||||
toString: () => string;
|
||||
toJSON: () => any;
|
||||
toImage: () => image_module.Image;
|
||||
}
|
||||
|
||||
// GET request
|
||||
http.request({
|
||||
url: "http://ip.jsontest.com/",
|
||||
method: "GET",
|
||||
headers: { "Content-Type" : "application/json" }
|
||||
}).then(function (r) {
|
||||
var status = r.statusCode;
|
||||
|
||||
for (var header in r.headers) {
|
||||
//
|
||||
}
|
||||
|
||||
var result = r.content.toJSON();
|
||||
}).fail(function (e) { console.log(e) });
|
||||
|
||||
// POST request
|
||||
http.request({
|
||||
url: "http://posttestserver.com/post.php?dump&html&dir=test",
|
||||
method: "POST",
|
||||
headers: { "Content-Type" : "application/x-www-form-urlencoded" },
|
||||
content: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
|
||||
}).then(function (r) {
|
||||
var status = r.statusCode;
|
||||
|
||||
for (var header in r.headers) {
|
||||
//
|
||||
}
|
||||
|
||||
var result = r.content.toString();
|
||||
}).fail(function (e) { console.log(e) });
|
||||
|
||||
// PUT request
|
||||
var data = YOUR_IMAGE_DATA;
|
||||
http.request({
|
||||
url: "http://httpbin.org/put",
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "image/jpg",
|
||||
"Content-Length": data.length() + ""
|
||||
},
|
||||
content: data
|
||||
}).then(function (r) {
|
||||
console.log(r.content.toString())
|
||||
}).fail(function (e) { console.log(e) });
|
||||
|
||||
http.getString("http://www.reddit.com/").then(function(result) {
|
||||
// Result is string!
|
||||
}).fail(function(e) { console.log(e); });
|
||||
|
||||
http.getJSON("http://www.reddit.com/r/aww.json?limit=10").then(function(result) {
|
||||
// Result is JSON!
|
||||
}).fail(function(e) { console.log(e); });
|
||||
|
||||
http.getImage("http://www.google.com/images/errors/logo_sm_2.png").then(function(result) {
|
||||
// Result is tk.ui.Image!
|
||||
}).fail(function(e) { console.log(e); });
|
||||
|
||||
```
|
||||
46
http/http.ts
Normal file
46
http/http.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import image_module = require("Image/image");
|
||||
import promises = require("promises/promises");
|
||||
import http = require("http/http_request");
|
||||
|
||||
// merge request
|
||||
declare var exports;
|
||||
exports.request = http.request;
|
||||
|
||||
/**
|
||||
* Gets string from url.
|
||||
*/
|
||||
export function getString(url: string): promises.Promise<string> {
|
||||
var d = promises.defer<string>();
|
||||
|
||||
http.request({ url: url, method: "GET" })
|
||||
.then(r => d.resolve(r.content.toString()))
|
||||
.fail(e => d.reject(e));
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets JSON from url.
|
||||
*/
|
||||
export function getJSON<T>(url: string): promises.Promise<T> {
|
||||
var d = promises.defer<T>();
|
||||
|
||||
http.request({ url: url, method: "GET" })
|
||||
.then(r => d.resolve(r.content.toJSON()))
|
||||
.fail(e => d.reject(e));
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets image from url.
|
||||
*/
|
||||
export function getImage(url: string): promises.Promise<image_module.Image> {
|
||||
var d = promises.defer<image_module.Image>();
|
||||
|
||||
http.request({ url: url, method: "GET" })
|
||||
.then(r => d.resolve(r.content.toImage()))
|
||||
.fail(e => d.reject(e));
|
||||
|
||||
return d.promise();
|
||||
}
|
||||
49
http/http_request.android.ts
Normal file
49
http/http_request.android.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Android specific http client implementation.
|
||||
*/
|
||||
import promises = require("promises/promises");
|
||||
import http = require("http/http_request");
|
||||
|
||||
// TODO: Replace with similar to iOS implementation!
|
||||
export function request(options: http.HttpRequestOptions): promises.Promise<http.HttpResponse> {
|
||||
var d = promises.defer<http.HttpResponse>();
|
||||
|
||||
try {
|
||||
var headers = new com.koushikdutta.async.http.libcore.RawHeaders();
|
||||
|
||||
if (options.headers) {
|
||||
for (var key in options.headers) {
|
||||
headers.add(key, options.headers[key])
|
||||
}
|
||||
}
|
||||
|
||||
var isImage = options.url.match(/\.(jpeg|jpg|gif|png)$/i) != null;
|
||||
|
||||
var context = require("Application/application").Application.current.android.context;
|
||||
var request = com.koushikdutta.ion.Ion.with(context, options.url);
|
||||
|
||||
request = isImage ? request.asBitmap() : request.asString();
|
||||
|
||||
request.setCallback(new com.koushikdutta.async.future.FutureCallback({
|
||||
onCompleted: function (error, data) {
|
||||
if (error) {
|
||||
d.reject(error);
|
||||
} else {
|
||||
d.resolve({
|
||||
content: {
|
||||
raw: data,
|
||||
toString: () => { return data },
|
||||
toJSON: () => { return JSON.parse(data) },
|
||||
toImage: () => { return require("Image/image").Image.imageFromNativeBitmap(data); }
|
||||
},
|
||||
statusCode: 0,
|
||||
headers: {}
|
||||
});
|
||||
}
|
||||
}
|
||||
}));
|
||||
} catch (ex) {
|
||||
d.reject(ex);
|
||||
}
|
||||
return d.promise();
|
||||
}
|
||||
27
http/http_request.d.ts
vendored
Normal file
27
http/http_request.d.ts
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* The http client interface.
|
||||
*/
|
||||
import image_module = require("Image/image");
|
||||
import promises = require("promises/promises");
|
||||
|
||||
export declare function request(options: HttpRequestOptions): promises.Promise<HttpResponse>;
|
||||
|
||||
export interface HttpRequestOptions {
|
||||
url: string;
|
||||
method: string;
|
||||
headers?: any;
|
||||
content?: any;
|
||||
}
|
||||
|
||||
export interface HttpResponse {
|
||||
statusCode: number;
|
||||
headers: any;
|
||||
content?: HttpContent;
|
||||
}
|
||||
|
||||
export interface HttpContent {
|
||||
raw: any;
|
||||
toString: () => string;
|
||||
toJSON: () => any;
|
||||
toImage: () => image_module.Image;
|
||||
}
|
||||
72
http/http_request.ios.ts
Normal file
72
http/http_request.ios.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* iOS specific http client implementation.
|
||||
*/
|
||||
import promises = require("promises/promises");
|
||||
import http = require("http/http_request");
|
||||
|
||||
export function request(options: http.HttpRequestOptions): promises.Promise<http.HttpResponse> {
|
||||
var d = promises.defer<http.HttpResponse>();
|
||||
|
||||
try {
|
||||
var sessionConfig = Foundation.NSURLSessionConfiguration.defaultSessionConfiguration();
|
||||
var queue = Foundation.NSOperationQueue.mainQueue();
|
||||
var session = Foundation.NSURLSession.sessionWithConfigurationDelegateDelegateQueue(
|
||||
sessionConfig, null, queue);
|
||||
|
||||
var urlRequest = Foundation.NSMutableURLRequest.requestWithURL(
|
||||
Foundation.NSURL.URLWithString(options.url));
|
||||
|
||||
if (options.method) {
|
||||
urlRequest.setHTTPMethod(options.method);
|
||||
}
|
||||
|
||||
if (options.headers) {
|
||||
for (var header in options.headers) {
|
||||
urlRequest.setValueForHTTPHeaderField(options.headers[header], header);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof options.content == "string") {
|
||||
urlRequest.setHTTPBody(Foundation.NSString.initWithString(options.content).dataUsingEncoding(4));
|
||||
}
|
||||
else {
|
||||
urlRequest.setHTTPBody(options.content);
|
||||
}
|
||||
|
||||
var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest,
|
||||
function (data, response, error) {
|
||||
if (error) {
|
||||
d.reject(new Error(error.localizedDescription()));
|
||||
} else {
|
||||
var headers = {};
|
||||
var headerFields = response.allHeaderFields();
|
||||
var keys = headerFields.allKeys();
|
||||
|
||||
for (var i = 0, l = keys.count(); i < l; i++) {
|
||||
var key = keys.objectAtIndex(i);
|
||||
headers[key] = headerFields.valueForKey(key);
|
||||
}
|
||||
|
||||
d.resolve({
|
||||
content: {
|
||||
raw: data,
|
||||
toString: () => { return NSDataToString(data); },
|
||||
toJSON: () => { return JSON.parse(NSDataToString(data)); },
|
||||
toImage: () => { return require("Image/image").Image.imageFromData(data); }
|
||||
},
|
||||
statusCode: response.statusCode(),
|
||||
headers: headers
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
dataTask.resume();
|
||||
} catch (ex) {
|
||||
d.reject(ex);
|
||||
}
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
function NSDataToString(data: any): string {
|
||||
return Foundation.NSString.initWithDataEncoding(data, 4).toString();
|
||||
}
|
||||
2
http/index.ts
Normal file
2
http/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
declare var module, require;
|
||||
module.exports = require("http/http");
|
||||
Reference in New Issue
Block a user