<commit_before>/* global angular */
'use strict';

function HttpClientService($q, $http, $rootScope, HttpRequestQueueService) {

  var methods = {};
  var credentials;
  var online = true;

  function getRequest(method, url, params, data){
    var request = {
      content: {
        method: method,
        url: url
      },
      params: params
    };
    if (data){
      request.content.data = data;
    }
    return request;
  }

  // Callbacks
  var primaryCallback, secondaryCallback, defaultCallback, onlineCallback;
  
  // Recursive method to empty the queue
  function executeRequests() {
    var headRequest = HttpRequestQueueService.getHead();
    if (headRequest){
      $http(headRequest.content).
      success(function(data /*, status, headers, config*/) {
          // First, execute callback
          if (headRequest.primary && primaryCallback){
            primaryCallback(headRequest, data);
          }else if (headRequest.secondary && secondaryCallback){
            secondaryCallback(headRequest, data, HttpRequestQueueService.getQueue());
            HttpRequestQueueService.saveQueue();
          }else if (defaultCallback){
            defaultCallback(headRequest, data, HttpRequestQueueService.getQueue());
            HttpRequestQueueService.saveQueue();
          }
          // Then remove the request from queue and release lock
          HttpRequestQueueService.remove(headRequest);

          // Execute online callback
          online = true;
          if (onlineCallback) {
            onlineCallback(online);
          }

          // Try to execute the next request in the queue
          executeRequests();
        }).
      error(function(data, status/*, headers, config*/) {
        if (status && (status === 404 || status === 502)){
            // Seems to be offline, stop processing
            HttpRequestQueueService.setOffline(headRequest);
            online = false;
            // Execute callback
            if (onlineCallback) {
              onlineCallback(online);
            }
          }else {
            // Emit unsuspected error to root scope, so that
            // it can be listened on at by the application
            $rootScope.$emit('emException', {type: 'http', status: status, data: data});
          }
        });
    }
  }


  // Methods for credentials
  methods.setCredentials = function(encodedCredentials) {
    credentials = encodedCredentials;
    $http.defaults.headers.common.Authorization = 'Basic ' + credentials;
  };

  methods.getCredentials = function() {
    return credentials;
  };

  // GET, HEAD and JSONP return chained promises
  angular.forEach(['get', 'head', 'jsonp'], function(name) {
    methods[name] = function(url) {
      return $http({method: name, url: url}).then(function(success) {
        online = true;
        if (onlineCallback) {
          onlineCallback(online);
        }
        return success;
      }, function(error){
        if (error && (error.status === 404 || error.status === 502)){
          online = false;
          if (onlineCallback) {
            onlineCallback(online);
          }<commit_msg>Include http request url into emitted error message in HttpClientService.<commit_after>/* global angular */
'use strict';

function HttpClientService($q, $http, $rootScope, HttpRequestQueueService) {

  var methods = {};
  var credentials;
  var online = true;

  function getRequest(method, url, params, data){
    var request = {
      content: {
        method: method,
        url: url
      },
      params: params
    };
    if (data){
      request.content.data = data;
    }
    return request;
  }

  // Callbacks
  var primaryCallback, secondaryCallback, defaultCallback, onlineCallback;
  
  // Recursive method to empty the queue
  function executeRequests() {
    var headRequest = HttpRequestQueueService.getHead();
    if (headRequest){
      $http(headRequest.content).
      success(function(data /*, status, headers, config*/) {
          // First, execute callback
          if (headRequest.primary && primaryCallback){
            primaryCallback(headRequest, data);
          }else if (headRequest.secondary && secondaryCallback){
            secondaryCallback(headRequest, data, HttpRequestQueueService.getQueue());
            HttpRequestQueueService.saveQueue();
          }else if (defaultCallback){
            defaultCallback(headRequest, data, HttpRequestQueueService.getQueue());
            HttpRequestQueueService.saveQueue();
          }
          // Then remove the request from queue and release lock
          HttpRequestQueueService.remove(headRequest);

          // Execute online callback
          online = true;
          if (onlineCallback) {
            onlineCallback(online);
          }

          // Try to execute the next request in the queue
          executeRequests();
        }).
      error(function(data, status, headers, config) {
        if (status && (status === 404 || status === 502)){
            // Seems to be offline, stop processing
            HttpRequestQueueService.setOffline(headRequest);
            online = false;
            // Execute callback
            if (onlineCallback) {
              onlineCallback(online);
            }
          }else {
            // Emit unsuspected error to root scope, so that
            // it can be listened on at by the application
            $rootScope.$emit('emException', {type: 'http', status: status, data: data, url: config.url});
          }
        });
    }
  }


  // Methods for credentials
  methods.setCredentials = function(encodedCredentials) {
    credentials = encodedCredentials;
    $http.defaults.headers.common.Authorization = 'Basic ' + credentials;
  };

  methods.getCredentials = function() {
    return credentials;
  };

  // GET, HEAD and JSONP return chained promises
  angular.forEach(['get', 'head', 'jsonp'], function(name) {
    methods[name] = function(url) {
      return $http({method: name, url: url}).then(function(success) {
        online = true;
        if (onlineCallback) {
          onlineCallback(online);
        }
        return success;
      }, function(error){
        if (error && (error.status === 404 || error.status === 502)){
          online = false;
          if (onlineCallback) {
            onlineCallback(online);
          }
{'fraction_non_alphanumeric': 0.10944172070219832, 'fraction_numerical': 0.003795666613949075, 'mean_word_length': 2.25643666323378, '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': '7591377', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1572, 'n_words': 435}