<commit_before>// TODO: jquery
function initScript() {
    console.warn('INIT SCRIPT')
    console.warn('value: ', document.getElementById( 'cert-request' ).value);
    
    if ( $('#gencert-url').val() === "" ) {
        alert( 'Enter a GenCert URL.' );
        return;
    }

    if ( $('#cert-request').val() === "" ) {
        alert( 'Enter valid javascript in the script box.' );
        return;
    }

    // TODO: token validation

    var script = document.createElement( 'script') ;
    script.onload = function () {
    
        try {
            eval( document.getElementById( 'cert-request' ).value );
        } 
        catch ( e ) {
            if ( e instanceof SyntaxError ) {
                alert( e.message );
            }
        }
        document.getElementById( 'gencert_test' ).style.display = 'none';
        document.getElementById( 'cert-demo-back' ).style.display = 'block';
    };
    
    script.src = document.getElementById( 'gencert-url' ).value + "/Gencert2/js";
    document.head.appendChild( script );
}

function getToken() {
    if ($('#api-url' ).val() === "" ||  $('#api-user' ).val() === "" || 
        $('#api-password' ).val() === "" || $('#client-id' ).val() === "" || 
        $('#customer-number' ).val() === "" ) {
            alert( 'You must provide all values to retrieve a token.' );
            return;
    }

    return $.ajax({
        url: $('#api-url').val() + '/v2/auth/get-token',
        type: 'POST',
        headers: {
            'Content-Type': 'application/json',
            // 'Access-Control-Allow-Headers' : "*"
            // 'x-customer-number': $('#customer-number').val(),
            "x-client-id": $('#client-id').val()
        },
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Access-Control-Allow-Headers", "x-customer-number")
            // xhr.setRequestHeader("x-client-id", $('#client-id').val());
            // xhr.setRequestHeader("x-customer-number", $('#customer-number').val());
            xhr.setRequestHeader("Authorization", "Basic " + window.btoa($('#api-user').val() + ":" + $('#api-password').val()));
        },
        success: function(result, status, xhr) {            
            if (xhr.responseText !== "") {
                alert( 'Token successfully generated.' );
                updateCertScript(result.response.token);
            } else {
                alert( 'Failed to generate a token. Please check your credentials and try again.' );
            }

            return status;
        },
        error: function(xhr, status, error) {
            if (error === 'Unauthorized') {
                alert( "Invalid Credentials. Please try again." );
            } else if (xhr.responseJson.error){
                alert( `Error: ${xhr.responseJson.error}` );
            }
            return status;
        }
    });
}

// TODO: updates when script box is cleared out
// TODO: doesn't clear token when updating exposure zone
function updateCertScript(tokenKey) {
    const exposureZone = $('#set-zone').val();    
    const token = tokenKey ? tokenKey : '';   
    const selectedOptions = $('.cert-demo-option:checked');
    let options = ``;
    
    if (selectedOptions.length > 0) {
        selectedOptions.each(function() {            
            options += `  ${this.id} : true, \n`;
        })
    } else {
        options = `  edit_purchaser: false, \n`;
    }
    
    const sampleScript = `GenCert.init(document.getElementById( 'form-container' ), { \n${options}  ship_zone: '${exposureZone}', \n  token: '${token}', \n}); \nGenCert.show();`;

    $('#cert-request').empty().text(sampleScript);
};<commit_msg>dc3345: tried getToken request a diff way, didn't work<commit_after>// TODO: jquery
function initScript() {
    console.warn('INIT SCRIPT')
    console.warn('value: ', document.getElementById( 'cert-request' ).value);
    
    if ( $('#gencert-url').val() === "" ) {
        alert( 'Enter a GenCert URL.' );
        return;
    }

    if ( $('#cert-request').val() === "" ) {
        alert( 'Enter valid javascript in the script box.' );
        return;
    }

    // TODO: token validation

    var script = document.createElement( 'script') ;
    script.onload = function () {
    
        try {
            eval( document.getElementById( 'cert-request' ).value );
        } 
        catch ( e ) {
            if ( e instanceof SyntaxError ) {
                alert( e.message );
            }
        }
        document.getElementById( 'gencert_test' ).style.display = 'none';
        document.getElementById( 'cert-demo-back' ).style.display = 'block';
    };
    
    script.src = document.getElementById( 'gencert-url' ).value + "/Gencert2/js";
    document.head.appendChild( script );
}

function getToken() {
    if ($('#api-url' ).val() === "" ||  $('#api-user' ).val() === "" || 
        $('#api-password' ).val() === "" || $('#client-id' ).val() === "" || 
        $('#customer-number' ).val() === "" ) {
            alert( 'You must provide all values to retrieve a token.' );
            return;
    }
    const apiURL = $('#api-url').val() + '/v2/auth/get-token';
    const authorization = "Basic " + window.btoa($('#api-user').val() + ":" + $('#api-password').val());
    var request = new Request(apiURL, {
        headers: new Headers({ 
            'Content-Type': 'application/json', 
            Authorization: authorization,
            'x-customer-number': $('#customer-number').val(),
            'x-client-id': $('#client-id').val(),
        })
    })
    
    return fetch(request, { 
        mode: 'cors', 
        method: 'POST',
        credentials: 'include',
        'x-customer-number': $('#customer-number').val(),
        'x-client-id': $('#client-id').val(),
        // Authorization: authorization
    })
    .then((data) => {
        console.warn("DATA: ", data.json());
        return data.json();
    })

    // return $.ajax({
    //     url: $('#api-url').val() + '/v2/auth/get-token',
    //     type: 'POST',
    //     headers: {
    //         'Content-Type': 'application/json',
    //         // 'Access-Control-Allow-Headers' : "*"
    //         // "x-customer-number": $('#customer-number').val(),
    //         // "x-client-id": $('#client-id').val()
    //     },
    //     // crossDomain: true,
    //     // xhrFields: {
    //     //     withCredentials: true
    //     // },
    //     beforeSend: function (xhr) {
    //         // xhr.setRequestHeader("Access-Control-Request-Headers", "x-customer-number, x-client-id")
    //         xhr.setRequestHeader("x-client-id", $('#client-id').val());
    //         xhr.setRequestHeader("x-customer-number", $('#customer-number').val());
    //         // xhr.withCredentials = true;
    //         xhr.setRequestHeader("Authorization", "Basic " + window.btoa($('#api-user').val() + ":" + $('#api-password').val()));
    //     },
    //     success: function(result, status, xhr) {            
    //         if (xhr.responseText !== "") {
    //             alert( 'Token successfully generated.' );
    //             updateCertScript(result.response.token);
    //         } else {
    //             alert( 'Failed to generate a token. Please check your credentials and try again.' );
    //         }

    //         return status;
    //     },
    //     error: function(xhr, status, error) {
    //         if (error === 'Unauthorized') {
    //             alert( "Invalid Credentials. Please try again." );
    //         } else if (xhr.responseJson.error){
    //             alert( `Error: ${xhr.responseJson.error}` );
    //         }
    //         return status;
    //     }
    // });
}

// TODO: updates when script box is cleared out
// TODO: doesn't clear token when updating exposure zone
function updateCertScript(tokenKey) {
    const exposureZone = $('#set-zone').val();    
    const token = tokenKey ? tokenKey : '';   
    const selectedOptions = $('.cert-demo-option:checked');
    let options = ``;
    
    if (selectedOptions.length > 0) {
        selectedOptions.each(function() {            
            options += `  ${this.id} : true, \n`;
        })
    } else {
        options = `  edit_purchaser: false, \n`;
    }
    
    const sampleScript = `GenCert.init(document.getElementById( 'form-container' ), { \n${options}  ship_zone: '${exposureZone}', \n  token: '${token}', \n}); \nGenCert.show();`;

    $('#cert-request').empty().text(sampleScript);
};
{'fraction_non_alphanumeric': 0.19086498639214294, 'fraction_numerical': 0.0013016211099278192, 'mean_word_length': 1.9245674740484429, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 6, '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': '737667', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2428, 'n_words': 505}