"use strict";

import Request      from "superagent";
import User         from "../stores/user";
import Constants    from "../constants";
import Dispatcher   from "../dispatcher";

const TIMEOUT = 10000;

let _pendingRequests = {};

function abortPendingRequests(key) {
  if(_pendingRequests[key]) {
    _pendingRequests[key]._callback = function() {};
    _pendingRequests[key].abort();
    _pendingRequests[key] = null;
  }
}

// Get the access token from the user
function token() {
  return User.token();
}

function makeUrl(part) {
  return GlobalSettings.apiUrl + part;
}

// GET request with a token param
function get(url) {
  return Request
    .get(url)
    .timeout(TIMEOUT)
    .set('Accept', 'application/json')
    .query({
      authtoken: token()
    });
}

// POST request with a token param
function post(url, body) {
  return Request
    .post(url)
    .send(body)
    .set('Accept', 'application/json')
    .timeout(TIMEOUT)
    .query({
      authtoken: token()
    });
}

function dispatch(key, response) {
  Dispatcher.dispatch({
    action: key,
    data: response
  });
}

// Dispatch a response based on the server response
function dispatchResponse(key) {
  return function(err, response) {
    if(err && err.timeout === TIMEOUT) {
      dispatch(Constants.TIMEOUT, response);
    } else if(response.status === 400) {
      dispatch(Constants.NOT_AUTHORIZED, response);
    } else if(!response.ok) {
      dispatch(Constants.ERROR, response);
    } else {
      dispatch(key, response);
    }
  };
}

function doRequest(key, url, callback){
  abortPendingRequests(key);
  let request = _pendingRequests[key] = callback(makeUrl(url));
  request.end(dispatchResponse(key));
  return request;
}

export default {

  get(key, url){
    return doRequest(key, url, function(fullUrl){
      return get(fullUrl);
    });
  },

  post(key, url, body){
    return doRequest(key, url, function(fullUrl){
      return post(fullUrl, body);
    });
  }

};

@@ -198,19 +198,19 @@
 10000;%0A%0A
-let
+var
  _pendin
@@ -526,32 +526,99 @@
 Url(
-part) %7B%0A  return GlobalS
+settings, part) %7B%0A  if(part.indexOf(%22http%22) %3E= 0)%7B%0A    return part;%0A  %7D else %7B%0A    return s
 etti
@@ -636,16 +636,20 @@
 + part;%0A
+  %7D%0A
 %7D%0A%0A// GE
@@ -1068,32 +1068,42 @@
 nction dispatch(
+settings, 
 key, response) %7B
@@ -1162,16 +1162,40 @@
 response
+,%0A    settings: settings
 %0A  %7D);%0A%7D
@@ -1270,24 +1270,34 @@
 tchResponse(
+settings, 
 key) %7B%0A  ret
@@ -1374,32 +1374,42 @@
 %0A      dispatch(
+settings, 
 Constants.TIMEOU
@@ -1470,32 +1470,42 @@
 %0A      dispatch(
+settings, 
 Constants.NOT_AU
@@ -1570,16 +1570,26 @@
 ispatch(
+settings, 
 Constant
@@ -1636,16 +1636,26 @@
 ispatch(
+settings, 
 key, res
@@ -1687,32 +1687,42 @@
 ction doRequest(
+settings, 
 key, url, callba
@@ -1761,11 +1761,11 @@
 ;%0A  
-let
+var
  req
@@ -1812,16 +1812,26 @@
 makeUrl(
+settings, 
 url));%0A 
@@ -1860,16 +1860,26 @@
 esponse(
+settings, 
 key));%0A 
@@ -1922,16 +1922,26 @@
 %0A%0A  get(
+settings, 
 key, url
@@ -1956,32 +1956,42 @@
 eturn doRequest(
+settings, 
 key, url, functi
@@ -2051,16 +2051,26 @@
 %0A  post(
+settings, 
 key, url
@@ -2099,16 +2099,26 @@
 Request(
+settings, 
 key, url

Include settings in calls to api.