reject(err); } else if (resp.statusCode > 204) { let message = resp.statusMessage; if (resp.body && resp.body.message) { message += ", " + resp.body.message;
let message = resp.statusMessage; if (resp.body && resp.body.message) { message += ", " + resp.body.message;
this._request(path, opts, function(err, res, body) { if (err) return callback(err); if (res.statusCode != 200 && res.statusCode != 201) { var msg = { ErrorCode: res.statusCode, Message: res.statusMessage, Detail: body }; return callback(JSON.stringify(msg)); } return callback(null, isUpload ? JSON.parse(body) : body); })
request(source) .on('response', function (resSource) { if (process.env.CONSOLELOG) console.log('Download ' + source.url + ': ' + resSource.statusCode + ' > ' + resSource.statusMessage); sourceStatusCode = resSource.statusCode; resSource.headers['content-type'] = undefined; // if the source response have this header, Dropbox may file for some types }) .pipe(request(destination) .on('response', function (resDestination) { if (process.env.CONSOLELOG) console.log('Upload ' + destination.url + ': ' + resDestination.statusCode + ' > ' + resDestination.statusMessage); MakeCallback(autodeskId, taskId, resDestination.statusCode, sourceStatusCode, data, callback); }));
/** * Executes a GET request to the given URL and calls a callback function with the request's response * @param {String} url the URL from which to get data via a GET request * @param {Function} callback the callback function that is given the request's response */ function httpRequest(url, callback) { request({ url, json: true, headers: { accept: 'application/json', }, }, (error, res, json) => { if (error) throw error; if (res.statusCode >= 400) throw new Error(res.statusMessage); callback(json); }); }
request.post(requestObject, function (error, response, body) { if (error) return reject(error) if (!body || !body.selectedProfile) { return reject(new Error('Validation error: ' + response.statusMessage)) } const userProfile = { access_token: body.accessToken, client_token: uuid(), uuid: body.selectedProfile.id, name: body.selectedProfile.name, user_properties: JSON.stringify(body.user.properties || {}) } resolve(userProfile) })
function listSubjectInLevel (level) { let levelUrl = levelUrlMapping[level.toLowerCase()] if (!levelUrl) throw new Error(`Unknown level ${level}`) return new Promise((resolve, reject) => { req({ uri: levelUrl, json: true }, (err, res, body) => { if (err) return void reject(err) if (res.statusCode !== 200) return void reject(new Error(`Error while loading ${levelUrl}: ${res.statusCode} (${res.statusMessage})`)) if (res.headers['content-type'].indexOf('json') < 0) return void reject(new Error(`${levelUrl} should return json, but it didn't.`)) let subjects = body try { resolve(subjects.map(s => ({ id: s.SyllabusCode.toString(), name: s.Title.replace(/\(\d{4}\)/, '').trim()}))) } catch (e) { reject(new Error(`Unexpected API response - ${e.message}`)) } }) }) }
get() { return new Promise((resolve, reject) => { if (!DarkSky.truthyOrZero(this.lat) || !DarkSky.truthyOrZero(this.long)) { reject("Request not sent. ERROR: Longitute or Latitude is missing.") } this._generateReqUrl() req( { url: this.url, json: true, timeout: this.timeoutVal, gzip: this.gzip }, (err, res, body) => { if (err) { reject(`Forecast cannot be retrieved. ERROR: ${err}`) return } if (res.statusCode !== 200) { reject( `Forecast cannot be retrieved. Response: ${res.statusCode} ${ res.statusMessage }` ) return } resolve(body) } ) }) }
cb( null, "Create user failed with : " + response.statusCode + " - " + response.statusMessage);
request(req, function (error, response, body) { if (error) { log(API_ERR_LEVEL, error); return callback(error, null); } if (response.statusCode != 200) { return callback(response.statusMessage, null); } log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body)); log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response)); try { body = body.response; callback(null, body); } catch (e) { log(API_ERR_LEVEL, 'Error parsing vehicles response'); callback(e, null); } log(API_RETURN_LEVEL, "\nGET request: " + "/vehicles" + " completed."); });
request.post(requestObject, function (error, response, body) { if (error) return reject(error) if (!body || !body.selectedProfile) { return reject(new Error('Validation error: ' + response.statusMessage)) } const userProfile = { access_token: body.accessToken, client_token: body.clientToken, uuid: body.selectedProfile.id, name: body.selectedProfile.name, selected_profile: body.selectedProfile, user_properties: JSON.stringify(body.user.properties || {}) } resolve(userProfile) })
request(req, function (error, response, body) { if (error) { log(API_ERR_LEVEL, error); return callback(error, null); } if (response.statusCode != 200) { return callback(response.statusMessage, null); } log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body)); log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response)); try { body = body.response[options.carIndex || 0]; body.id = body.id_s; options.vehicleID = body.id; callback(null, body); } catch (e) { log(API_ERR_LEVEL, 'Error parsing vehicles response'); callback(e, null); } log(API_RETURN_LEVEL, "\nGET request: " + "/vehicles" + " completed."); });
request(req, function(error, response, body) { if (error) { log(API_ERR_LEVEL, error); return callback(error, null); } if (response.statusCode != 200) { return callback(response.statusMessage, null); } log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body)); log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response)); try { body = body.response; callback(null, body); } catch (e) { console.log(e); log(API_ERR_LEVEL, "Error parsing products response"); callback(e, null); } log(API_RETURN_LEVEL, "\nGET request: " + "/products" + " completed."); });
request(source) .on('response', function (resSource) { if (process.env.CONSOLELOG) console.log('Download ' + source.url + ': ' + resSource.statusCode + ' > ' + resSource.statusMessage); sourceStatusCode = resSource.statusCode; resSource.headers['content-type'] = undefined; // if the source response have this header, Dropbox may file for some types }) .pipe(request(destination) .on('response', function (resDestination) { if (process.env.CONSOLELOG) console.log('Upload ' + destination.url + ': ' + resDestination.statusCode + ' > ' + resDestination.statusMessage); MakeCallback(autodeskId, taskId, resDestination.statusCode, sourceStatusCode, data, callback); }));
request(req, function(error, response, body) { if (error) { log(API_ERR_LEVEL, error); return callback(error, null); } if (response.statusCode != 200) { return callback(response.statusMessage, null); } log(API_BODY_LEVEL, "\nBody: " + JSON.stringify(body)); log(API_RESPONSE_LEVEL, "\nResponse: " + JSON.stringify(response)); try { body = body.response; callback(null, body); } catch (e) { log(API_ERR_LEVEL, "Error parsing solarStatus response"); callback(e, null); } log(API_RETURN_LEVEL, "\nGET request: " + "/solarStatus" + " completed."); });