/*jshint strict:true, trailing:false, unused:true, node:true */
'use strict';

require("babel/register");

var debug = require('debug')('librarian');
var https = require('https');
var qs    = require('qs');

var Client = function(token) {
  this.token = token;
};

['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].forEach(function(method) {
  Client.prototype[method.toLowerCase()] = function(path, query, body) {
    return this.request(method, path, query, body);
  };
});

Client.prototype.request = function(method, path, query, body) {

  path = encodeURI(path);
  query = query || {};

  var headers = {
    'User-Agent'    : 'GitHub Client',
    'Accept'        : 'application/json'
  };

  if (this.token) {
    headers['Authorization'] = `token ${this.token}`;
  } else {
    query.client_id = process.env.GHCLIENT;
    query.client_secret = process.env.GHSECRET;
  }
  
  if (body) headers['Content-Type'] = 'application/json';

  var options = {
    hostname: 'api.github.com',
    port:     443,
    method:   method,
    path:     query ? path + '?' + qs.stringify(query) : path,
    headers:  headers
  };

  return new Promise(function(resolve, reject) {
    var responseHandler = function(res) {
      debug('Remaining: ' + res.headers['x-ratelimit-remaining']);

      res.setEncoding('utf8');

      var data = '';
      res.on('data', chunk => data += chunk );

      res.on('end', () => {
        try { resolve(JSON.parse(data)); } catch(err) { reject('Invalid JSON'); }
      });

      debug('GH: ' + res.statusCode + ' ' + method + ' ' + options.path);

      if ([200,201].indexOf(res.statusCode) === -1) {
        return reject(new Error(res.statusCode));
      }
    };

    var req = https.request(options, responseHandler);
    req.on('error', reject);
    if (body) req.write(JSON.stringify(body));
    req.end();
  });
};

module.exports = Client;

/*jshint strict:true, trailing:false, unused:true, node:true */
'use strict';

require("babel/register");

var debug = require('debug')('librarian');
var https = require('https');
var qs    = require('qs');

// TODO https://developer.github.com/v3/#conditional-requests

var Client = function(token) {
  this.token = token;
};

['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].forEach(function(method) {
  Client.prototype[method.toLowerCase()] = function(path, query, body) {
    return this.request(method, path, query, body);
  };
});

Client.prototype.request = function(method, path, query, body) {

  path = encodeURI(path);
  query = query || {};

  var headers = {
    'User-Agent'    : 'GitHub Client',
    'Accept'        : 'application/vnd.github.moondragon+json'
  };

  if (this.token) {
    headers['Authorization'] = `token ${this.token}`;
  } else {
    query.client_id = process.env.GHCLIENT;
    query.client_secret = process.env.GHSECRET;
  }
  
  if (body) headers['Content-Type'] = 'application/json';

  var options = {
    hostname: 'api.github.com',
    port:     443,
    method:   method,
    path:     query ? path + '?' + qs.stringify(query) : path,
    headers:  headers
  };

  return new Promise(function(resolve, reject) {
    var responseHandler = function(res) {
      debug('Remaining: ' + res.headers['x-ratelimit-remaining']);

      res.setEncoding('utf8');

      var data = '';
      res.on('data', chunk => data += chunk );

      res.on('end', () => {
        try { resolve(JSON.parse(data)); } catch(err) { reject('Invalid JSON'); }
      });

      debug('GH: ' + res.statusCode + ' ' + method + ' ' + options.path);

      if ([200,201].indexOf(res.statusCode) === -1) {
        return reject(new Error(res.statusCode));
      }
    };

    var req = https.request(options, responseHandler);
    req.on('error', reject);
    if (body) req.write(JSON.stringify(body));
    req.end();
  });
};

module.exports = Client;

Use moondragon