<commit_before>'use strict';

/* Services */


// Demonstrate how to register services
// In this case it is a simple value service.
angular.module('kivame.services', []).
  factory('KivaService', ['$http', '$window' , function($http, $window) {

    var service = {};
    service.URL_KIVA_TOKEN = "/ws/auth/kiva_request/";
    service.URL_KIVAME_LOGIN = "ws/auth/kiva_access/";

    service.URL_LOAN_LIST = "/ws/all_loans";

    service.URL_KIVA_LOGIN = "https://www.kiva.org/oauth/authorize?client_id=com.kivame&response_type=code&oauth_callback=oob&state=[STATE]&oauth_token=[OAUTH_TOKEN]";

    service.TOKEN_STATE = "[STATE]";
    service.OAUTH_TOKEN = "[OAUTH_TOKEN]";

    service.handleError = function(message, result){
        alert(message);
        console.log("Error occurred:");
        console.log(message);
        console.log(result);
    };

    service.loginKivaToken = function(cb, params){
        console.log("loginKivaToken...");
        $http.get(service.URL_KIVA_TOKEN).success(function(result){
            console.log("loginKivaToken...");
            console.log(result);
            cb(result);
        }).error(function(result){
            service.handleError("An error has occured while login with Kiva.", result);
        });
    };

    service.redirectToKivaLogin = function(cb, params){
        var tmpUrl = service.normalizeKivaRedirectUrl(service.URL_KIVA_LOGIN, params);
        $window.open(tmpUrl);
        cb();
    };

    service.normalizeKivaRedirectUrl = function(argUrl, argParams){
        return argUrl.replace(service.OAUTH_TOKEN, argParams.oauthToken)
                     .replace(service.TOKEN_STATE, "kivame");
    }

    service.loginKivame = function(cb, params){
        $http.post(service.URL_KIVAME_LOGIN, params)
        .success(function(result){
            console.log("loginKivame...");
            console.log(result);
            cb(result);
        }).error(function(result){
            service.handleError("An error has occured while login with Kiva.", result);
        });

    };

    service.getRecommendedLoans = function(cb, params){
        console.log('getRecommendedLoans...');
        console.log(params);
        var postParam = undefined;
        if(params && params.authResponse){
           postParam = {params: {token: params.authResponse.accessToken} };
        }

        $http.post(service.URL_LOAN_LIST, null ,postParam)
        .success(function(result){
            console.log("allloanList...");
            console.log(result);
            cb(result);
        }).error(function(result){
            service.handleError("An error has occured while login with Kiva.", result);
        });

        /*
        console.log("service.getRecommendedLoans...");
        console.log(cb);
        var result = [];
<commit_msg>Fix integration with WS by sending info as json.<commit_after>'use strict';

/* Services */


// Demonstrate how to register services
// In this case it is a simple value service.
angular.module('kivame.services', []).
  factory('KivaService', ['$http', '$window' , function($http, $window) {

    var service = {};
    service.URL_KIVA_TOKEN = "/ws/auth/kiva_request/";
    service.URL_KIVAME_LOGIN = "ws/auth/kiva_access/";

    service.URL_LOAN_LIST = "/ws/all_loans";

    service.URL_KIVA_LOGIN = "https://www.kiva.org/oauth/authorize?client_id=com.kivame&response_type=code&oauth_callback=oob&state=[STATE]&oauth_token=[OAUTH_TOKEN]";

    service.TOKEN_STATE = "[STATE]";
    service.OAUTH_TOKEN = "[OAUTH_TOKEN]";

    service.handleError = function(message, result){
        alert(message);
        console.log("Error occurred:");
        console.log(message);
        console.log(result);
    };

    service.loginKivaToken = function(cb, params){
        console.log("loginKivaToken...");
        $http.get(service.URL_KIVA_TOKEN).success(function(result){
            console.log("loginKivaToken...");
            console.log(result);
            cb(result);
        }).error(function(result){
            service.handleError("An error has occured while login with Kiva.", result);
        });
    };

    service.redirectToKivaLogin = function(cb, params){
        var tmpUrl = service.normalizeKivaRedirectUrl(service.URL_KIVA_LOGIN, params);
        $window.open(tmpUrl);
        cb();
    };

    service.normalizeKivaRedirectUrl = function(argUrl, argParams){
        return argUrl.replace(service.OAUTH_TOKEN, argParams.oauthToken)
                     .replace(service.TOKEN_STATE, "kivame");
    }

    service.loginKivame = function(cb, params){
        $http.post(service.URL_KIVAME_LOGIN, params)
        .success(function(result){
            console.log("loginKivame...");
            console.log(result);
            cb(result);
        }).error(function(result){
            service.handleError("An error has occured while login with Kiva.", result);
        });

    };

    service.getRecommendedLoans = function(cb, params){
        console.log('getRecommendedLoans...');
        console.log(params);
        var postBody = undefined;
        if(params && params.authResponse){
           postBody = {token: params.authResponse.accessToken};
            //params: {token: params.authResponse.accessToken}
        }

        $http.post(service.URL_LOAN_LIST, postBody)
        .success(function(result){
            console.log("allloanList...");
            console.log(result);
            cb(result);
        }).error(function(result){
            service.handleError("An error has occured while login with Kiva.", result);
        });

        /*
        console.log("service.getRecommendedLoans...");
        console.log(cb);
        var result = [];

{'fraction_non_alphanumeric': 0.14177793394237526, 'fraction_numerical': 0.0, 'mean_word_length': 2.6307397959183674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7175868', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1690, 'n_words': 277}