Skip to content

Commit 15e887c

Browse files
Make error signature on-par with strider-github
API interaction is now partially handled by superagent. This enables the same error signature that is expected in other parts of the application.
1 parent b9641d4 commit 15e887c

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

lib/api.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var request = require('request'),
22
qs = require('querystring'),
33
_ = require('lodash'),
4-
async = require('async');
4+
async = require('async'),
5+
superagent = require('superagent');
56

67
module.exports = {
78
get: get,
@@ -12,23 +13,39 @@ module.exports = {
1213
removeDeployKey: removeDeployKey
1314
};
1415

16+
/**
17+
* Retrieve a file from a GitLab repository, using the GitLab API.
18+
* @param {Object} config The configuration of the GitLab provider plugin.
19+
* @param {String} uri The URI of the file to retrieve.
20+
* @param {Function} done A function to call, once a result is available.
21+
*/
22+
function get(config, uri, done) {
23+
// If the configuration has no GitLab API URL, bail.
24+
if (!config.api_url) {
25+
return done(new Error('API URL missing from GitLab provider configuration!'));
26+
}
27+
// Append a slash to the URL if there isn't one.
28+
var apiBase = config.api_url;
29+
if (!/\/$/.test(apiBase)) {
30+
apiBase += '/';
31+
}
32+
// Construct the complete request URL
33+
var url = apiBase + uri;
34+
var req = superagent.get(url).set('User-Agent', 'StriderCD (http://stridercd.com)');
35+
// Add our additional request parameters to the query part of the URL.
36+
req.query({
37+
private_token: config.api_key,
38+
per_page: 100
39+
});
1540

16-
function get(config, uri, callback) {
17-
var qpm = {
18-
private_token: config.api_key,
19-
per_page: 100
20-
},
21-
glue = (~uri.indexOf('?')) ? "&" : "?",
22-
url = config.api_url + '/' + uri + glue + qs.stringify(qpm);
23-
24-
request.get({
25-
url: url
26-
}, function(err, res) {
27-
if (err) {
28-
return callback(err);
41+
req.end(function (res) {
42+
if (res.error) {
43+
return done(res.error, null);
44+
}
45+
if (!res.body) {
46+
return done();
2947
}
30-
31-
callback(undefined, res.body);
48+
done(null, res.body);
3249
});
3350
}
3451

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"lodash": "~2.2.0",
4343
"request": "~2.27.0",
4444
"step": "0.0.5",
45-
"strider-git": "^0.2.3"
45+
"strider-git": "^0.2.3",
46+
"superagent": "~0.15.4"
4647
},
4748
"bugs": {
4849
"url": "https://github.com/meric426/strider-gitlab/issues"

0 commit comments

Comments
 (0)