<commit_before>import { Component, PropTypes } from 'react'
import isEqual from 'lodash/isEqual'
import axios from 'axios'

const http = axios.create()

export default class Fetch extends Component {
  static propTypes = {
    method: PropTypes.oneOf(['get', 'post', 'put', 'delete']),
    url: PropTypes.string.isRequired,
    params: PropTypes.object,
    headers: PropTypes.object,
    body: PropTypes.object,
    lazy: PropTypes.bool,
    onResponse: PropTypes.func,
    onData: PropTypes.func,
    onError: PropTypes.func,
    children: PropTypes.func.isRequired
  }

  static defaultProps = { method: 'get' }

  static setBaseUrl = baseUrl => http.defaults.baseURL = baseUrl

  static setAuthorization = authorization => {
    http.defaults.headers.common['Authorization'] = authorization
  }

  state = { fetching: false, response: null, data: null, error: null }

  componentDidMount() {
    if (!this.props.lazy) {
      this.fetch()
    }
  }

  componentWillReceiveProps(nextProps) {
    if (!this.props.lazy && !isEqual(this.props, nextProps)) {
      this.fetch()
    }
  }

  componentWillUnmount() {
    this.willUnmount = true
  }

  fetch = (body = this.props.body) => {
    this.setState({ fetching: true }, () => {
      http.request({
        method: this.props.method,
        url: this.props.url,
        params: this.props.params,
        headers: this.props.headers,
        data: body
      }).then(response => {
        if (this.willUnmount) {
          return
        }
        this.setState({ fetching: false, response, data: response.data }, () => {
          if (this.props.onResponse) {
            this.props.onResponse(null, response)
          }
          if (this.props.onData) {
            this.props.onData(this.state.data)
          }
        })
      }).catch(error => {
        if (this.willUnmount) {
          return
        }
        this.setState({
          fetching: false,
          response: error.response || error,
          error: error.response.data
        }, () => {
          if (this.props.onResponse) {
            this.props.onResponse(error)
          }
          if (this.props.onError) {
            this.props.onError(this.state.error)
          }
        })
      })
    })
  }

  render() {
    return this.props.children({
      fetching: this.state.fetching,
      response: this.state.response,
      data: this.state.data,
      error: this.state.error,
      fetch: this.fetch
    })
  }
}<commit_msg>Fix Fetch<commit_after>import { Component, PropTypes } from 'react'
import isEqual from 'lodash/isEqual'
import axios from 'axios'

const http = axios.create()

export default class Fetch extends Component {
  static propTypes = {
    method: PropTypes.oneOf(['get', 'post', 'put', 'delete']),
    url: PropTypes.string.isRequired,
    params: PropTypes.object,
    headers: PropTypes.object,
    body: PropTypes.object,
    lazy: PropTypes.bool,
    onResponse: PropTypes.func,
    onData: PropTypes.func,
    onError: PropTypes.func,
    children: PropTypes.func.isRequired
  }

  static defaultProps = { method: 'get' }

  static setBaseUrl = baseUrl => http.defaults.baseURL = baseUrl

  static setAuthorization = authorization => {
    http.defaults.headers.common['Authorization'] = authorization
  }

  state = { fetching: false, response: null, data: null, error: null }

  componentDidMount() {
    if (!this.props.lazy) {
      this.fetch()
    }
  }

  componentWillReceiveProps(nextProps) {
    if (!this.props.lazy && !isEqual(this.props, nextProps)) {
      this.fetch()
    }
  }

  componentWillUnmount() {
    this.willUnmount = true
  }

  fetch = (body = this.props.body) => {
    this.setState({ fetching: true }, () => {
      http.request({
        method: this.props.method,
        url: this.props.url,
        params: this.props.params,
        headers: this.props.headers,
        data: body
      }).then(response => {
        if (this.willUnmount) {
          return
        }
        this.setState({ fetching: false, response, data: response.data }, () => {
          if (this.props.onResponse) {
            this.props.onResponse(null, response)
          }
          if (this.props.onData) {
            this.props.onData(this.state.data)
          }
        })
      }).catch(error => {
        if (this.willUnmount) {
          return
        }
        this.setState({
          fetching: false,
          response: error.response || error,
          error: error.response.data || error.response || error
        }, () => {
          if (this.props.onResponse) {
            this.props.onResponse(error)
          }
          if (this.props.onError) {
            this.props.onError(this.state.error)
          }
        })
      })
    })
  }

  render() {
    return this.props.children({
      fetching: this.state.fetching,
      response: this.state.response,
      data: this.state.data,
      error: this.state.error,
      fetch: this.fetch
    })
  }
}
{'fraction_non_alphanumeric': 0.13192346424974824, 'fraction_numerical': 0.0, 'mean_word_length': 2.15702479338843, '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': '1915106', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1515, 'n_words': 299}