зеркало из https://github.com/mozilla/CSOL-site.git
Adding some method functionality to `remote`
This commit is contained in:
Родитель
8cac79ee5c
Коммит
054b3e7067
55
api.js
55
api.js
|
@ -77,34 +77,47 @@ function apiMethod (method) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load data from remote endpoint
|
// Load data from remote endpoint
|
||||||
function remote (method, path, callback) {
|
var remote = (function() {
|
||||||
// TODO - put this in settings somewhere
|
function remote (method, path, callback) {
|
||||||
var origin = 'http://openbadger-csol.mofostaging.net';
|
// TODO - put this in settings somewhere
|
||||||
|
var origin = 'http://openbadger-csol.mofostaging.net';
|
||||||
|
|
||||||
if (!request[method])
|
if (!request[method])
|
||||||
return callback(500, 'Unknown method');
|
return callback(500, 'Unknown method');
|
||||||
|
|
||||||
// TODO - need to add ability to pass data through
|
// TODO - need to add ability to pass data through
|
||||||
// TODO - might want to cache this at some point
|
// TODO - might want to cache this at some point
|
||||||
request[method](origin + path, function(err, response, body) {
|
request[method](origin + path, function(err, response, body) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(500, err);
|
return callback(500, err);
|
||||||
|
|
||||||
if (response.statusCode !== 200)
|
if (response.statusCode !== 200)
|
||||||
return callback(500, 'Upstream error');
|
return callback(500, 'Upstream error');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var data = JSON.parse(body);
|
var data = JSON.parse(body);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return callback(500, e.message);
|
return callback(500, e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.status !== 'ok')
|
if (data.status !== 'ok')
|
||||||
return callback(500, data.reason);
|
return callback(500, data.reason);
|
||||||
|
|
||||||
callback(null, data);
|
callback(null, data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
_.each(['get', 'post', 'put', 'patch', 'head', 'del'], function(method) {
|
||||||
|
Object.defineProperty(remote, method, {
|
||||||
|
enumerable: true,
|
||||||
|
value: function(path, callback) {
|
||||||
|
remote(method, path, callback);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
return remote;
|
||||||
|
})();
|
||||||
|
|
||||||
// Make sure badges returned from remote API
|
// Make sure badges returned from remote API
|
||||||
// contain all the information we need
|
// contain all the information we need
|
||||||
|
|
Загрузка…
Ссылка в новой задаче