nodesu
Version:
a node.js osu! api wrapper
148 lines (115 loc) • 6.45 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: Requester.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: Requester.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
const http = require('http')
, https = require('https')
, querystring = require('querystring');
const OsuApiError = require('./error/OsuApiError');
/**
* Class for internal API requests
* @property {String} userAgent The useragent used to make requests.
*/
class Requester {
/**
* Creates the internal requester
* @return {Requester} A Requester instance
*/
constructor(api) {
this.api = api;
this.userAgent = `nodesu v${require('../package.json').version} (https://github.com/nicholastay/nodesu)`;
}
/**
* Gets data from an API endpoint.
* @param {String} endpoint The endpoint to get the data from.
* @param {Object} payloadOptions An object of options to be used as the query string.
* @param {Boolean} withKey To use the API key defined in the parent client or not.
* @param {Boolean} ratelimit Ratelimit the request on the main bucket if ratelimiting is enabled.
* @param {Any} castType "Cast" the elements of the array to the certain type.
* @return {Promise<Object[]|Any>} The returned object array from the API or Type[] as defined in castType.
*/
get(endpoint, payloadOptions, withKey, ratelimit, castType) {
let p = () => {
let prom = this._get(endpoint, payloadOptions, withKey);
if (castType && this.api.parseData) {
return prom
.then(d => {
if (Array.isArray(d))
return d.map(f => new castType(f));
return new castType(d);
});
}
return prom;
};
if (ratelimit && !this.api.disableRateLimiting) {
return new Promise((resolve, reject) => {
this.api.ratelimiting.mainBucket.removeTokens(1, () => {
p().then(resolve).catch(reject);
});
});
}
return p();
}
_get(endpoint, payloadOptions, withKey) {
let options = {};
if (withKey)
options.k = this.api.apiKey; // api key
Object.assign(options, payloadOptions || {}); // merge in payload of options
const driver = this.api.baseUrl.indexOf('https') === 0 ? https : http;
const url = `${this.api.baseUrl}${endpoint}?${querystring.stringify(options)}`;
const requestOptions = {
headers: {
'User-Agent': this.userAgent
}
};
return new Promise((resolve, reject) => {
driver.get(url, requestOptions, res => {
let data = '';
res.on('data', chunk => data += chunk);
res.once('end', () => {
// parse json
let j;
try {
j = JSON.parse(data);
}
catch (e) {
return reject(e);
}
if (j.error) // error from api
return reject(new OsuApiError(j.error));
resolve(j); // resolve json
});
})
});
}
}
module.exports = Requester;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-Components.html">Components</a></li><li><a href="module-CustomClasses.html">CustomClasses</a></li><li><a href="module-Enums.html">Enums</a></li><li><a href="module-Errors.html">Errors</a></li></ul><h3>Classes</h3><ul><li><a href="Client.html">Client</a></li><li><a href="module-Components.BeatmapsComponent.html">BeatmapsComponent</a></li><li><a href="module-Components.MultiComponent.html">MultiComponent</a></li><li><a href="module-Components.ReplayComponent.html">ReplayComponent</a></li><li><a href="module-Components.ScoresComponent.html">ScoresComponent</a></li><li><a href="module-Components.UserComponent.html">UserComponent</a></li><li><a href="module-CustomClasses.Beatmap.html">Beatmap</a></li><li><a href="module-CustomClasses.BeatmapScore.html">BeatmapScore</a></li><li><a href="module-CustomClasses.Multi.html">Multi</a></li><li><a href="module-CustomClasses.MultiGame.html">MultiGame</a></li><li><a href="module-CustomClasses.MultiMatch.html">MultiMatch</a></li><li><a href="module-CustomClasses.MultiScore.html">MultiScore</a></li><li><a href="module-CustomClasses.ReplayData.html">ReplayData</a></li><li><a href="module-CustomClasses.Score.html">Score</a></li><li><a href="module-CustomClasses.User.html">User</a></li><li><a href="module-CustomClasses.UserEvent.html">UserEvent</a></li><li><a href="module-CustomClasses.UserScore.html">UserScore</a></li><li><a href="module-Errors.OsuApiError.html">OsuApiError</a></li><li><a href="Requester.html">Requester</a></li></ul><h3>Global</h3><ul><li><a href="global.html#ApprovalStatus">ApprovalStatus</a></li><li><a href="global.html#Converts">Converts</a></li><li><a href="global.html#Genre">Genre</a></li><li><a href="global.html#Language">Language</a></li><li><a href="global.html#LookupType">LookupType</a></li><li><a href="global.html#Mode">Mode</a></li><li><a href="global.html#Mods">Mods</a></li><li><a href="global.html#MultiScoringType">MultiScoringType</a></li><li><a href="global.html#MultiTeam">MultiTeam</a></li><li><a href="global.html#MultiTeamType">MultiTeamType</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Sat Apr 11 2020 14:41:31 GMT+1000 (GMT+10:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>