<commit_before>var request = require('request'),
    crypto = require('crypto'),
    URL = require('url'),
    querystring = require('querystring'),
    xml2js = require('xml2js');

var defaults = {
  host    : "sts.amazonaws.com",
  version : "2011-06-15",
  duration : 43200, // 12 hours
  algorithm : "HmacSHA256",
  signatureVersion : 2
};

var STS = exports.STS = function STS(config) {
  if ( !config.accessKeyId || ! config.secretAccessKey) {
    throw new Error('You must set the AWS credentials: accessKeyId + secretAccessKey');
  }

  this.config = config;
  this.parser = new xml2js.Parser();

  var credentials = null;

  this.__defineGetter__('credentials', function() {
    if(credentials && credentials.expiration > new Date()) {
      return credentials;
    } else {
      return null;
    }
  });

  this.__defineSetter__('credentials', function(resp){
    credentials = {
      sessionToken : resp.SessionToken,
      secretAccessKey : resp.SecretAccessKey,
      expiration : new Date(resp.Expiration),
      accessKeyId : resp.AccessKeyId
    };
  });

};

STS.prototype.getSessionToken = function(cb) {
  if(this.credentials) return cb(null, this.credentials);

  var self = this;
  
  var url = URL.format({host:defaults.host, protocol: "https", query : this._buildParams() });

  request(url, function (err, resp, body) {
    if(err) return cb(err);

    self.parser.parseString(body, function (err, result) {
      if(err) return cb(err);

      if(resp.statusCode != 200) return cb(new Error(result.Error.Code + ": " + result.Error.Message));
        
      self.credentials = result.GetSessionTokenResult.Credentials;
    <commit_msg>refactored to using define property for setting/getting credentials<commit_after>var request = require('request'),
    crypto = require('crypto'),
    URL = require('url'),
    querystring = require('querystring'),
    xml2js = require('xml2js');

var defaults = {
  host    : "sts.amazonaws.com",
  version : "2011-06-15",
  duration : 43200, // 12 hours
  algorithm : "HmacSHA256",
  signatureVersion : 2
};

var STS = exports.STS = function STS(config) {
  if ( !config.accessKeyId || ! config.secretAccessKey) {
    throw new Error('You must set the AWS credentials: accessKeyId + secretAccessKey');
  }

  this.config = config;
  this.parser = new xml2js.Parser();

  var credentials = null;

  Object.defineProperty(this, "credentials", {
    
    get : function () {
      if(credentials && credentials.expiration > new Date()) {
        return credentials;
      } else {
        return null;
      }
    },

    set : function(resp) {
      credentials = {
        sessionToken : resp.SessionToken,
        secretAccessKey : resp.SecretAccessKey,
        expiration : new Date(resp.Expiration),
        accessKeyId : resp.AccessKeyId
      };
    }
  });

};

STS.prototype.getSessionToken = function(cb) {
  if(this.credentials) return cb(null, this.credentials);

  var self = this;
  
  var url = URL.format({host:defaults.host, protocol: "https", query : this._buildParams() });

  request(url, function (err, resp, body) {
    if(err) return cb(err);

    self.parser.parseString(body, function (err, result) {
      if(err) return cb(err);

      if(resp.statusCode != 200) return cb(new Error(result.Error.Code + ": " + result.Error.Message));
        
      self.credentials = result.GetSessionTokenResult.Credentials;
    
{'fraction_non_alphanumeric': 0.13525433695971772, 'fraction_numerical': 0.014701558365186709, 'mean_word_length': 3.226086956521739, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '5339183', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1028, 'n_words': 235}