'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

require('babel-polyfill');

var _request = require('request');

var _request2 = _interopRequireDefault(_request);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = (() => {
  var _ref = _asyncToGenerator(function* (method, data, ...path) {
    // build URL
    let url = 'http://masterlist.jcmp.nanos.io:2507';
    path.forEach(function (p) {
      url += `/${p}`;
    });

    const options = {
      method,
      url,
      timeout: 10000
    };

    if (method === 'GET') {
      options.qs = data;
      options.useQuerystring = true;
    } else {
      options.json = true;
      options.body = data;
    }

    return new Promise(function (resolve, reject) {
      (0, _request2.default)(options, function (err, r, body) {
        if (err !== null) {
          reject({ status: 'error', message: err.message });
          return;
        }

        if (r.statusCode !== 200) {
          reject(body);
          return;
        }
        resolve(body);
      }).on('error', function (e) {
        reject({ status: 'error', message: e.message });
        return;
      });
    });
  });

  function API(_x, _x2) {
    return _ref.apply(this, arguments);
  }

  return API;
})();