Skip to content

Commit

Permalink
Updated HTTP Provider execute() to return a promise
Browse files Browse the repository at this point in the history
  • Loading branch information
kfitzgerald committed Apr 25, 2019
1 parent 7ccccee commit be63e71
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 68 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,10 @@

When stuff changes, it's described here.

## 1.7.1
* Updated HTTP Provider to return a promise
* Updated supporting tests

## 1.6.1
* Updated jsdoc to show that callback is optional

Expand Down
2 changes: 1 addition & 1 deletion dist/client.js
Expand Up @@ -80,7 +80,7 @@ function Client(config) {
/**
* SDK Version
*/
Client.Version = '1.6.1';
Client.Version = '1.7.0';

/**
* Expose the Provider base class
Expand Down
2 changes: 1 addition & 1 deletion dist/okanjo-sdk.js
Expand Up @@ -1713,7 +1713,7 @@ function Client(config) {
/**
* SDK Version
*/
Client.Version = '1.6.1';
Client.Version = '1.7.0';

/**
* Expose the Provider base class
Expand Down
2 changes: 1 addition & 1 deletion dist/okanjo-sdk.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/okanjo-sdk.min.js.map

Large diffs are not rendered by default.

114 changes: 65 additions & 49 deletions lib/providers/http_provider.js
Expand Up @@ -113,16 +113,14 @@ HttpProvider.prototype._getAuthorization = function(key, token) {
* @private
*/
HttpProvider.prototype._handleRequestTimeout = function(req, callback) {
/* istanbul ignore else: race conditions with testing - it works i swear */
if (!req._replied && callback) callback({
callback({
statusCode: 504,
error: "ETIMEDOUT",
message: "API request took too long to complete",
attributes: {
source: new Error('ETIMEDOUT')
}
}, null, req);
req._replied = true;
};


Expand Down Expand Up @@ -175,7 +173,7 @@ HttpProvider.prototype._handleRequestResponse = function(req, callback, query, r
err = data;

// Copy the payload status if different
if (data.statusCode != res.statusCode) {
if (data.statusCode !== res.statusCode) {
if (isPayloadError) {
err.responseStatusCode = res.statusCode;
} else {
Expand Down Expand Up @@ -217,8 +215,7 @@ HttpProvider.prototype._handleRequestResponse = function(req, callback, query, r
}

// Send the response
if (!req._replied && callback) callback(err, data, req);
req._replied = true;
callback(err, data, req);
}.bind(this));
};

Expand All @@ -231,72 +228,91 @@ HttpProvider.prototype._handleRequestResponse = function(req, callback, query, r
* @private
*/
HttpProvider.prototype._handleRequestError = function(req, callback, error) {
/* istanbul ignore else: double callbacks are tested elsewhere */
if (!req._replied && callback) callback({
callback({
statusCode: 503,
error: error.message,
message: "Something went wrong",
attributes: {
source: error
}
}, null, req);
req._replied = true;
};

/**
* Executes the query over HTTP
* @param {Query} query - The query to execute
* @param {requestCallback} callback – Callback to fire when request is completed
* @param {requestCallback} [callback] – Callback to fire when request is completed
*/
HttpProvider.prototype.execute = function(query, callback) {
return new Promise((resolve, reject) => {

// Prevent duplicate callbacks if multiple failures occur
let replied = false;
const done = function(err, res) {
if (!replied) {
replied = true;
if (err) {
if (callback) {
return callback(err, null);
} else {
return reject(err);
}
} else {
if (callback) {
return callback(null, res);
} else {
return resolve(res);
}
}
}
};

// Initialize headers
var headers = {
'User-Agent': this.userAgent
};
// Initialize headers
var headers = {
'User-Agent': this.userAgent
};

// Build authorization
var key = query.key || this.client.config.key || "",
sessionToken = query.sessionToken || this.client.config.sessionToken || "";
// Build authorization
var key = query.key || this.client.config.key || "",
sessionToken = query.sessionToken || this.client.config.sessionToken || "";

// Attach authorization (if any) to the request headers
if (key || sessionToken) headers.Authorization = this._getAuthorization(key, sessionToken);
// Attach authorization (if any) to the request headers
if (key || sessionToken) headers.Authorization = this._getAuthorization(key, sessionToken);

// Just in case we throw before req is set
var req = {
_replied: false
};
// Just in case we throw before req is set
var req = {};

try {
try {

// Fire off the request!
req = this.protocol.request({
host: this.host,
port: this.port,
ciphers: this.ciphers,
// Fire off the request!
req = this.protocol.request({
host: this.host,
port: this.port,
ciphers: this.ciphers,

method: query.method,
path: query.getFullPath(),
headers: headers
});
method: query.method,
path: query.getFullPath(),
headers: headers
});

req.setTimeout(this.timeout, this._handleRequestTimeout.bind(this, req, callback));
req.on('response', this._handleRequestResponse.bind(this, req, callback, query));
req.on('error', this._handleRequestError.bind(this, req, callback));
req.setTimeout(this.timeout, this._handleRequestTimeout.bind(this, req, done));
req.on('response', this._handleRequestResponse.bind(this, req, done, query));
req.on('error', this._handleRequestError.bind(this, req, done));

req.on('socket', function(socket) {
socket.on(this.protocolName === 'https' ? 'secureConnect' : 'connect', function() {
if (query.payload) {
req.write(JSON.stringify(query.payload));
}
req.end();
});
}.bind(this));
} catch (err) {
/* istanbul ignore next: out of scope */
// Request blew up before it even got off the ground
this._handleRequestError(req, callback, err);
}
req.on('socket', function(socket) {
socket.on(this.protocolName === 'https' ? 'secureConnect' : 'connect', function() {
if (query.payload) {
req.write(JSON.stringify(query.payload));
}
req.end();
});
}.bind(this));
} catch (err) {
/* istanbul ignore next: out of scope */
// Request blew up before it even got off the ground
this._handleRequestError(req, done, err);
}
});
};


Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "okanjo",
"version": "1.6.1",
"version": "1.7.0",
"description": "Integrate your application with the Okanjo API.",
"main": "dist/client.js",
"scripts": {
Expand Down

0 comments on commit be63e71

Please sign in to comment.