<commit_before>/*
 * This function probes Okapi to discover what versions of what
 * interfaces are supported by the services that it is proxying
 * for. This information can be used to configure the UI at run-time
 * (e.g. not attempting to fetch loan information for a
 * non-circulating library that doesn't provide the circ interface)
 */
import 'isomorphic-fetch';

function discoverInterfaces(okapiUrl, store, entry) {
  fetch(`${okapiUrl}/_/proxy/modules/${entry.id}`)
    .then((response) => {
      if (response.status >= 400) {
        store.dispatch({ type: 'DISCOVERY_FAILURE', code: response.status });
      } else {
        response.json().then((json) => {
          store.dispatch({ type: 'DISCOVERY_INTERFACES', data: json });
        });
      }
    })
    .catch((reason) => {
      store.dispatch({ type: 'DISCOVERY_FAILURE', message: reason });
    });
}

export function discoverServices(okapiUrl, store) {
  fetch(`${okapiUrl}/_/proxy/modules`)
    .then((response) => {
      if (response.status >= 400) {
        store.dispatch({ type: 'DISCOVERY_FAILURE', code: response.status });
      } else {
        response.json().then((json) => {
          store.dispatch({ type: 'DISCOVERY_SUCCESS', data: json });
          for (const entry of json) {
            discoverInterfaces(okapiUrl, store, entry);
          }
        });
      }
    })
    .catch((reason) => {
      store.dispatch({ type: 'DISCOVERY_FAILURE', message: reason });
    });
}

export function discoveryReducer(state = {}, action) {
  switch (action.type) {
    case 'DISCOVERY_FAILURE':
      return Object.assign({}, state, { failure: action });
    case 'DISCOVERY_SUCCESS': {
      const modules = {};
      for (const entry of action.data) {
        modules[entry.id] = entry.name;
      }
      return Object.assign({}, state, { modules });
    }
    case 'DISCOVERY_INTERFACES': {
      const interfaces = {};
      for (const entry of action.data.provides) {
        interfaces[entry.id] = entry.version;
      }
      return Object.assign({}, state, {
        interfaces: Object.assign(state.interfaces || {}, interfaces),
      });
    }
    default:
      return state;
  }<commit_msg>Reverse order of catch() and then() calls on promises<commit_after>/*
 * This function probes Okapi to discover what versions of what
 * interfaces are supported by the services that it is proxying
 * for. This information can be used to configure the UI at run-time
 * (e.g. not attempting to fetch loan information for a
 * non-circulating library that doesn't provide the circ interface)
 */
import 'isomorphic-fetch';

function discoverInterfaces(okapiUrl, store, entry) {
  fetch(`${okapiUrl}/_/proxy/modules/${entry.id}`)
    .catch((reason) => {
      store.dispatch({ type: 'DISCOVERY_FAILURE', message: reason });
    }).then((response) => {
      if (response.status >= 400) {
        store.dispatch({ type: 'DISCOVERY_FAILURE', code: response.status });
      } else {
        response.json().then((json) => {
          store.dispatch({ type: 'DISCOVERY_INTERFACES', data: json });
        });
      }
    });
}

export function discoverServices(okapiUrl, store) {
  fetch(`${okapiUrl}/_/proxy/modules`)
    .catch((reason) => {
      store.dispatch({ type: 'DISCOVERY_FAILURE', message: reason });
    }).then((response) => {
      if (response.status >= 400) {
        store.dispatch({ type: 'DISCOVERY_FAILURE', code: response.status });
      } else {
        response.json().then((json) => {
          store.dispatch({ type: 'DISCOVERY_SUCCESS', data: json });
          for (const entry of json) {
            discoverInterfaces(okapiUrl, store, entry);
          }
        });
      }
    });
}

export function discoveryReducer(state = {}, action) {
  switch (action.type) {
    case 'DISCOVERY_FAILURE':
      return Object.assign({}, state, { failure: action });
    case 'DISCOVERY_SUCCESS': {
      const modules = {};
      for (const entry of action.data) {
        modules[entry.id] = entry.name;
      }
      return Object.assign({}, state, { modules });
    }
    case 'DISCOVERY_INTERFACES': {
      const interfaces = {};
      for (const entry of action.data.provides) {
        interfaces[entry.id] = entry.version;
      }
      return Object.assign({}, state, {
        interfaces: Object.assign(state.interfaces || {}, interfaces),
      });
    }
    default:
      return state;
  }
{'fraction_non_alphanumeric': 0.145578231292517, 'fraction_numerical': 0.0027210884353741495, 'mean_word_length': 2.7286559594251902, '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': '2451770', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1300, 'n_words': 332}