<commit_before>var util = require("util");
var Resource = require("./resource");
var fs = require('fs');
var _ = require('underscore');
var request = require("request-promise");
var CookieStorage = require("./cookie-storage");
var RequestJar = require("./jar");

function Session(device, storage) {
    this.setDevice(device);    
    this.setCookiesStorage(storage);
}

util.inherits(Session, Resource);
module.exports = Session;

var CONSTANTS = require("./constants");
var Account = require('./account');
var Exceptions = require('./exceptions');
var Request = require('./request');
var Device = require("./device");
var QE = require("./qe");
var Megaphone = require("./megaphone");
var Timeline = require("./feeds/timeline-feed");
var Inbox = require("./feeds/inbox");
var Thread = require("./thread");
var Relationship = require("./relationship");
var Helpers = require("../../helpers");

Object.defineProperty(Session.prototype, "jar", {
    get: function() { return this._jar },
    set: function(val) {}
});


Object.defineProperty(Session.prototype, "cookieStore", {
    get: function() { return this._cookiesStore },
    set: function(val) {}
});


Object.defineProperty(Session.prototype, "device", {
    get: function() { return this._device },
    set: function(val) {}
});


Object.defineProperty(Session.prototype, "CSRFToken", {
    get: function() { 
        var cookies = this.jar.getCookies(CONSTANTS.HOST) 
        var item = _.findWhere(cookies, { key: "csrftoken" });
        return item ? item.value : "missing";
    },
    set: function(val) {}
});

Object.defineProperty(Session.prototype, "proxyUrl", {
    get: function() { 
        return this._proxyUrl;
    },
    set: function (val) {
        if (!Helpers.isValidUrl(val) && val !== null)
            throw new Error("`proxyUrl` argument is not an valid url")
        this._proxyUrl = val;
    }
});


Session.prototype.setCookiesStorage = function (storage) {
    if(!(storage instanceof CookieStorage))
        throw new Error("`storage` is not an valid instance of `CookieStorage`");
    this._cookiesStore = storage;
    this._jar = new RequestJar(storage.store);
    return this;
};


Session.prototype.setDevice = function (device) {
    if(!(device instanceof Device))
        throw new Error("`device` is not an valid instance of `Device`");
    this._device = device;
    return this;
};


Session.prototype.getAccountId = function() {
    var that = this;
    return this._cookiesStore.getSessionId()
        .then(function () {
            return that._cookiesStore.getAccountId();
        })
}


Session.prototype.setProxy = function(url) {
    this.proxyUrl = url;
    return this;
}


Session.prototype.getAccount = function () {
    var that = this;
    return that.getAccountId()
        .then(function(id){
            return Account.getById(that, id);
        })
};


Session.prototype.destroy = function () {
    this.cookiesStore.destroy();
    return new Request(this)
        .setMethod('POST')
        .setResource('logout')
        .generateUUID()
        .send();
};


Session.login = function(session, username, password) {
    return new Request(session)
        .setResource('login')
        .setMethod('POST')
        .generateUUID()
        .setData({<commit_msg>Fixed an issue that an exception will be thrown when calling session.destroy()<commit_after>var util = require("util");
var Resource = require("./resource");
var fs = require('fs');
var _ = require('underscore');
var request = require("request-promise");
var CookieStorage = require("./cookie-storage");
var RequestJar = require("./jar");

function Session(device, storage) {
    this.setDevice(device);    
    this.setCookiesStorage(storage);
}

util.inherits(Session, Resource);
module.exports = Session;

var CONSTANTS = require("./constants");
var Account = require('./account');
var Exceptions = require('./exceptions');
var Request = require('./request');
var Device = require("./device");
var QE = require("./qe");
var Megaphone = require("./megaphone");
var Timeline = require("./feeds/timeline-feed");
var Inbox = require("./feeds/inbox");
var Thread = require("./thread");
var Relationship = require("./relationship");
var Helpers = require("../../helpers");

Object.defineProperty(Session.prototype, "jar", {
    get: function() { return this._jar },
    set: function(val) {}
});


Object.defineProperty(Session.prototype, "cookieStore", {
    get: function() { return this._cookiesStore },
    set: function(val) {}
});


Object.defineProperty(Session.prototype, "device", {
    get: function() { return this._device },
    set: function(val) {}
});


Object.defineProperty(Session.prototype, "CSRFToken", {
    get: function() { 
        var cookies = this.jar.getCookies(CONSTANTS.HOST) 
        var item = _.findWhere(cookies, { key: "csrftoken" });
        return item ? item.value : "missing";
    },
    set: function(val) {}
});

Object.defineProperty(Session.prototype, "proxyUrl", {
    get: function() { 
        return this._proxyUrl;
    },
    set: function (val) {
        if (!Helpers.isValidUrl(val) && val !== null)
            throw new Error("`proxyUrl` argument is not an valid url")
        this._proxyUrl = val;
    }
});


Session.prototype.setCookiesStorage = function (storage) {
    if(!(storage instanceof CookieStorage))
        throw new Error("`storage` is not an valid instance of `CookieStorage`");
    this._cookiesStore = storage;
    this._jar = new RequestJar(storage.store);
    return this;
};


Session.prototype.setDevice = function (device) {
    if(!(device instanceof Device))
        throw new Error("`device` is not an valid instance of `Device`");
    this._device = device;
    return this;
};


Session.prototype.getAccountId = function() {
    var that = this;
    return this._cookiesStore.getSessionId()
        .then(function () {
            return that._cookiesStore.getAccountId();
        })
}


Session.prototype.setProxy = function(url) {
    this.proxyUrl = url;
    return this;
}


Session.prototype.getAccount = function () {
    var that = this;
    return that.getAccountId()
        .then(function(id){
            return Account.getById(that, id);
        })
};


Session.prototype.destroy = function () {
    this._cookiesStore.destroy();
    return new Request(this)
        .setMethod('POST')
        .setResource('logout')
        .generateUUID()
        .send();
};


Session.login = function(session, username, password) {
    return new Request(session)
        .setResource('login')
        .setMethod('POST')
        .generateUUID()
        .setData({
{'fraction_non_alphanumeric': 0.1549742969458724, 'fraction_numerical': 0.0, 'mean_word_length': 3.2982456140350878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2807155', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1912, 'n_words': 454}