<commit_before>/* API service */

angular.module( "vokal.API", [ "vokal.Humps" ] )

.provider( "API", [ "$http", "$rootScope", "$q", "Humps",

    function ( $http, $rootScope, $q, Humps )
    {
        "use strict";

        // Initialize private parameters
        var globalHeaders = {};
        var rootPath      = "";
        var that          = this;

        // App config options
        this.transformHumps        = true;
        this.cancelOnRouteChange   = true;
        this.unauthorizedInterrupt = true;
        this.setHeaders = function ( headers )
        {
            angular.extend( globalHeaders, headers );
        };
        this.setRootPath = function ( path )
        {
            rootPath = String( path );
        };

        // The driver for API requests
        var apiRequest = function ( method, path, requestData )
        {
            var defer   = $q.defer();
            var options = {
                method:  method,
                url:     that.rootPath + path,
                headers: globalHeaders,
                data:    requestData || {},
                timeout: defer.promise
            };

            if( method === "postFile" )
            {
                globalHeaders[ "Content-Type" ] = undefined;  // To ensure multipart boundary is added
                options.method                  = "post";
                options.transformRequest        = angular.identity;
            }
            else if( that.transformHumps )
            {
                options.transformRequest  = Humps.requestToSnake( $http.defaults.transformRequest );
                options.transformResponse = Humps.responseToCamel( $http.defaults.transformResponse );
            }

            $http( options )

                .success( function ( data )
                {
                    $rootScope.$broadcast( "APIRequestComplete", options, data, status );
                    $rootScope.$broadcast( "APIRequestSuccess",  options, data, status );

                    defer.resolve( data );

                } )

                .error( function ( data, status )
                {
                    $rootScope.$broadcast( "APIRequestComplete", options, data, status );

                    if( status === 401 || status === 403 )
                    {
                        $rootScope.$broadcast( "APIRequestUnauthorized", options, data, status );

                        // By default, prevent resolutions for unauthorized requests to facilitate clean redirects
                        if( !that.unauthorizedInterrupt )
                        {
                            $rootScope.$broadcast( "APIRequestError", options, data, status );
                            defer.reject( data, status );
                        }
                    }
                    else
                    {
                        $rootScope.$broadcast( "APIRequestError", options, data, status );
                        defer.reject( data, status );
                    }

                } );

            $rootScope.broadcast( "APIRequestStart", options );

            // By default, cancel this API call if the application route is changed before it resolves
            if( that.cancelOnRouteChange )
            {
                var routeMonitor = $rootScope.$on( "$routeChangeSuccess", function ()
                {
                    defer.promise.$cancel( "Request canceled", options );
                    routeMonitor();
                } );

                var removeRouteMonitor = $rootScope.$on( "APIRequestComplete", function ()
                {
                    routeMonitor();
                    removeRouteMonitor();
                } );
            }

            defer.promise.$cancel = function ( message, options )
            {
                defer.reject( message || "Request cancelled", options );
            };

            return defer.promise;
        };

        // Interface methods
        this.$get = function ()
        {
            return {
                setKey: function ( key )
                {
                    globalHeaders.AUTHORIZATION = String( key );
                },
                getKey: function ()
                {
                    return globalHeaders.AUTHORIZATION;
                },
                $get:      function ( path ) {              return apiRequest( "get",      path ); },
                $post:     function ( path, requestData ) { return apiRequest( "post",     path, requestData ); },
                $postFile: function ( path, requestData ) { return apiRequest( "postFile", path, requestData ); },
                $put:      function ( path, requestData ) { return apiRequest( "put",      path, requestData ); },
                $patch:    function ( path, requestData ) { return apiRequest( "patch",    path, requestData ); },
                $delete:   function ( path ) {              return apiRequest( "delete",   path ); }
            };
        };

    }

] );<commit_msg>Reorganizing<commit_after>/* API service */

angular.module( "vokal.API", [ "vokal.Humps" ] )

.provider( "API", function ()
{
    "use strict";

    // Initialize private parameters
    var globalHeaders = {};
    var rootPath      = "";
    var that          = this;

    // App config options
    this.transformHumps        = true;
    this.cancelOnRouteChange   = true;
    this.unauthorizedInterrupt = true;
    this.setHeaders = function ( headers )
    {
        angular.extend( globalHeaders, headers );
    };
    this.setRootPath = function ( path )
    {
        rootPath = String( path );
    };

    this.$get = [ "$http", "$rootScope", "$q", "Humps", function ( $http, $rootScope, $q, Humps )
    {
        // The driver for API requests
        var apiRequest = function ( method, path, requestData )
        {
            var defer   = $q.defer();
            var options = {
                method:  method,
                url:     that.rootPath + path,
                headers: globalHeaders,
                data:    requestData || {},
                timeout: defer.promise
            };

            if( method === "postFile" )
            {
                globalHeaders[ "Content-Type" ] = undefined;  // To ensure multipart boundary is added
                options.method                  = "post";
                options.transformRequest        = angular.identity;
            }
            else if( that.transformHumps )
            {
                options.transformRequest  = Humps.requestToSnake( $http.defaults.transformRequest );
                options.transformResponse = Humps.responseToCamel( $http.defaults.transformResponse );
            }

            $http( options )

                .success( function ( data )
                {
                    $rootScope.$broadcast( "APIRequestComplete", options, data, status );
                    $rootScope.$broadcast( "APIRequestSuccess",  options, data, status );

                    defer.resolve( data );

                } )

                .error( function ( data, status )
                {
                    $rootScope.$broadcast( "APIRequestComplete", options, data, status );

                    if( status === 401 || status === 403 )
                    {
                        $rootScope.$broadcast( "APIRequestUnauthorized", options, data, status );

                        // By default, prevent resolutions for unauthorized requests to facilitate clean redirects
                        if( !that.unauthorizedInterrupt )
                        {
                            $rootScope.$broadcast( "APIRequestError", options, data, status );
                            defer.reject( data, status );
                        }
                    }
                    else
                    {
                        $rootScope.$broadcast( "APIRequestError", options, data, status );
                        defer.reject( data, status );
                    }

                } );

            $rootScope.$broadcast( "APIRequestStart", options );

            // By default, cancel this API call if the application route is changed before it resolves
            if( that.cancelOnRouteChange )
            {
                var routeMonitor = $rootScope.$on( "$routeChangeSuccess", function ()
                {
                    defer.promise.$cancel( "Request canceled", options );
                    routeMonitor();
                } );

                var removeRouteMonitor = $rootScope.$on( "APIRequestComplete", function ()
                {
                    routeMonitor();
                    removeRouteMonitor();
                } );
            }

            defer.promise.$cancel = function ( message, options )
            {
                defer.reject( message || "Request cancelled", options );
            };

            return defer.promise;
        };

        // Interface methods
        return {
            setKey: function ( key )
            {
                globalHeaders.AUTHORIZATION = String( key );
            },
            getKey: function ()
            {
                return globalHeaders.AUTHORIZATION;
            },
            $get:      function ( path ) {              return apiRequest( "get",      path ); },
            $post:     function ( path, requestData ) { return apiRequest( "post",     path, requestData ); },
            $postFile: function ( path, requestData ) { return apiRequest( "postFile", path, requestData ); },
            $put:      function ( path, requestData ) { return apiRequest( "put",      path, requestData ); },
            $patch:    function ( path, requestData ) { return apiRequest( "patch",    path, requestData ); },
            $delete:   function ( path ) {              return apiRequest( "delete",   path ); }
        };
    } ];

} );
{'fraction_non_alphanumeric': 0.10381570935660646, 'fraction_numerical': 0.0012177795818956768, 'mean_word_length': 1.2860125260960333, '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': '5346959', 'n_tokens_mistral': 2386, 'n_tokens_neox': 2207, 'n_words': 562}