<commit_before>import { getUserToken } from '../reducers/userReducer';

let getState = null;

export function allowApiToAccessState(store) {
  getState = store.getState;
}

async function callApi(url, { method, body }) {
  const fetchOptions = {
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      Authorization: getUserToken(getState())
    },
    method
  };
  if (body) {
    fetchOptions.body = JSON.stringify(body);
  }
  const res = await fetch(`/api/${url}`, fetchOptions);
  let json = {};

  if (method !== 'DELETE') {
    json = await res.json();
  }

  if (!res.ok) {
    const errMessage = json.error ? json.error.message : res.statusText;
    throw new Error(errMessage);
  }

  return json;
}

export const get = (url, options) => callApi(url, { ...options, method: 'GET' });
export const post = (url, options) => callApi(url, { ...options, method: 'POST' });
export const patch = (url, options) => callApi(url, { ...options, method: 'PATCH' });
export const put = (url, options) => callApi(url, { ...options, method: 'PUT' });
export const del = (url, options) => callApi(url, { ...options, method: 'DELETE' });<commit_msg>Wrap JSON parse in try ... catch<commit_after>import { getUserToken } from '../reducers/userReducer';

let getState = null;

export function allowApiToAccessState(store) {
  getState = store.getState;
}

async function callApi(url, { method, body }) {
  const fetchOptions = {
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      Authorization: getUserToken(getState())
    },
    method
  };
  if (body) {
    fetchOptions.body = JSON.stringify(body);
  }
  const res = await fetch(`/api/${url}`, fetchOptions);
  let json;

  try {
    json = await res.json();
  } catch (ex) {
    json = {};
  }

  if (!res.ok) {
    const errMessage = json.error ? json.error.message : res.statusText;
    throw new Error(errMessage);
  }

  return json;
}

export const get = (url, options) => callApi(url, { ...options, method: 'GET' });
export const post = (url, options) => callApi(url, { ...options, method: 'POST' });
export const patch = (url, options) => callApi(url, { ...options, method: 'PATCH' });
export const put = (url, options) => callApi(url, { ...options, method: 'PUT' });
export const del = (url, options) => callApi(url, { ...options, method: 'DELETE' });
{'fraction_non_alphanumeric': 0.17440401505646172, 'fraction_numerical': 0.0, 'mean_word_length': 3.446096654275093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5191542', 'n_tokens_mistral': 771, 'n_tokens_neox': 762, 'n_words': 208}