<commit_before>import 'isomorphic-fetch'
import { call, put, select, take } from 'redux-saga/effects'
import { AUTHENTICATION } from '../constants/action_types'

import {
  accessTokenSelector,
  shouldUseAccessTokenSelector,
  shouldUseRefreshTokenSelector,
} from './selectors'

import { refreshAuthenticationToken } from '../actions/authentication'

export function* fetchCredentials() {
  const accessToken = yield select(accessTokenSelector)
  if (yield select(shouldUseAccessTokenSelector)) {
    return {
      token: {
        access_token: accessToken,
      },
    }
  } else if (yield(select(shouldUseRefreshTokenSelector))) {
    yield put(refreshAuthenticationToken())
    // Wait for the refresh to resolve one way or another before firing
    // fetchCredentials again
    yield take([AUTHENTICATION.REFRESH_SUCCESS, AUTHENTICATION.REFRESH_FAILURE])
    return yield call(fetchCredentials)
  }

  const tokenPath = (typeof window === 'undefined') ?
        `http://localhost:${process.env.PORT || 6660}/api/webapp-token` :
        `${document.location.protocol}//${document.location.host}/api/webapp-token`

  try {
    const response = yield call(fetch, tokenPath, { credentials: 'same-origin' })
    if (response.ok) {
      // Pass response as binding for response.json
      return yield call([response, response.json])
    }
    return response
  } catch (_err) {
    return yield call(fetchCredentials)
  }
}

function checkStatus(response) {
  if (response.ok) {
    return response
  }
  const error = new Error(response.statusText)
  error.response = response
  throw error
}

export function extractJSON(response) {
  return response ? response.json() : response
}

export function* sagaFetch(path, options) {
  const response = yield call(fetch, path, options)
  checkStatus(response)

  // allow for the json to be empty for a 201/202/204
  let json = {}
  if (response.status === 200) {
    json = yield call(extractJSON, response)
  }
  return { serverResponse: response, json }
}<commit_msg>Adds a content-type check to the parsing.<commit_after>import 'isomorphic-fetch'
import { call, put, select, take } from 'redux-saga/effects'
import { AUTHENTICATION } from '../constants/action_types'

import {
  accessTokenSelector,
  shouldUseAccessTokenSelector,
  shouldUseRefreshTokenSelector,
} from './selectors'

import { refreshAuthenticationToken } from '../actions/authentication'

export function* fetchCredentials() {
  const accessToken = yield select(accessTokenSelector)
  if (yield select(shouldUseAccessTokenSelector)) {
    return {
      token: {
        access_token: accessToken,
      },
    }
  } else if (yield(select(shouldUseRefreshTokenSelector))) {
    yield put(refreshAuthenticationToken())
    // Wait for the refresh to resolve one way or another before firing
    // fetchCredentials again
    yield take([AUTHENTICATION.REFRESH_SUCCESS, AUTHENTICATION.REFRESH_FAILURE])
    return yield call(fetchCredentials)
  }

  const tokenPath = (typeof window === 'undefined') ?
        `http://localhost:${process.env.PORT || 6660}/api/webapp-token` :
        `${document.location.protocol}//${document.location.host}/api/webapp-token`

  try {
    const response = yield call(fetch, tokenPath, { credentials: 'same-origin' })
    if (response.ok) {
      // Pass response as binding for response.json
      return yield call([response, response.json])
    }
    return response
  } catch (_err) {
    return yield call(fetchCredentials)
  }
}

function checkStatus(response) {
  if (response.ok) {
    return response
  }
  const error = new Error(response.statusText)
  error.response = response
  throw error
}

export function extractJSON(response) {
  return response ? response.json() : response
}

export function* sagaFetch(path, options) {
  const response = yield call(fetch, path, options)
  checkStatus(response)

  // allow for the json to be empty for a 201/202/204
  let json = {}
  if (response.status === 200 && /application\/json/.test(response.headers.get('content-type'))) {
    json = yield call(extractJSON, response)
  }
  return { serverResponse: response, json }
}
{'fraction_non_alphanumeric': 0.11218568665377177, 'fraction_numerical': 0.007736943907156673, 'mean_word_length': 3.8160651920838182, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1826341', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1166, 'n_words': 311}