Updated webapi.js methods to use new http interface

This commit is contained in:
Alex Corn 2023-06-27 04:11:04 -04:00
parent e65d50b5f7
commit 38d3fb39bf
No known key found for this signature in database
GPG Key ID: E51989A3E7A27FDF

View File

@ -1,48 +1,52 @@
const StdLib = require('@doctormckay/stdlib');
const SteamCommunity = require('../index.js'); const SteamCommunity = require('../index.js');
const Helpers = require('./helpers.js'); const Helpers = require('./helpers.js');
/**
* @param {string} domain
* @param {function} [callback]
* @return Promise<{key: string}>
*/
SteamCommunity.prototype.getWebApiKey = function(domain, callback) { SteamCommunity.prototype.getWebApiKey = function(domain, callback) {
this.httpRequest({ return StdLib.Promises.callbackPromise(['key'], callback, false, async (resolve, reject) => {
uri: 'https://steamcommunity.com/dev/apikey?l=english', let {textBody} = await this.httpRequest({
followRedirect: false method: 'GET',
}, (err, response, body) => { url: 'https://steamcommunity.com/dev/apikey?l=english',
if (err) { followRedirect: false,
callback(err); source: 'steamcommunity'
return; });
if (textBody.includes('<h2>Access Denied</h2>')) {
return reject(new Error('Access Denied'));
} }
if (body.includes('<h2>Access Denied</h2>')) { if (textBody.includes('You must have a validated email address to create a Steam Web API key.')) {
return callback(new Error('Access Denied')); return reject(new Error('You must have a validated email address to create a Steam Web API key.'));
} }
if (body.includes('You must have a validated email address to create a Steam Web API key.')) { let match = textBody.match(/<p>Key: ([0-9A-F]+)<\/p>/);
return callback(new Error('You must have a validated email address to create a Steam Web API key.'));
}
let match = body.match(/<p>Key: ([0-9A-F]+)<\/p>/);
if (match) { if (match) {
// We already have an API key registered // We already have an API key registered
callback(null, match[1]); return resolve({key: match[1]});
} else {
// We need to register a new API key
this.httpRequestPost('https://steamcommunity.com/dev/registerkey?l=english', {
form: {
domain,
agreeToTerms: 'agreed',
sessionid: this.getSessionID(),
Submit: 'Register'
}
}, (err, response, body) => {
if (err) {
callback(err);
return;
}
this.getWebApiKey(domain, callback);
}, 'steamcommunity');
} }
}, "steamcommunity");
// We need to register a new API key
await this.httpRequest({
method: 'POST',
url: 'https://steamcommunity.com/dev/registerkey?l=english',
form: {
domain,
agreeToTerms: 'agreed',
sessionid: this.getSessionID(),
Submit: 'Register'
},
source: 'steamcommunity'
});
resolve({key: await this.getWebApiKey(domain)});
});
}; };
/** /**