<commit_before>import _ from 'lodash';

import * as Action from '../actionTypes';
import store from '../store';
import { keycloak } from '../Keycloak';

import * as Constant from '../constants';
import { resetSessionTimeoutTimer } from '../App';

var numRequestsInFlight = 0;

function incrementRequests() {
  numRequestsInFlight += 1;
  if (numRequestsInFlight === 1) {
    store.dispatch({ type: Action.REQUESTS_BEGIN });
  }
}

function decrementRequests() {
  numRequestsInFlight -= 1;
  if (numRequestsInFlight <= 0) {
    numRequestsInFlight = 0; // sanity check;
    store.dispatch({ type: Action.REQUESTS_END });
  }
}

export const HttpError = function (msg, method, path, status, body) {
  this.message = msg || '';
  this.method = method;
  this.path = path;
  this.status = status || null;
  this.body = body;
};

HttpError.prototype = Object.create(Error.prototype, {
  constructor: { value: HttpError },
});

export const ApiError = function (msg, method, path, status, errorCode, errorDescription, json) {
  this.message = msg || '';
  this.method = method;
  this.path = path;
  this.status = status || null;
  this.errorCode = errorCode || null;
  this.errorDescription = errorDescription || null;
  this.json = json || null;
};

ApiError.prototype = Object.create(Error.prototype, {
  constructor: { value: ApiError },
});

export const Resource404 = function (name, id) {
  this.name = name;
  this.id = id;
};

Resource404.prototype = Object.create(Error.prototype, {
  constructor: { value: Resource404 },
  toString: {
    value() {
      return `Resouce ${this.name} #${this.id} Not Found`;
    },
  },
});

export async function request(path, options) {
  try {
    if (await keycloak.updateToken(70)) {
      //if token expires within 70 seconds it gets updated will return true to trigger resetSessionTimeoutTimer refresh.
      resetSessionTimeoutTimer();
    }
  } catch {
    console.log('Failed to refresh the token, or the session has expired');
  }

  options = options || {};
  options.headers = Object.assign(
    {
      Authorization: `Bearer ${keycloak.token}`,
    },
    options.headers || {}
  );

  var xhr = new XMLHttpRequest();
  var method = (options.method || 'GET').toUpperCase();

  if (!options.files) {
    options.headers = Object.assign(
      {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      options.headers
    );
  }<commit_msg>token minValidity seconds to 5 after each api call<commit_after>import _ from 'lodash';

import * as Action from '../actionTypes';
import store from '../store';
import { keycloak } from '../Keycloak';

import * as Constant from '../constants';
import { resetSessionTimeoutTimer } from '../App';

var numRequestsInFlight = 0;

function incrementRequests() {
  numRequestsInFlight += 1;
  if (numRequestsInFlight === 1) {
    store.dispatch({ type: Action.REQUESTS_BEGIN });
  }
}

function decrementRequests() {
  numRequestsInFlight -= 1;
  if (numRequestsInFlight <= 0) {
    numRequestsInFlight = 0; // sanity check;
    store.dispatch({ type: Action.REQUESTS_END });
  }
}

export const HttpError = function (msg, method, path, status, body) {
  this.message = msg || '';
  this.method = method;
  this.path = path;
  this.status = status || null;
  this.body = body;
};

HttpError.prototype = Object.create(Error.prototype, {
  constructor: { value: HttpError },
});

export const ApiError = function (msg, method, path, status, errorCode, errorDescription, json) {
  this.message = msg || '';
  this.method = method;
  this.path = path;
  this.status = status || null;
  this.errorCode = errorCode || null;
  this.errorDescription = errorDescription || null;
  this.json = json || null;
};

ApiError.prototype = Object.create(Error.prototype, {
  constructor: { value: ApiError },
});

export const Resource404 = function (name, id) {
  this.name = name;
  this.id = id;
};

Resource404.prototype = Object.create(Error.prototype, {
  constructor: { value: Resource404 },
  toString: {
    value() {
      return `Resouce ${this.name} #${this.id} Not Found`;
    },
  },
});

export async function request(path, options) {
  try {
    if (await keycloak.updateToken(5)) {
      //if token expires within 70 seconds it gets updated will return true to trigger resetSessionTimeoutTimer refresh.
      resetSessionTimeoutTimer();
    }
  } catch {
    console.log('Failed to refresh the token, or the session has expired');
  }

  options = options || {};
  options.headers = Object.assign(
    {
      Authorization: `Bearer ${keycloak.token}`,
    },
    options.headers || {}
  );

  var xhr = new XMLHttpRequest();
  var method = (options.method || 'GET').toUpperCase();

  if (!options.files) {
    options.headers = Object.assign(
      {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      options.headers
    );
  }
{'fraction_non_alphanumeric': 0.14238683127572016, 'fraction_numerical': 0.007818930041152264, 'mean_word_length': 3.4070716228467814, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7457481', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1497, 'n_words': 396}