diff --git a/lib/index.js b/lib/index.js index 79e6245..3e69391 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,5 @@ var request = require('request'); +var exec = require('child_process').exec; var API_URL = 'https://api.filepreviews.io/v2'; var FilePreviews; @@ -20,6 +21,17 @@ FilePreviews = function(options) { } }; +FilePreviews.VERSION = require('../package.json').version; +FilePreviews.CLIENT_UA_SERIALIZED = null; +FilePreviews.CLIENT_UA = { + lang: 'node', + publisher: 'filepreviews', + bindings_version: FilePreviews.VERSION, + lang_version: process.version, + platform: process.platform, + uname: null +}; + FilePreviews.prototype.log = function(msg) { if (this.debug) { console.log(msg); @@ -82,6 +94,18 @@ FilePreviews.prototype.request = function(url, options, callback) { callback(body); }.bind(this); + var getClientUserAgent = function(cb) { + if (FilePreviews.CLIENT_UA_SERIALIZED) { + return cb(FilePreviews.CLIENT_UA_SERIALIZED); + } + + exec('uname -a', function(err, uname) { + FilePreviews.CLIENT_UA.uname = uname || 'UNKNOWN'; + FilePreviews.CLIENT_UA_SERIALIZED = JSON.stringify(FilePreviews.CLIENT_UA); + cb(FilePreviews.CLIENT_UA_SERIALIZED); + }); + }; + var requestOptions = { url: url, method: _options.method || 'GET', @@ -89,6 +113,11 @@ FilePreviews.prototype.request = function(url, options, callback) { auth: { username: this.apiKey, password: this.apiSecret + }, + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'User-Agent': 'FilePreviews/v2 NodeBindings/' + FilePreviews.VERSION } }; @@ -98,13 +127,17 @@ FilePreviews.prototype.request = function(url, options, callback) { this.log('API request to: ' + url); - this._request(requestOptions, function(error, response, body) { - if (error) { - onError(error, response, body); - } else { - onSuccess(error, response, body); - } - }); + getClientUserAgent(function(clientUA) { + requestOptions.headers['X-FilePreviews-Client-User-Agent'] = clientUA; + + this._request(requestOptions, function(error, response, body) { + if (error) { + onError(error, response, body); + } else { + onSuccess(error, response, body); + } + }); + }.bind(this)); }; FilePreviews.prototype.getAPIRequestData = function(url, options) {