mirror of
https://github.com/HackPlan/node-iap.git
synced 2026-06-14 17:09:15 +08:00
initial implementation of google play platform
This commit is contained in:
10
lib/https/get.js
Normal file
10
lib/https/get.js
Normal file
@@ -0,0 +1,10 @@
|
||||
var https = require('./index');
|
||||
|
||||
|
||||
module.exports = function (url, options, cb) {
|
||||
options = options || {};
|
||||
|
||||
// Set method to GET and call it
|
||||
options.method = 'GET';
|
||||
https.request(url, options, null, cb);
|
||||
};
|
||||
3
lib/https/index.js
Normal file
3
lib/https/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
exports.request = require('./request');
|
||||
exports.post = require('./post');
|
||||
exports.get = require('./get');
|
||||
26
lib/https/post.js
Normal file
26
lib/https/post.js
Normal file
@@ -0,0 +1,26 @@
|
||||
var https = require('./index');
|
||||
var formUrlencoded = require('form-urlencoded');
|
||||
|
||||
|
||||
module.exports = function (url, options, cb) {
|
||||
options = options || {};
|
||||
|
||||
// Try and extract data and set it's type
|
||||
var data = null;
|
||||
try {
|
||||
if (options.form) {
|
||||
data = formUrlencoded.encode(options.form);
|
||||
delete options.form;
|
||||
options.headers = {
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
'content-length': Buffer.byteLength(data)
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
cb(error);
|
||||
}
|
||||
|
||||
// Set method to POST and call it
|
||||
options.method = 'POST';
|
||||
https.request(url, options, data, cb);
|
||||
};
|
||||
43
lib/https/request.js
Normal file
43
lib/https/request.js
Normal file
@@ -0,0 +1,43 @@
|
||||
var url = require('url');
|
||||
var https = require('https');
|
||||
|
||||
|
||||
module.exports = function (requestUrl, options, data, cb) {
|
||||
options = options || {};
|
||||
|
||||
var parsedUrl = url.parse(requestUrl);
|
||||
|
||||
if (parsedUrl.hostname) {
|
||||
options.hostname = parsedUrl.hostname;
|
||||
}
|
||||
|
||||
if (parsedUrl.port) {
|
||||
options.port = parsedUrl.port;
|
||||
}
|
||||
|
||||
if (parsedUrl.path) {
|
||||
options.path = parsedUrl.path;
|
||||
}
|
||||
|
||||
var req = https.request(options, function (res) {
|
||||
res.setEncoding('utf8');
|
||||
|
||||
var responseData = '';
|
||||
|
||||
res.on('data', function (str) {
|
||||
responseData += str;
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
if (res.statusCode !== 200) {
|
||||
return cb(new Error('Received ' + res.statusCode + ' status code with body: ' + responseData));
|
||||
}
|
||||
|
||||
cb(null, responseData);
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', cb);
|
||||
|
||||
req.end(data);
|
||||
};
|
||||
Reference in New Issue
Block a user