<commit_before>import delay from 'timeout-as-promise'

export const getUrlQueryString = urlParams =>
  Object.entries(urlParams).map(([key, value]) => [key, value].map(encodeURIComponent).join('=')).join('&')

/**
 * Encapsulates the cycle of:
 * 1. sending an HTTP request to the server
 * 2. calling an onSuccess or onError handler with the response.
 * 3. setting a timer, and then clearing any notification message after a few seconds, assuming
 *    no new request was started.
 */
export class HttpRequestHelper {

  /**
   * Creates a new HttpPost helper.
   * @param url {string} The URL to send the request to.
   * @param onSuccess {function} optional handler called if server responds with status code 200
   * @param onError {function} optional handler called if server responds with a code other than 200
   * @param onClear {function} optional handler called some time after the onSuccess or onError handler is called
   * @param delayBeforeClearing {number} milliseconds delay before calling the onClear handler
   */
  constructor(url, onSuccess = null, onError = null, onClear = null, delayBeforeClearing = 3000, debug = false) {
    this.url = url
    this.httpPostId = 0
    this.onSuccess = onSuccess
    this.onError = onError
    this.onClear = onClear
    this.delayBeforeClearing = delayBeforeClearing
    this.debug = debug
  }

  /**
   * Submit an HTTP GET request.
   * @param urlParams A dictionary of key-value pairs {gene: 'ENSG00012345', chrom: '1'} to encode
   *   and append to the url as HTTP GET params (eg. "?gene=ENSG00012345&chrom=1")
   */
  get = (urlParams = {}) => {
    const urlQueryString = getUrlQueryString(urlParams)

    const p = fetch(`${this.url}?${urlQueryString}`, {
      method: 'GET',
      credentials: 'include',
    })

    return this.handlePromise(p, urlParams)
  }

  /**
   * Submit an HTTP POST request.
   * @param jsonBody The request body.
   */
  post = (jsonBody = {}) => {
    if (this.debug) {
      console.log(`${this.url} httpHelder - request: `, jsonBody)
    }
    const promise = fetch(this.url, {
      method: 'POST',
      credentials: 'include',
      body: JSON.stringify(jsonBody),
    })

    return this.handlePromise(promise, jsonBody)
  }

  /**
   * Shared code to process the Promise object returned by fetch(..)
   * @param promise
   * @param onSuccessArg
   */
  handlePromise = (promise, onSuccessArg) => {
    return promise.then((response) => {
      if (response.status === 401) {
        window.location.href = `${window.location.origin}/login?next=${window.location.href.replace(window.location.origin, '')}`
      }
      if (!response.ok) {
        console.log('ERROR: ', response.statusText, response.status, response)
        const throwJsonError = (responseJson) => {
          const message = responseJson.message || `${response.statusText.toLowerCase()} (${response.status})`
          const err = new Error(message)
          err.body = responseJson
          throw err
        }
        return response.json().then(throwJsonError, () => throwJsonError({}))<commit_msg>fix auth errors on login page<commit_after>import delay from 'timeout-as-promise'

export const getUrlQueryString = urlParams =>
  Object.entries(urlParams).map(([key, value]) => [key, value].map(encodeURIComponent).join('=')).join('&')

/**
 * Encapsulates the cycle of:
 * 1. sending an HTTP request to the server
 * 2. calling an onSuccess or onError handler with the response.
 * 3. setting a timer, and then clearing any notification message after a few seconds, assuming
 *    no new request was started.
 */
export class HttpRequestHelper {

  /**
   * Creates a new HttpPost helper.
   * @param url {string} The URL to send the request to.
   * @param onSuccess {function} optional handler called if server responds with status code 200
   * @param onError {function} optional handler called if server responds with a code other than 200
   * @param onClear {function} optional handler called some time after the onSuccess or onError handler is called
   * @param delayBeforeClearing {number} milliseconds delay before calling the onClear handler
   */
  constructor(url, onSuccess = null, onError = null, onClear = null, delayBeforeClearing = 3000, debug = false) {
    this.url = url
    this.httpPostId = 0
    this.onSuccess = onSuccess
    this.onError = onError
    this.onClear = onClear
    this.delayBeforeClearing = delayBeforeClearing
    this.debug = debug
  }

  /**
   * Submit an HTTP GET request.
   * @param urlParams A dictionary of key-value pairs {gene: 'ENSG00012345', chrom: '1'} to encode
   *   and append to the url as HTTP GET params (eg. "?gene=ENSG00012345&chrom=1")
   */
  get = (urlParams = {}) => {
    const urlQueryString = getUrlQueryString(urlParams)

    const p = fetch(`${this.url}?${urlQueryString}`, {
      method: 'GET',
      credentials: 'include',
    })

    return this.handlePromise(p, urlParams)
  }

  /**
   * Submit an HTTP POST request.
   * @param jsonBody The request body.
   */
  post = (jsonBody = {}) => {
    if (this.debug) {
      console.log(`${this.url} httpHelder - request: `, jsonBody)
    }
    const promise = fetch(this.url, {
      method: 'POST',
      credentials: 'include',
      body: JSON.stringify(jsonBody),
    })

    return this.handlePromise(promise, jsonBody)
  }

  /**
   * Shared code to process the Promise object returned by fetch(..)
   * @param promise
   * @param onSuccessArg
   */
  handlePromise = (promise, onSuccessArg) => {
    return promise.then((response) => {
      if (response.status === 401 && !window.location.href.includes('login')) {
        window.location.href = `${window.location.origin}/login?next=${window.location.href.replace(window.location.origin, '')}`
      }
      if (!response.ok) {
        console.log('ERROR: ', response.statusText, response.status, response)
        const throwJsonError = (responseJson) => {
          const message = responseJson.message || `${response.statusText.toLowerCase()} (${response.status})`
          const err = new Error(message)
          err.body = responseJson
          throw err
        }
        return response.json().then(throwJsonError, () => throwJsonError({}))
{'fraction_non_alphanumeric': 0.12099758648431215, 'fraction_numerical': 0.011263073209975865, 'mean_word_length': 3.197164078325456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6871330', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1782, 'n_words': 553}