<commit_before>import EventEmitter2 from 'eventemitter2';
import * as events from './events';
import PluginError from './plugin-error';
import merge from 'merge';
import { defaults as _defaults, allowedEncodings } from './defaults';

export default class Client {
  constructor(defaults) {
    const encodingIsInvalid = () => defaults &&
      typeof defaults.encoding !== 'undefined' &&
      allowedEncodings.indexOf(defaults.encoding) === -1;

    if (encodingIsInvalid()) {
      throw new Error(`${defaults.encoding} is not an allowed encoding value.`);
    }

    this.defaults = merge.recursive(true, this._getDefaults(), defaults);

    if (this.defaults.url) {
      this.defaults.sep = this.defaults.url[this.defaults.url.length - 1] === '/' ? '' : '/';
    }

    this.eventEmitter = new EventEmitter2();
    this._plugins = [];
  }

  _getDefaults() {
    return _defaults;
  }

  _callPluginMethod(method, mutableArgIdx, earlyExit, ...args) {
    let i = 0;

    const shouldContinue = () => (earlyExit ? args[mutableArgIdx] : true);

    while (i < this._plugins.length && shouldContinue()) {
      if (this._plugins[i][method]) {
        args[mutableArgIdx] = this._plugins[i][method](...args);
      }
      i += 1;
    }

    return args[mutableArgIdx];
  }

  _callOnStarts(request) {
    return this._callPluginMethod('onStart', 0, true, request);
  }

  _callOnSuccesses(request, response) {
    return this._callPluginMethod('onSuccess', 1, false, request, response);
  }

  _callOnFails(request, response) {
    return this._callPluginMethod('onFail', 1, false, request, response);
  }

  _callOnErrors(request, response) {
    return this._callPluginMethod('onError', 1, false, request, response);
  }

  _addHelpers(helpers) {
    if (helpers) {
      Object.keys(helpers).forEach((key) => {
        this[key] = helpers[key];
      });
    }
  }

  _getRequest(path, options) {
    let fullPath = path;
    if (this.defaults && this.defaults.url) {
      fullPath = `${this.defaults.url}${this.defaults.sep}${path}`;
    }

    return new Request(fullPath, options);
  }

  fetch(path, options) {
    let request;
    let onStartError;

    if (path instanceof Request) {
      request = path;
    } else {
      try {
        request = this._callOnStarts(request);
      } catch (err) {
        request = this._getRequest(path, options);
        this.eventEmitter.emit(events.REQUEST_START, request);
        onStartError = err;
      }
    }

    let fetchPromise;
    if (request && !onStartError) {
      fetchPromise = fetch(request)
        .then(response => {
          let mutatedResponse;
          if (response.status >= 400) {
            mutatedResponse = this._callOnFails(request, response);
            this.eventEmitter.emit(events.REQUEST_FAILURE, request, mutatedResponse);
          } else {
            mutatedResponse = this._callOnSuccesses(request, response);
            this.eventEmitter.emit(events.REQUEST_SUCCESS, request, mutatedResponse);
          }

          return mutatedResponse;
        })
        .catch(err => {
          const mutatedError = this._callOnErrors(request, err);
          this.eventEmitter.emit(events.REQUEST_ERROR, request, mutatedError);
          throw mutatedError;<commit_msg>Fix fetch method<commit_after>import EventEmitter2 from 'eventemitter2';
import * as events from './events';
import PluginError from './plugin-error';
import merge from 'merge';
import { defaults as _defaults, allowedEncodings } from './defaults';

export default class Client {
  constructor(defaults) {
    const encodingIsInvalid = () => defaults &&
      typeof defaults.encoding !== 'undefined' &&
      allowedEncodings.indexOf(defaults.encoding) === -1;

    if (encodingIsInvalid()) {
      throw new Error(`${defaults.encoding} is not an allowed encoding value.`);
    }

    this.defaults = merge.recursive(true, this._getDefaults(), defaults);

    if (this.defaults.url) {
      this.defaults.sep = this.defaults.url[this.defaults.url.length - 1] === '/' ? '' : '/';
    }

    this.eventEmitter = new EventEmitter2();
    this._plugins = [];
  }

  _getDefaults() {
    return _defaults;
  }

  _callPluginMethod(method, mutableArgIdx, earlyExit, ...args) {
    let i = 0;

    const shouldContinue = () => (earlyExit ? args[mutableArgIdx] : true);

    while (i < this._plugins.length && shouldContinue()) {
      if (this._plugins[i][method]) {
        args[mutableArgIdx] = this._plugins[i][method](...args);
      }
      i += 1;
    }

    return args[mutableArgIdx];
  }

  _callOnStarts(request) {
    return this._callPluginMethod('onStart', 0, true, request);
  }

  _callOnSuccesses(request, response) {
    return this._callPluginMethod('onSuccess', 1, false, request, response);
  }

  _callOnFails(request, response) {
    return this._callPluginMethod('onFail', 1, false, request, response);
  }

  _callOnErrors(request, response) {
    return this._callPluginMethod('onError', 1, false, request, response);
  }

  _addHelpers(helpers) {
    if (helpers) {
      Object.keys(helpers).forEach((key) => {
        this[key] = helpers[key];
      });
    }
  }

  _getRequest(path, options) {
    let fullPath = path;
    if (this.defaults && this.defaults.url) {
      fullPath = `${this.defaults.url}${this.defaults.sep}${path}`;
    }

    return new Request(fullPath, options);
  }

  fetch(path, options) {
    let request;
    let onStartError;

    if (path instanceof Request) {
      request = path;
    } else {
      try {
        request = this._getRequest(path, options);
        request = this._callOnStarts(request);
        this.eventEmitter.emit(events.REQUEST_START, request);
      } catch (err) {
        onStartError = err;
      }
    }

    let fetchPromise;
    if (request && !onStartError) {
      fetchPromise = fetch(request)
        .then(response => {
          let mutatedResponse;
          if (response.status >= 400) {
            mutatedResponse = this._callOnFails(request, response);
            this.eventEmitter.emit(events.REQUEST_FAILURE, request, mutatedResponse);
          } else {
            mutatedResponse = this._callOnSuccesses(request, response);
            this.eventEmitter.emit(events.REQUEST_SUCCESS, request, mutatedResponse);
          }

          return mutatedResponse;
        })
        .catch(err => {
          const mutatedError = this._callOnErrors(request, err);
          this.eventEmitter.emit(events.REQUEST_ERROR, request, mutatedError);
          throw mutatedError;
{'fraction_non_alphanumeric': 0.1327609995400889, 'fraction_numerical': 0.004292503449333129, 'mean_word_length': 2.8535144713526286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '861043', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1913, 'n_words': 403}