Skip to content

Commit

Permalink
Merge pull request #25 from TourCMS/release/1.3.0
Browse files Browse the repository at this point in the history
release/1.3.0
  • Loading branch information
paulslugocki authored Feb 16, 2023
2 parents 23eb818 + 9e3ab5f commit b3f2964
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
39 changes: 32 additions & 7 deletions node-tourcms.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ function TourCMS(options) {
apiKey: '',
marketplaceId: 0,
channelId: 0,
channels: []
channels: [],
userAgent: '',
prependCallerToUserAgent: true
};

if(typeof options !== 'undefined') {
Expand All @@ -30,6 +32,9 @@ function TourCMS(options) {

if(typeof options.channelId !== 'undefined')
this.options.channelId = options.channelId;

if(typeof options.userAgent !== 'undefined')
this.options.userAgent = options.userAgent;
}

}
Expand Down Expand Up @@ -60,16 +65,20 @@ TourCMS.prototype.makeRequest = function(a) {
// Generate the signature
var signature = this.generateSignature(a.path, a.channelId, a.verb, outboundTime, this.options.apiKey);

var headers = {
'x-tourcms-date': outboundTime,
'Authorization': 'TourCMS ' + a.channelId + ':' + this.options.marketplaceId + ':' + signature,
'Content-type': 'text/xml;charset="utf-8"',
'Content-length': Buffer.byteLength(apiParams, 'utf8')
};

headers = this.addUserAgent(headers, a);

var options = {
method: a.verb,
hostname: this.options.hostname,
path: a.path,
headers: {
'x-tourcms-date': outboundTime,
'Authorization': 'TourCMS ' + a.channelId + ':' + this.options.marketplaceId + ':' + signature,
'Content-type': 'text/xml;charset="utf-8"',
'Content-length': Buffer.byteLength(apiParams, 'utf8')
}
headers: headers
};

var req = https.request(options, function(response) {
Expand Down Expand Up @@ -1296,6 +1305,22 @@ TourCMS.prototype.generateSignature = function(path, channelId, verb, outboundTi
return signature;
};

TourCMS.prototype.addUserAgent = function(headers, a) {

let userAgent = this.options.userAgent;

if(userAgent == '')
return headers;

if(this.options.prependCallerToUserAgent)
userAgent = userAgent + ' (' + this.options.marketplaceId + "_" + a.channelId + ')';

headers['User-Agent'] = userAgent;

return headers;

}

// Generate the current Unix Timestamp (PHP style)
TourCMS.prototype.generateTime = function() {
return Math.floor(new Date().getTime() / 1000);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"api"
],
"homepage": "https://github.com/tourcms/node-tourcms",
"version": "1.2.1",
"version": "1.3.0",
"author": "TourCMS <support@tourcms.com> (http://www.tourcms.com)",
"contributors": [
"Paul Slugocki <paul.slugocki@tourcms.com> (https://github.com/paulslugocki)"
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Create a new `tourcms` API object, passing in your API credentials
var TourCMS = new TourCMSApi({
channelId: YOUR_CHANNEL_ID,
apiKey: 'YOUR_API_KEY',
marketplaceId: YOUR_MARKETPLACE_ID
marketplaceId: YOUR_MARKETPLACE_ID,
userAgent: 'Your app name'
});
```
Then call one of the API methods below.
Expand Down

0 comments on commit b3f2964

Please sign in to comment.