<commit_before>const fetch = require('cross-fetch');
const crypto = require('crypto');
const qs = require('qs');

exports.get = function(params){
  return exports._send(exports._makeBaseRequest(params, 'GET'));
};

exports.post = function(params){
  return exports._send(exports._makeBaseRequest(params, 'POST'));
};

exports.delete = function(params){
  return exports._send(exports._makeBaseRequest(params, 'DELETE'));
};

exports.put = function(params){
  return exports._send(exports._makeBaseRequest(params, 'PUT'));
};

exports.patch = function(params){
  return exports._send(exports._makeBaseRequest(params, 'PATCH'));
};

function shouldOverwriteSignature(newSignature, headers){
  return newSignature && !/^Bearer/.test(headers.Authorization || '');
}

exports._makeBaseRequest = function(params, method){
  let oauthHeader = null;
  params.method = method;
  if (params.oauth) {
    oauthHeader = exports._createOAuthHeader(params, params.oauth.consumer_key, params.oauth.consumer_secret);
  }
  const headers = params.headers || {};
  if (shouldOverwriteSignature(oauthHeader, headers)) headers.Authorization = oauthHeader;
  if (params.body) headers['content-type'] = 'application/json';
  const fullUrl = [params.uri].concat(exports._querystringStringify(params.qs)).join('?');

  const result = {
    fullUrl: fullUrl,
    method: params.method,
    headers: headers
  };
  if (params.body) result.body = params.body;

  return result;
};

exports._send = function(params){
  const fetchOptions = {
    method: params.method,
    headers: params.headers
  };
  if (params.body) fetchOptions.body = JSON.stringify(params.body);
  return fetch(params.fullUrl, fetchOptions)
    .then(function(res){ 
      let result = res;
      if (res.status !== 204) { // no content. Example: delete doesn't send back any content
        const contentType = res.headers.get("content-type");
        if (contentType && contentType.indexOf("application/json") !== -1) {
          result = res.json();
        }
      } else result = null;

      return Promise.all([
        res,
        result,<commit_msg>PO-1089: fix for application json<commit_after>const fetch = require('cross-fetch');
const crypto = require('crypto');
const qs = require('qs');
const _ = require("lodash");

const applicationJsonTypes = ['application/vnd.api+json', 'application/json'];
const hasContentType = function(contentType, types){
  return _.reduce(types, function(acc, val) {
    return acc || contentType.indexOf(val) !== -1;
  }, false);
};

exports.get = function(params){
  return exports._send(exports._makeBaseRequest(params, 'GET'));
};

exports.post = function(params){
  return exports._send(exports._makeBaseRequest(params, 'POST'));
};

exports.delete = function(params){
  return exports._send(exports._makeBaseRequest(params, 'DELETE'));
};

exports.put = function(params){
  return exports._send(exports._makeBaseRequest(params, 'PUT'));
};

exports.patch = function(params){
  return exports._send(exports._makeBaseRequest(params, 'PATCH'));
};

function shouldOverwriteSignature(newSignature, headers){
  return newSignature && !/^Bearer/.test(headers.Authorization || '');
}

exports._makeBaseRequest = function(params, method){
  let oauthHeader = null;
  params.method = method;
  if (params.oauth) {
    oauthHeader = exports._createOAuthHeader(params, params.oauth.consumer_key, params.oauth.consumer_secret);
  }
  const headers = params.headers || {};
  if (shouldOverwriteSignature(oauthHeader, headers)) headers.Authorization = oauthHeader;
  if (params.body) headers['content-type'] = 'application/json';
  const fullUrl = [params.uri].concat(exports._querystringStringify(params.qs)).join('?');

  const result = {
    fullUrl: fullUrl,
    method: params.method,
    headers: headers
  };
  if (params.body) result.body = params.body;

  return result;
};

exports._send = function(params){
  const fetchOptions = {
    method: params.method,
    headers: params.headers
  };
  if (params.body) fetchOptions.body = JSON.stringify(params.body);
  return fetch(params.fullUrl, fetchOptions)
    .then(function(res){ 
      let result = res;
      if (res.status !== 204) { // no content. Example: delete doesn't send back any content
        const contentType = res.headers.get("content-type");
        if (contentType && hasContentType(contentType, applicationJsonTypes)) {
          result = res.json();
        }
      } else result = null;

      return Promise.all([
        res,
        result,
{'fraction_non_alphanumeric': 0.14463176574977818, 'fraction_numerical': 0.0026619343389529724, 'mean_word_length': 4.323494687131051, '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': '2203516', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1313, 'n_words': 282}