Skip to content

Commit 2307b1f

Browse files
Change to object configuration. Closes #2
1 parent e821efa commit 2307b1f

File tree

6 files changed

+61
-42
lines changed

6 files changed

+61
-42
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ Node.js module for interacting with the ATLauncher API.
44

55
Examples
66
----
7-
To get started simply require the package and optionally provide the arguments it needs.
7+
To get started simply require the package and optionally provide an optional object with settings.
88

99
```
10-
var api = require('atlauncher-api')(API_KEY, FORCE);
10+
var api = require('atlauncher-api')({
11+
api_key: 'my-api-key,
12+
force_run: false
13+
});
1114
```
1215

13-
The API_KEY argument is your API-KEY used for admin or PSP api calls.
16+
The api_key argument is your API-KEY used for admin or PSP api calls.
1417

15-
The FORCE argument is a boolean (true/false) if we should ignore breaking api calls. For instance when you're about to reach your request limit and get IP banned the force option will allow you to bypass the exception which gets thrown halting execution.
18+
The force_run argument is a boolean (true/false) if we should ignore breaking api calls. For instance when you're about to reach your request limit and get IP banned the force option will allow you to bypass the exception which gets thrown halting execution.
1619

1720
For instance for running public api calls such as getting a list of all packs you can use the following:
1821

index.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
module.exports = function (apiKey, forceRun, baseUrl, version) {
19+
module.exports = function (obj) {
2020
var request = require('request'),
2121
fs = require('fs'),
22-
baseUrl = baseUrl || 'https://api.atlauncher.com/',
23-
version = version || 'v1',
24-
forceRun = forceRun || false;
22+
merge = require('merge');
23+
24+
var settings = {
25+
base_url: 'https://api.atlauncher.com/',
26+
api_version: 'v1',
27+
api_key: false,
28+
force_run: false
29+
};
30+
31+
if (obj !== undefined) {
32+
settings = merge(settings, obj);
33+
}
2534

2635
var makeUrl = function (path) {
27-
return baseUrl + version + '/' + (path ? path : '');
36+
return settings.base_url + settings.api_version + '/' + (path ? path : '');
2837
};
2938

3039
var saveToFile = function (err, res, saveTo, base64, callback) {
@@ -55,7 +64,7 @@ module.exports = function (apiKey, forceRun, baseUrl, version) {
5564
};
5665

5766
var makeRequest = function (needsApiKey, path, method, data, callback) {
58-
if (needsApiKey && !apiKey) {
67+
if (needsApiKey && !settings.api_key) {
5968
return console.error('An API Key must be set in order to make this request!');
6069
}
6170

@@ -77,16 +86,16 @@ module.exports = function (apiKey, forceRun, baseUrl, version) {
7786
options.body = data;
7887
}
7988

80-
if (apiKey) {
81-
options.headers['API-KEY'] = apiKey;
89+
if (settings.api_key) {
90+
options.headers['API-KEY'] = settings.api_key;
8291
}
8392

8493
request(options, function (err, req, body) {
8594
if (!err && body.error && body.code == 401 && body.message == 'API key missing or invalid!') {
8695
console.error('The API key provided was not valid!');
8796
}
8897

89-
if (!err && body && body.code == 429 && !forceRun) {
98+
if (!err && body && body.code == 429 && !settings.force_run) {
9099
// Exceeded API request limit, we must stop now
91100
throw new Error(body.message);
92101
}
@@ -97,7 +106,7 @@ module.exports = function (apiKey, forceRun, baseUrl, version) {
97106

98107
return {
99108
heartbeat: function (callback) {
100-
makeRequest(false, baseUrl, 'GET', callback);
109+
makeRequest(false, settings.base_url, 'GET', callback);
101110
},
102111
pack: function (name, version, callback) {
103112
if (version && !callback) {

package.json

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
{
2-
"name": "atlauncher-api",
3-
"version": "1.0.3",
4-
"description": "Node.js module for interacting with the ATLauncher API",
5-
"main": "index.js",
6-
"scripts": {
7-
"test": "node tests/test.js"
8-
},
9-
"repository": {
10-
"type": "git",
11-
"url": "https://github.com/ATLauncher/ATLauncher-API---NodeJS.git"
12-
},
13-
"keywords": [
14-
"atlauncher",
15-
"minecraft",
16-
"api"
17-
],
18-
"author": "ATLauncher",
19-
"license": "GNU GPLv3",
20-
"bugs": {
21-
"url": "https://github.com/ATLauncher/ATLauncher-API---NodeJS/issues"
22-
},
23-
"homepage": "https://github.com/ATLauncher/ATLauncher-API---NodeJS",
24-
"dependencies": {
25-
"request": "^2.54.0"
26-
}
2+
"name": "atlauncher-api",
3+
"version": "1.1.0",
4+
"description": "Node.js module for interacting with the ATLauncher API",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node tests/test.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/ATLauncher/ATLauncher-API---NodeJS.git"
12+
},
13+
"keywords": [
14+
"atlauncher",
15+
"minecraft",
16+
"api"
17+
],
18+
"author": "ATLauncher",
19+
"license": "GNU GPLv3",
20+
"bugs": {
21+
"url": "https://github.com/ATLauncher/ATLauncher-API---NodeJS/issues"
22+
},
23+
"homepage": "https://github.com/ATLauncher/ATLauncher-API---NodeJS",
24+
"dependencies": {
25+
"merge": "^1.2.0",
26+
"request": "^2.54.0"
27+
}
2728
}

tests/admin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
'use strict';
2020

21-
var atlauncher = require('../index')(process.env.API_KEY),
21+
var atlauncher = require('../index')({
22+
api_key: process.env.API_KEY
23+
}),
2224
assert = require('assert');
2325

2426

tests/psp.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
'use strict';
2020

21-
var atlauncher = require('../index')(process.env.PSP_API_KEY),
21+
var atlauncher = require('../index')({
22+
api_key: process.env.PSP_API_KEY
23+
}),
2224
assert = require('assert');
2325

2426
atlauncher.psp.packs.all(function (err, res) {

tests/public.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
'use strict';
2020

21-
var atlauncher = require('../index')(process.env.API_KEY),
21+
var atlauncher = require('../index')({
22+
api_key: process.env.API_KEY
23+
}),
2224
assert = require('assert');
2325

2426
atlauncher.heartbeat(function (err, res) {

0 commit comments

Comments
 (0)