<commit_before>/*****************************************************************************
 * FMEServer.js
 * 2013 Safe Software
 * <EMAIL>
 *
 * Unofficial JavaScript API for FME Server >= 2014. This is not intended to
 * be a complete API like the C++, C# and Java APIs, but rather a collection
 * of boilerplate methods for REST API calls typically needed in a web page
 * interfacing with FME Server.
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 Safe Software
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 *****************************************************************************/



/**
 * Create instance of FME Server connection object
 * @this FMEServer
 * @constructor
 * @author Safe Software
 * @version 3.0
 * @param {String} svrHost Host name only, not URL
 * @param {String} token Obtained from http://yourfmeserver/fmetoken
 * @param {Number} svrPort Port, default is 80 - string
 * @param {Boolean} isSSL Connect to the server via HTTPS?
 * @return {FMEServer} FME Server connection object
 */
function FMEServer(svrHost, token, svrPort, isSSL) {

    this.svrHost = svrHost;
    this.svrPort = svrPort || '80';
    this.token = <PASSWORD>;
    this.isSSL = isSSL || false;

}


/**
 * Gets the current session id from FME Server. You can use this to get the path to any files added through
 * the file upload service.
 * @param {String} repository The repository on the FME Server
 * @param {String} wrkspName The name of the workspace on FME Server, i.e. workspace.fmw
 * @param {Function} callback Callback function accepting sessionID as a string
 */
FMEServer.prototype.getSessionID = getSessionID;
function getSessionID(repository, wrkspName, callback){
    callback = callback || _returnObject;
    var url = 'http://'+this.svrHost + '/fmedataupload/' + repository + '/' + wrkspName + '?opt_extractarchive=false&opt_pathlevel=3&opt_fullpath=true';
    
    _ajax(url, function(json) {
        callback(json.serviceResponse.session);
    });
    
}


/**
 * Returns a WebSocket connection object to the specified server
 * @param {String} stream_id A name for the desired WebSocket stream id
 * @return {WebSocket} A WebSocket connection object
 */
FMEServer.prototype.getWebSocketConnection = getWebSocketConnection;
function getWebSocketConnection(stream_id) {
    var wsConn = new WebSocket('ws://' + svrHost + ':7078/websocket');
    wsConn.onopen = function() {
        var openMsg = {
            ws_op : 'open',
            ws_stream_id : stream_id
        }
    wsConn.send(JSON.stringify(openMsg));
    };
    return wsConn;
}


/**
 * Runs a workspace using the Data Download service and returns the 
 * path to download the results in the Json results object
 * @param {String} repository The repository on the FME Server
 * @param {String} wrkspName The name of the workspace on FME Server, i.e. workspace.fmw
 * @param {String} params Any workspace-specific parameter values as a string of this form: &<paramName>=<value>&<paramName2>=<value2>
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.runDataDownload = runDataDownload;
function runDataDownload(repository, wrkspName, params, callback){
    callback = callback || _returnObject;
    var url = 'http://' + this.svrHost + '/fmedatadownload/' + repository + '/' + wrkspName + '.fmw?' + params;

    _ajax(url, function(json){
        callback(json);
    });
}


/**
 * Retrieves all available repositories on the FME Server
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.getRepositories = getRepositories;
function getRepositories(callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/repositories.json?token=' + this.token;

    _ajax(url, function(json){
        callback(json.serviceResponse.repositories.repository);
    });
}


/**
 * Retrieves all items on the FME Server for a given Repository
 * @param {String} repository The repository on the FME Server
 * @param {String} type (optional) the specific type of file item requested
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.getRepositoryItems = getRepositoryItems;
function getRepositoryItems(repository, type, callback) {
    type = type || null;
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/repositories/' + repository + '.json?token=' + this.token + '&type=' + type;
    _ajax(url, function(json){
        callback(json.serviceResponse.repository.workspaces.workspace);
    });
}


/**
 * Retrieves all published parameters for a given workspace
 * @param {String} repository The repository on the FME Server
 * @param {String} wrkspName The name of the workspace on FME Server, i.e. workspace.fmw
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.getWorkspaceParameters = getWorkspaceParameters;
function getWorkspaceParameters(repository, workspace, callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/repositories/' + repository + '/' + workspace + '/parameters.json?token=' + this.token;

    _ajax(url, function(json){
        callback(json.serviceResponse.parameters.parameter);
    });
}


/**
 * Retrieves all scheduled items
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.getSchedule = getSchedule;
function getSchedule(callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/schedules?token=' + this.token + '&accept=json&detail=high';
    var parameters = null;

    _ajax(url, function(json){
        callback(json);
    });
}


/**
 * Enables a scheduled item
 * @param {String} category Schedule category title
 * @param {String} workspace Scheduled workspace name
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.enableSchedule = enableSchedule;
function enableSchedule(category, workspace, callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/schedules/' + category + '/' + workspace + '/enabled?token=' + this.token + '&accept=json';
    var parameters = null;

    _ajax(url, function(json){
        callback(json);
    }, 'PUT', 'value=true');
}


/**
 * Disables a scheduled item
 * @param {String} category Schedule category title
 * @param {String} workspace Scheduled workspace name
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.disableSchedule = disableSchedule;
function disableSchedule(category, workspace, callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/schedules/' + category + '/' + workspace + '/enabled?token=' + this.token + '&accept=json';
    var parameters = null;
    
    _ajax(url, function(json){
        callback(json);
    }, 'PUT', 'value=false');
}<commit_msg>Replaced old REST calls with V2 API reference.<commit_after>/*****************************************************************************
 * FMEServer.js
 * 2013 Safe Software
 * <EMAIL>
 *
 * Unofficial JavaScript API for FME Server >= 2014. This is not intended to
 * be a complete API like the C++, C# and Java APIs, but rather a collection
 * of boilerplate methods for REST API calls typically needed in a web page
 * interfacing with FME Server.
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 Safe Software
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 *****************************************************************************/



/**
 * Create instance of FME Server connection object
 * @this FMEServer
 * @constructor
 * @author Safe Software
 * @version 3.0
 * @param {String} svrHost Host name only, not URL
 * @param {String} token Obtained from http://yourfmeserver/fmetoken
 * @param {Number} svrPort Port, default is 80 - string
 * @param {Boolean} isSSL Connect to the server via HTTPS?
 * @return {FMEServer} FME Server connection object
 */
function FMEServer(svrHost, token, svrPort, isSSL) {

    this.svrHost = svrHost;
    this.svrPort = svrPort || '80';
    this.token = <PASSWORD>;
    this.isSSL = isSSL || false;
    this.uriDefaults = '&accept=json&detail=high';

}


/**
 * Gets the current session id from FME Server. You can use this to get the path to any files added through
 * the file upload service.
 * @param {String} repository The repository on the FME Server
 * @param {String} wrkspName The name of the workspace on FME Server, i.e. workspace.fmw
 * @param {Function} callback Callback function accepting sessionID as a string
 */
FMEServer.prototype.getSessionID = getSessionID;
function getSessionID(repository, wrkspName, callback){
    callback = callback || _returnObject;
    var url = 'http://'+this.svrHost + '/fmedataupload/' + repository + '/' + wrkspName + '?opt_extractarchive=false&opt_pathlevel=3&opt_fullpath=true';
    
    _ajax(url, function(json) {
        callback(json.serviceResponse.session);
    });
    
}


/**
 * Returns a WebSocket connection object to the specified server
 * @param {String} stream_id A name for the desired WebSocket stream id
 * @return {WebSocket} A WebSocket connection object
 */
FMEServer.prototype.getWebSocketConnection = getWebSocketConnection;
function getWebSocketConnection(stream_id) {
    var wsConn = new WebSocket('ws://' + svrHost + ':7078/websocket');
    wsConn.onopen = function() {
        var openMsg = {
            ws_op : 'open',
            ws_stream_id : stream_id
        }
    wsConn.send(JSON.stringify(openMsg));
    };
    return wsConn;
}


/**
 * Runs a workspace using the Data Download service and returns the 
 * path to download the results in the Json results object
 * @param {String} repository The repository on the FME Server
 * @param {String} wrkspName The name of the workspace on FME Server, i.e. workspace.fmw
 * @param {String} params Any workspace-specific parameter values as a string of this form: &<paramName>=<value>&<paramName2>=<value2>
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.runDataDownload = runDataDownload;
function runDataDownload(repository, wrkspName, params, callback){
    callback = callback || _returnObject;
    var url = 'http://' + this.svrHost + '/fmedatadownload/' + repository + '/' + wrkspName + '.fmw?' + params;

    _ajax(url, function(json){
        callback(json);
    });
}


/**
 * Retrieves all available repositories on the FME Server
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.getRepositories = getRepositories;
function getRepositories(callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/repositories?token=' + this.token + this.uriDefaults;
    
    _ajax(url, function(json){
        callback(json);
    });
}


/**
 * Retrieves all items on the FME Server for a given Repository
 * @param {String} repository The repository on the FME Server
 * @param {String} type (optional) the specific type of file item requested
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.getRepositoryItems = getRepositoryItems;
function getRepositoryItems(repository, type, callback) {
    type = type || '';
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/repositories/' + repository + '/items?token=' + this.token + '&type=' + type + this.uriDefaults;
    
    _ajax(url, function(json){
        callback(json);
    });
}


/**
 * Retrieves all published parameters for a given workspace
 * @param {String} repository The repository on the FME Server
 * @param {String} wrkspName The name of the workspace on FME Server, i.e. workspace.fmw
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.getWorkspaceParameters = getWorkspaceParameters;
function getWorkspaceParameters(repository, workspace, callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/repositories/' + repository + '/items/' + workspace + '/parameters?token=' + this.token + this.uriDefaults;
    
    _ajax(url, function(json){
        callback(json);
    });
}


/**
 * Retrieves all scheduled items
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.getSchedule = getSchedule;
function getSchedule(callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/schedules?token=' + this.token + this.uriDefaults;
    var parameters = null;

    _ajax(url, function(json){
        callback(json);
    });
}


/**
 * Enables a scheduled item
 * @param {String} category Schedule category title
 * @param {String} workspace Scheduled workspace name
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.enableSchedule = enableSchedule;
function enableSchedule(category, workspace, callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/schedules/' + category + '/' + workspace + '/enabled?token=' + this.token + this.uriDefaults;
    var parameters = null;

    _ajax(url, function(json){
        callback(json);
    }, 'PUT', 'value=true');
}


/**
 * Disables a scheduled item
 * @param {String} category Schedule category title
 * @param {String} workspace Scheduled workspace name
 * @param {Function} callback Callback function accepting the json return value
 */
FMEServer.prototype.disableSchedule = disableSchedule;
function disableSchedule(category, workspace, callback) {
    callback = callback || _returnObject;
    var url = this.svrHost + '/fmerest/v2/schedules/' + category + '/' + workspace + '/enabled?token=' + this.token + this.uriDefaults;
    var parameters = null;
    
    _ajax(url, function(json){
        callback(json);
    }, 'PUT', 'value=false');
}
{'fraction_non_alphanumeric': 0.11899299311713275, 'fraction_numerical': 0.0037204687790661625, 'mean_word_length': 4.055799373040752, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3769706', 'n_tokens_mistral': 4501, 'n_tokens_neox': 4328, 'n_words': 1602}