<commit_before>// Check for and attach token on all API requests
angular.module( "vokal.API", [ "vokal.Humps" ] )

.factory( "API", [ "$http", "$rootScope", "$location", "$q", "Humps",

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

        var apiRequest = function( method, path, requestData )
        {
            var defer   = $q.defer();
            var headers = { "AUTHORIZATION": "Token " + $rootScope.authToken };
            var options = {
                method: method,
                url: path,
                headers: headers,
                data: requestData || {},
                transformRequest: Humps.requestToSnake( $http.defaults.transformResponse ),
                transformResponse: Humps.responseToCamel( $http.defaults.transformResponse ),
                timeout: defer.promise
            };

            if( method === "postFile" )
            {
                headers[ "Content-Type" ] = undefined;  // To ensure multipart boundary is added
                options.method            = "post";
                options.headers           = headers;
                options.transformRequest  = angular.identity;
            }

            $http( options ).success( function ( data, status, headers, config )
            {
                defer.resolve( data );
            } ).error( function ( data, status, headers, config )
            {
                if( status === 401 || status === 403 )
                {
                    var loginPath = "/login/";

                    if( $location.path() !== loginPath )
                    {
                        $location.path( loginPath );
                        return;
                    }
                }
                defer.reject( data );
                
            } );

            return defer.promise;
        };

        return {
            $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>Issue with resolving promise outside of service. Added $cancel on promise object.<commit_after>// Check for and attach token on all API requests
angular.module( "vokal.API", [ "vokal.Humps" ] )

.factory( "API", [ "$http", "$rootScope", "$location", "$q", "Humps",

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

        var apiRequest = function( method, path, requestData )
        {
            var defer   = $q.defer();
            var headers = { "AUTHORIZATION": "Token " + $rootScope.authToken };
            var options = {
                method: method,
                url: path,
                headers: headers,
                data: requestData || {},
                transformRequest: Humps.requestToSnake( $http.defaults.transformResponse ),
                transformResponse: Humps.responseToCamel( $http.defaults.transformResponse ),
                timeout: defer.promise
            };

            if( method === "postFile" )
            {
                headers[ "Content-Type" ] = undefined;  // To ensure multipart boundary is added
                options.method            = "post";
                options.headers           = headers;
                options.transformRequest  = angular.identity;
            }

            $http( options ).success( function ( data, status, headers, config )
            {
                defer.resolve( data );
            } ).error( function ( data, status, headers, config )
            {
                if( status === 401 || status === 403 )
                {
                    var loginPath = "/login/";

                    if( $location.path() !== loginPath )
                    {
                        $location.path( loginPath );
                        return;
                    }
                }
                defer.reject( data );
                
            } );

            defer.promise.$cancel = function ( val )
            {
                defer.reject( val || "Request Cancelled" );
            };

            return defer.promise;
        };

        return {
            $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.12251090460838232, 'fraction_numerical': 0.0022757443580504457, 'mean_word_length': 1.414835164835165, 'pattern_counts': {'":': 2, '<': 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': '297982', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1265, 'n_words': 344}