<commit_before>import { HttpError } from './error';
import log, { eventTypes } from '../log';
import uuid from 'uuid/v4';
import user from '../api-clients/user';
import notifications from '../notifications';

/**
 * 
 * @param {string} method - must match an HTTP method (eg "GET")
 * @param {string} URI - URI to make a request to
 * @param {boolean} willRetry - (default = true) if true then this function will retry the connection on failure 
 * @param {object} body - JSON of the request body (if it's an applicable HTTP method)
 * @param {function} onRetry - Runs whenever the request is going to be retried. Added for use in unit tests, so that we can run our mocked timeOuts (or else the async test breaks)
 * @param {boolean} callerHandles401 - Flag to decide whether caller or global handler is to handle 401 responses 
 * 
 * @returns {Promise} which returns the response body in JSON format
 */

export default function request(method, URI, willRetry = true, onRetry = () => {}, body, callerHandles401) {
    const baseInterval = 50;
    let interval = baseInterval;
    const maxRetries = 5;
    let retryCount = 0;

    return new Promise(function(resolve, reject) {
        tryFetch(resolve, reject, URI, willRetry, body);
    });

    function tryFetch(resolve, reject, URI, willRetry, body) {
        const UID = uuid();
        const logEventPayload = {
            method: method,
            requestID: UID,
            willRetry,
            retryCount,
            URI
        };
        const fetchConfig = {
            method,
            credentials: "include",
            headers: {
                'Content-Type': 'application/json',
                'Request-ID': UID,
                'internal-token': "FD0108EA-825D-411C-9B1D-41EF7727F465"
            }
        }

        if (method === "POST" || method === "PUT") {
            fetchConfig.body = JSON.stringify(body || {});
        }

        log.add(eventTypes.requestSent, {...logEventPayload});

        fetch(URI, fetchConfig).then(response => {
            logEventPayload.status = response.status;
            logEventPayload.message = response.message;
            log.add(eventTypes.requestReceived, logEventPayload);

            const responseIsJSON = response.headers.get('content-type').match(/application\/json/);
            const responseIsText = response.headers.get('content-type').match(/text\/plain/);

            if (response.status >= 500) {<commit_msg>Allow responses that have no content type<commit_after>import { HttpError } from './error';
import log, { eventTypes } from '../log';
import uuid from 'uuid/v4';
import user from '../api-clients/user';
import notifications from '../notifications';

/**
 * 
 * @param {string} method - must match an HTTP method (eg "GET")
 * @param {string} URI - URI to make a request to
 * @param {boolean} willRetry - (default = true) if true then this function will retry the connection on failure 
 * @param {object} body - JSON of the request body (if it's an applicable HTTP method)
 * @param {function} onRetry - Runs whenever the request is going to be retried. Added for use in unit tests, so that we can run our mocked timeOuts (or else the async test breaks)
 * @param {boolean} callerHandles401 - Flag to decide whether caller or global handler is to handle 401 responses 
 * 
 * @returns {Promise} which returns the response body in JSON format
 */

export default function request(method, URI, willRetry = true, onRetry = () => {}, body, callerHandles401) {
    const baseInterval = 50;
    let interval = baseInterval;
    const maxRetries = 5;
    let retryCount = 0;

    return new Promise(function(resolve, reject) {
        tryFetch(resolve, reject, URI, willRetry, body);
    });

    function tryFetch(resolve, reject, URI, willRetry, body) {
        const UID = uuid();
        const logEventPayload = {
            method: method,
            requestID: UID,
            willRetry,
            retryCount,
            URI
        };
        const fetchConfig = {
            method,
            credentials: "include",
            headers: {
                'Content-Type': 'application/json',
                'Request-ID': UID,
                'internal-token': "<PASSWORD>"
            }
        }

        if (method === "POST" || method === "PUT") {
            fetchConfig.body = JSON.stringify(body || {});
        }

        log.add(eventTypes.requestSent, {...logEventPayload});

        fetch(URI, fetchConfig).then(response => {
            logEventPayload.status = response.status;
            logEventPayload.message = response.message;
            log.add(eventTypes.requestReceived, logEventPayload);

            let responseIsJSON = false;
            let responseIsText = false;

            if (response.headers.get('content-type') !== null) {
                responseIsJSON = response.headers.get('content-type').match(/application\/json/);
                responseIsText = response.headers.get('content-type').match(/text\/plain/);
            }

            if (response.status >= 500) {
{'fraction_non_alphanumeric': 0.11470240441466299, 'fraction_numerical': 0.01083957430035475, 'mean_word_length': 2.4083277367360645, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 10, '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': '1548452', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1335, 'n_words': 443}