'use strict';

const request = require('request');

var Webhook = function(params) {
    this.serverUrl = params.baseBackendUrl;
    this.webhookUrl = this.serverUrl + 'syndication/webhook';
    this.host = params.baseBackendUrl.match(/^http:\/\/([^/:]+)/i)[1];
    this.port = params.baseBackendUrl.match(/:(\d{4})/i)[1];
    this.username = params.username;
    this.password = params.password;
    this.token = '';

    this.request = this.request.bind(this);
    this.login = this.login.bind(this);
    this.getSyndication = this.getSyndication.bind(this);
    this.fire = this.fire.bind(this);
};

Webhook.prototype.login = function() {
    return this.request({ 
        path: 'auth', 
        method: 'POST',
        data: {
            "username": this.username,
            "password": this.password
        }
    });
};

Webhook.prototype.getSyndication = function() {
    return this.request({
        path: 'syndication_in',
        method: 'GET'
    })
}

Webhook.prototype.request = function(params) {
    var options = {
        url: this.serverUrl + params.path,
        method: (params.hasOwnProperty('method')) ? params.method : 'GET',
        json: params.data,
        headers: {
            "Content-Type": "application/json;charset=UTF-8"
        }
    };

    if (this.token)
        options.headers['Authentication'] = 'Basic ' + this.token;

    return new Promise(function(resolve, reject) {
        request(options, function(err, response, body) {
            console.log(response);
            //console.log(err, body);
            if (err)
                reject(err);
            else
                resolve(body);
        });
    });
};

Webhook.prototype.fire = function(currentUrl) {
    return this.login()
        .then((body) => {
            console.log('token', body.token);
            this.token = body.token;
            return this.getSyndication();
        })
        .then((body) => {
            console.log('body get synd', body);
        })
        .catch(function(err) {
            console.log('err', err);
        });
};

module.exports = Webhook;

@@ -399,21 +399,20 @@
    this.
-token
+auth
  = '';%0A%0A
@@ -1284,21 +1284,20 @@
 f (this.
-token
+auth
 )%0A      
@@ -1323,13 +1323,12 @@
 Auth
-entic
+oriz
 atio
@@ -1337,29 +1337,17 @@
 %5D = 
-'Basic ' + this.token
+this.auth
 ;%0A%0A 
@@ -1457,81 +1457,8 @@
 ) %7B%0A
-            console.log(response);%0A            //console.log(err, body);%0A
     
@@ -1689,77 +1689,78 @@
     
-console.log('token', body.token);%0A            this.token = body.token
+this.auth = 'Basic ' + new Buffer(body.token + ':').toString('base64')
 ;%0A  

Fix authentication