Added unified interface for HTTP requests with pre/post hooks

This commit is contained in:
Alexander Corn 2016-03-04 18:26:47 -05:00
parent 574a3127cb
commit eecd682033
15 changed files with 159 additions and 81 deletions

View File

@ -7,7 +7,7 @@ SteamCommunity.prototype.getMarketItem = function(appid, hashName, currency, cal
currency = 1;
}
var self = this;
this.request("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) {
this.httpRequest("https://steamcommunity.com/market/listings/" + appid + "/" + encodeURIComponent(hashName), function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@ -103,9 +103,9 @@ CMarketItem.prototype.updatePriceForCommodity = function(currency, callback) {
}
var self = this;
this._community.request({
this._community.httpRequest({
"uri": "https://steamcommunity.com/market/itemordershistogram?country=US&language=english&currency=" + currency + "&item_nameid=" + this.commodityID,
"json": true,
"json": true
}, function(err, response, body) {
if(self._community._checkHttpError(err, response, callback)) {
return;
@ -146,7 +146,7 @@ CMarketItem.prototype.updatePriceForNonCommodity = function (currency, callback)
}
var self = this;
this._community.request({
this._community.httpRequest({
"uri": "https://steamcommunity.com/market/listings/" +
this._appid + "/" +
encodeURIComponent(this._hashName) +

View File

@ -27,7 +27,7 @@ SteamCommunity.prototype.marketSearch = function(options, callback) {
qs.count = 100;
qs.sort_column = 'price';
qs.sort_dir = 'asc';
performSearch.call(this, this.request, qs, [], callback);
performSearch.call(this, this.httpRequest, qs, [], callback);
};
function performSearch(request, qs, results, callback) {

View File

@ -12,7 +12,7 @@ SteamCommunity.prototype.getSteamGroup = function(id, callback) {
}
var self = this;
this.request("https://steamcommunity.com/" + (typeof id === 'string' ? "groups/" + id : "gid/" + id.toString()) + "/memberslistxml/?xml=1", function(err, response, body) {
this.httpRequest("https://steamcommunity.com/" + (typeof id === 'string' ? "groups/" + id : "gid/" + id.toString()) + "/memberslistxml/?xml=1", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}

View File

@ -12,7 +12,7 @@ SteamCommunity.prototype.getSteamUser = function(id, callback) {
}
var self = this;
this.request("http://steamcommunity.com/" + (typeof id === 'string' ? "id/" + id : "profiles/" + id.toString()) + "/?xml=1", function(err, response, body) {
this.httpRequest("http://steamcommunity.com/" + (typeof id === 'string' ? "id/" + id : "profiles/" + id.toString()) + "/?xml=1", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}

View File

@ -48,7 +48,7 @@ SteamCommunity.prototype.chatLogon = function(interval, uiMode) {
return;
}
self.request.post({
self.httpRequestPost({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logon/v1",
"form": {
"ui_mode": uiMode,
@ -103,7 +103,7 @@ SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback)
type = type || 'saytext';
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Message/v1",
"form": {
"access_token": this._chat.accessToken,
@ -132,7 +132,7 @@ SteamCommunity.prototype.chatMessage = function(recipient, text, type, callback)
SteamCommunity.prototype.chatLogoff = function() {
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Logoff/v1",
"form": {
"access_token": this._chat.accessToken,
@ -156,7 +156,7 @@ SteamCommunity.prototype._chatPoll = function() {
this.emit('debug', 'Doing chat poll');
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://api.steampowered.com/ISteamWebUserPresenceOAuth/Poll/v1",
"form": {
"umqid": self._chat.umqid,
@ -217,7 +217,7 @@ SteamCommunity.prototype._chatPoll = function() {
SteamCommunity.prototype._chatUpdatePersona = function(steamID) {
this.emit('debug', 'Updating persona data for ' + steamID);
var self = this;
this.request({
this.httpRequest({
"uri": "https://steamcommunity.com/chat/friendstate/" + steamID.accountid,
"json": true
}, function(err, response, body) {

View File

@ -148,7 +148,7 @@ function request(community, url, key, time, tag, params, json, callback) {
params.m = "android";
params.tag = tag;
community.request.get({
community.httpRequestGet({
"uri": "https://steamcommunity.com/mobileconf/" + url,
"qs": params,
"json": !!json

View File

@ -38,7 +38,7 @@ SteamCommunity.prototype.getGroupMembers = function(gid, callback, members, link
}
var self = this;
this.request(options, function(err, response, body) {
this.httpRequest(options, function(err, response, body) {
if (self._checkHttpError(err, response, callback)) {
return;
}
@ -73,7 +73,7 @@ SteamCommunity.prototype.joinGroup = function(gid, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64(),
"form": {
"action": "join",
@ -136,7 +136,7 @@ SteamCommunity.prototype.getAllGroupAnnouncements = function(gid, time, callback
}
var self = this;
this.request({
this.httpRequest({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/rss/"
}, function(err, response, body) {
@ -181,7 +181,7 @@ SteamCommunity.prototype.postGroupAnnouncement = function(gid, headline, content
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/announcements",
"form": {
"sessionID": this.getSessionID(),
@ -229,7 +229,7 @@ SteamCommunity.prototype.editGroupAnnouncement = function(gid, aid, headline, co
}
}
this.request.post(submitData, function(err, response, body) {
this.httpRequestPost(submitData, function(err, response, body) {
if(!callback) {
return;
}
@ -257,7 +257,7 @@ SteamCommunity.prototype.deleteGroupAnnouncement = function(gid, aid, callback)
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/announcements/delete/" + aid + "?sessionID=" + this.getSessionID(),
}
this.request.get(submitData, function(err, response, body) {
this.httpRequestGet(submitData, function(err, response, body) {
if(!callback) {
return;
}
@ -319,7 +319,7 @@ SteamCommunity.prototype.scheduleGroupEvent = function(gid, name, type, descript
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.toString() + "/eventEdit",
"form": form
}, function(err, response, body) {
@ -350,7 +350,7 @@ SteamCommunity.prototype.setGroupPlayerOfTheWeek = function(gid, steamID, callba
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/potwEdit",
"form": {
"xml": 1,
@ -393,7 +393,7 @@ SteamCommunity.prototype.kickGroupMember = function(gid, steamID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/gid/" + gid.getSteamID64() + "/membersManage",
"form": {
"sessionID": this.getSessionID(),
@ -430,7 +430,7 @@ SteamCommunity.prototype.getGroupHistory = function(gid, page, callback) {
}
var self = this;
this.request("https://steamcommunity.com/gid/" + gid.getSteamID64() + "/history?p=" + page, function(err, response, body) {
this.httpRequest("https://steamcommunity.com/gid/" + gid.getSteamID64() + "/history?p=" + page, function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}

95
components/http.js Normal file
View File

@ -0,0 +1,95 @@
var SteamCommunity = require('../index.js');
SteamCommunity.prototype.httpRequest = function(uri, options, callback, method) {
if (typeof uri === 'object') {
callback = options;
options = uri;
uri = options.url || options.uri;
}
options.url = options.uri = uri;
if (this._httpRequestConvenienceMethod) {
options.method = this._httpRequestConvenienceMethod;
delete this._httpRequestConvenienceMethod;
}
if (this.onPreHttpRequest && this.onPreHttpRequest(options, callback) === false) {
return false;
}
var self = this;
this.request(options, function(err, response, body) {
var hasCallback = !!callback;
var httpError = options.checkHttpError !== false && self._checkHttpError(err, response, callback);
var communityError = !options.json && options.checkCommunityError !== false && self._checkCommunityError(body, httpError ? function() {} : callback); // don't fire the callback if hasHttpError did it already
var tradeError = !options.json && options.checkTradeError !== false && self._checkTradeError(body, httpError || communityError ? function() {} : callback); // don't fire the callback if either of the previous already did
self.emit('postHttpRequest', httpError || communityError || tradeError || null, response, body, {
"hasCallback": hasCallback,
"httpError": httpError,
"communityError": communityError,
"tradeError": tradeError
});
if (hasCallback && !(httpError || communityError || tradeError)) {
callback.apply(self, arguments);
}
});
return true;
};
SteamCommunity.prototype.httpRequestGet = function() {
this._httpRequestConvenienceMethod = "GET";
return this.httpRequest.apply(this, arguments);
};
SteamCommunity.prototype.httpRequestPost = function() {
this._httpRequestConvenienceMethod = "POST";
return this.httpRequest.apply(this, arguments);
};
SteamCommunity.prototype._checkHttpError = function(err, response, callback) {
if(err) {
callback(err);
return err;
}
if(response.statusCode >= 300 && response.statusCode <= 399 && response.headers.location.indexOf('/login') != -1) {
err = new Error("Not Logged In");
callback(err);
return err;
}
if(response.statusCode >= 400) {
err = new Error("HTTP error " + response.statusCode);
err.code = response.statusCode;
callback(err);
return err;
}
return false;
};
SteamCommunity.prototype._checkCommunityError = function(html, callback) {
if(html.match(/<h1>Sorry!<\/h1>/)) {
var match = html.match(/<h3>(.+)<\/h3>/);
var err = new Error(match ? match[1] : "Unknown error occurred");
callback(err);
return err;
}
return false;
};
SteamCommunity.prototype._checkTradeError = function(html, callback) {
var match = html.match(/<div id="error_msg">\s*([^<]+)\s*<\/div>/);
if (match) {
var err = new Error(match[1].trim());
callback(new Error(err));
return err;
}
return false;
};

View File

@ -13,7 +13,7 @@ SteamCommunity.prototype.getInventoryHistory = function(options, callback) {
options.page = options.page || 1;
this.request("https://steamcommunity.com/my/inventoryhistory?l=english&p=" + options.page, function(err, response, body) {
this.httpRequest("https://steamcommunity.com/my/inventoryhistory?l=english&p=" + options.page, function(err, response, body) {
if(err) {
callback(err);
return;

View File

@ -3,7 +3,7 @@ var Cheerio = require('cheerio');
SteamCommunity.prototype.getMarketApps = function(callback) {
var self = this;
this.request('https://steamcommunity.com/market/', function (err, response, body) {
this.httpRequest('https://steamcommunity.com/market/', function (err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}

View File

@ -220,7 +220,7 @@ SteamCommunity.prototype.uploadAvatar = function(image, format, callback) {
if(image instanceof Buffer) {
doUpload(image);
} else if(image.match(/^https?:\/\//)) {
this.request.get({
this.httpRequestGet({
"uri": image,
"encoding": null
}, function(err, response, body) {
@ -300,7 +300,7 @@ SteamCommunity.prototype.uploadAvatar = function(image, format, callback) {
return;
}
self.request.post({
self.httpRequestPost({
"uri": "https://steamcommunity.com/actions/FileUploader",
"formData": {
"MAX_FILE_SIZE": buffer.length,

View File

@ -16,7 +16,7 @@ SteamCommunity.prototype.enableTwoFactor = function(callback) {
return;
}
self.request.post({
self.httpRequestPost({
"uri": "https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v1/",
"form": {
"steamid": self.steamID.getSteamID64(),
@ -66,7 +66,7 @@ SteamCommunity.prototype.finalizeTwoFactor = function(secret, activationCode, ca
function finalize(token) {
var code = SteamTotp.generateAuthCode(secret, diff);
self.request.post({
self.httpRequestPost({
"uri": "https://api.steampowered.com/ITwoFactorService/FinalizeAddAuthenticator/v1/",
"form": {
"steamid": self.steamID.getSteamID64(),
@ -117,7 +117,7 @@ SteamCommunity.prototype.disableTwoFactor = function(revocationCode, callback) {
return;
}
self.request.post({
self.httpRequestPost({
"uri": "https://api.steampowered.com/ITwoFactorService/RemoveAuthenticator/v1/",
"form": {
"steamid": self.steamID.getSteamID64(),

View File

@ -8,7 +8,7 @@ SteamCommunity.prototype.addFriend = function(userID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/AddFriendAjax",
"form": {
"accept_invite": 0,
@ -39,7 +39,7 @@ SteamCommunity.prototype.acceptFriendRequest = function(userID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/AddFriendAjax",
"form": {
"accept_invite": 1,
@ -65,7 +65,7 @@ SteamCommunity.prototype.removeFriend = function(userID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/RemoveFriendAjax",
"form": {
"sessionID": this.getSessionID(),
@ -90,7 +90,7 @@ SteamCommunity.prototype.blockCommunication = function(userID, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/BlockUserAjax",
"form": {
"sessionID": this.getSessionID(),
@ -137,7 +137,7 @@ SteamCommunity.prototype.postUserComment = function(userID, message, callback) {
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/comment/Profile/post/" + userID.toString() + "/-1",
"form": {
"comment": message,
@ -170,7 +170,7 @@ SteamCommunity.prototype.inviteUserToGroup = function(userID, groupID, callback)
}
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://steamcommunity.com/actions/GroupInvite",
"form": {
"group": groupID.toString(),
@ -215,7 +215,7 @@ SteamCommunity.prototype.getUserInventoryContexts = function(userID, callback) {
}
var self = this;
this.request("https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/inventory/", function(err, response, body) {
this.httpRequest("https://steamcommunity.com/profiles/" + userID.getSteamID64() + "/inventory/", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@ -249,7 +249,7 @@ SteamCommunity.prototype.getUserInventory = function(userID, appID, contextID, t
get([], []);
function get(inventory, currency, start) {
self.request({
self.httpRequest({
"uri": "https://steamcommunity.com" + endpoint + "/inventory/json/" + appID + "/" + contextID,
"qs": {
"start": start,

View File

@ -2,7 +2,7 @@ var SteamCommunity = require('../index.js');
SteamCommunity.prototype.getWebApiKey = function(domain, callback) {
var self = this;
this.request({
this.httpRequest({
"uri": "https://steamcommunity.com/dev/apikey",
"followRedirect": false
}, function(err, response, body) {
@ -20,7 +20,7 @@ SteamCommunity.prototype.getWebApiKey = function(domain, callback) {
callback(null, match[1]);
} else {
// We need to register a new API key
self.request.post('https://steamcommunity.com/dev/registerkey', {
self.httpRequestPost('https://steamcommunity.com/dev/registerkey', {
"form": {
"domain": domain,
"agreeToTerms": "agreed",
@ -46,7 +46,7 @@ SteamCommunity.prototype.getWebApiOauthToken = function(callback) {
}
// Pull an oauth token from the webchat UI
this.request("https://steamcommunity.com/chat", function(err, response, body) {
this.httpRequest("https://steamcommunity.com/chat", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}

View File

@ -67,7 +67,7 @@ SteamCommunity.prototype.login = function(details, callback) {
this._jar.setCookie(Request.cookie("mobileClientVersion=0 (2.1.3)"), "https://steamcommunity.com");
this._jar.setCookie(Request.cookie("mobileClient=android"), "https://steamcommunity.com");
this.request.post("https://steamcommunity.com/login/getrsakey/", {
this.httpRequestPost("https://steamcommunity.com/login/getrsakey/", {
"form": {"username": details.accountName},
"headers": mobileHeaders,
"json": true
@ -87,7 +87,7 @@ SteamCommunity.prototype.login = function(details, callback) {
var key = new RSA();
key.setPublic(body.publickey_mod, body.publickey_exp);
self.request.post({
self.httpRequestPost({
"uri": "https://steamcommunity.com/login/dologin/",
"json": true,
"form": {
@ -175,7 +175,7 @@ SteamCommunity.prototype.oAuthLogin = function(steamguard, token, callback) {
var steamID = new SteamID(steamguard[0]);
var self = this;
this.request.post({
this.httpRequestPost({
"uri": "https://api.steampowered.com/IMobileAuthService/GetWGToken/v1/",
"form": {
"access_token": token
@ -234,7 +234,9 @@ function generateSessionID() {
}
SteamCommunity.prototype.parentalUnlock = function(pin, callback) {
this.request.post("https://steamcommunity.com/parental/ajaxunlock", {
var self = this;
this.httpRequestPost("https://steamcommunity.com/parental/ajaxunlock", {
"json": true,
"form": {
"pin": pin
@ -262,7 +264,7 @@ SteamCommunity.prototype.parentalUnlock = function(pin, callback) {
SteamCommunity.prototype.getNotifications = function(callback) {
var self = this;
this.request.get("https://steamcommunity.com/actions/RefreshNotificationArea", function(err, response, body) {
this.httpRequestGet("https://steamcommunity.com/actions/RefreshNotificationArea", function(err, response, body) {
if(self._checkHttpError(err, response, callback)) {
return;
}
@ -298,7 +300,7 @@ SteamCommunity.prototype.getNotifications = function(callback) {
SteamCommunity.prototype.resetItemNotifications = function(callback) {
var self = this;
this.request.get("https://steamcommunity.com/my/inventory", function(err, response, body) {
this.httpRequestGet("https://steamcommunity.com/my/inventory", function(err, response, body) {
if(!callback) {
return;
}
@ -312,7 +314,7 @@ SteamCommunity.prototype.resetItemNotifications = function(callback) {
};
SteamCommunity.prototype.loggedIn = function(callback) {
this.request("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
this.httpRequest("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
if(err || (response.statusCode != 302 && response.statusCode != 403)) {
callback(err || new Error("HTTP error " + response.statusCode));
return;
@ -327,19 +329,9 @@ SteamCommunity.prototype.loggedIn = function(callback) {
});
};
SteamCommunity.prototype._checkCommunityError = function(html, callback) {
if(html.match(/<h1>Sorry!<\/h1>/)) {
var match = html.match(/<h3>(.+)<\/h3>/);
callback(new Error(match ? match[1] : "Unknown error occurred"));
return true;
}
return false;
};
SteamCommunity.prototype._myProfile = function(endpoint, form, callback) {
var self = this;
this.request("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
this.httpRequest("https://steamcommunity.com/my", {"followRedirect": false}, function(err, response, body) {
if(err || response.statusCode != 302) {
callback(err || "HTTP error " + response.statusCode);
return;
@ -350,32 +342,23 @@ SteamCommunity.prototype._myProfile = function(endpoint, form, callback) {
callback(new Error("Can't get profile URL"));
return;
}
(form ? self.request.post : self.request)("https://steamcommunity.com" + match[1] + "/" + endpoint, form ? {"form": form} : {}, callback);
var options = {
"uri": "https://steamcommunity.com" + mtch[1] + "/" + endpoint,
"method": "GET"
};
if (form) {
options.method = "POST";
options.form = form;
}
self.httpRequest(options, callback);
});
};
SteamCommunity.prototype._checkHttpError = function(err, response, callback) {
if(err) {
callback(err);
return true;
}
if(response.statusCode >= 300 && response.statusCode <= 399 && response.headers.location.indexOf('/login') != -1) {
callback(new Error("Not Logged In"));
return true;
}
if(response.statusCode >= 400) {
var error = new Error("HTTP error " + response.statusCode);
error.code = response.statusCode;
callback(error);
return true;
}
return false;
};
require('./components/http.js');
require('./components/chat.js');
require('./components/profile.js');
require('./components/market.js');