<commit_before>/*
 * Fitbit-js
 *
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 */

var url = require('url');
var http = require('http');
var OAuth = require('oauth').OAuth;
var querystring = require('querystring');
var Serializer = require('serializer');

var baseURI = 'https://api.fitbit.com/1';

module.exports = function (api_key, api_secret, callbackURI, unit_system) {
    var client = {version: '0.2.1'};
    var serializer = Serializer.createSecureSerializer(api_key, api_secret);
    var headers = {'Accept': '*/*', 'Connection': 'close', 'User-Agent': 'fitbit-js ' + client.version};

    if (typeof unit_system !== 'undefined' && unit_system !== null) headers['Accept-Language'] = unit_system;

    var oAuth = new OAuth('https://api.fitbit.com/oauth/request_token',
                          'https://api.fitbit.com/oauth/access_token',
                          api_key, api_secret, '1.0', callbackURI,
                          'HMAC-SHA1', null, headers);

    function requestCallback(callback) {
      return function (err, data, response) {
        if (err) return callback(err, data);
        var exception = null;
        try {
          data = JSON.parse(data);
        } catch (e) { exception = e; }
        callback(exception, response, data);
      };
    }

    function get(path, params, token, callback) {
      oAuth.get(baseURI + path + '?' + querystring.stringify(params),
                token.oauth_token, token.oauth_token_secret, requestCallback(callback));
    }

    function post(path, params, token, callback) {
      oAuth.post(baseURI + path, token.oauth_token, token.oauth_token_secret,
                 params, null, requestCallback(callback));
    }

    // PUBLIC
    client.apiCall = function (method, path, params, callback) {
      var token = params.token;
      delete params.token;
      if (method === 'GET') get(path, params, token, callback);
      else if (method === 'POST') post(path, params, token, callback);
    };

    client.getAccessToken = function (req, res, callback) {
      var sess;
      if(req.cookies && req.cookies.fitbit_client) {
        try { sess = serializer.parse(req.cookies.fitbit_client); }catch(E){ }
      }
      var qs = url.parse(req.url, true).query;

      var has_token = qs && qs.oauth_token,
          has_secret = sess && sess.token_secret;

      if(has_token &&  has_secret) { // Access token
        oAuth.getOAuthAccessToken(qs.oauth_token,
                                  sess.tokenSecret,
                                  qs.oauth_verifier,
        function (error, oauth_token, oauth_token_secret, additionalParameters) {
          if (error) return callback(error, null);
          callback(null, {oauth_token: oauth_token, oauth_token_secret: oauth_token_secret});
        });
      } else { // Request token
        oAuth.getOAuthRequestToken({oauth_callback: callbackURI},
          function (error, oauth_token, oauth_token_secret, oauth_authorize_url, additionalParameters) {
          if (error) return callback(error, null);<commit_msg>Add delete api call method<commit_after>/*
 * Fitbit-js
 *
 * Copyright (c) 2011 <NAME> <<EMAIL>>
 */

var url = require('url');
var http = require('http');
var OAuth = require('oauth').OAuth;
var querystring = require('querystring');
var Serializer = require('serializer');

var baseURI = 'https://api.fitbit.com/1';

module.exports = function (api_key, api_secret, callbackURI, unit_system) {
    var client = {version: '0.2.1'};
    var serializer = Serializer.createSecureSerializer(api_key, api_secret);
    var headers = {'Accept': '*/*', 'Connection': 'close', 'User-Agent': 'fitbit-js ' + client.version};

    if (typeof unit_system !== 'undefined' && unit_system !== null) headers['Accept-Language'] = unit_system;

    var oAuth = new OAuth('https://api.fitbit.com/oauth/request_token',
                          'https://api.fitbit.com/oauth/access_token',
                          api_key, api_secret, '1.0', callbackURI,
                          'HMAC-SHA1', null, headers);

    function requestCallback(callback) {
      return function (err, data, response) {
        if (err) return callback(err, data);
        var exception = null;
        try {
          data = JSON.parse(data);
        } catch (e) { exception = e; }
        callback(exception, response, data);
      };
    }

    function get(path, params, token, callback) {
      oAuth.get(baseURI + path + '?' + querystring.stringify(params),
                token.oauth_token, token.oauth_token_secret, requestCallback(callback));
    }

    function post(path, params, token, callback) {
      oAuth.post(baseURI + path, token.oauth_token, token.oauth_token_secret,
                 params, null, requestCallback(callback));
    }
    
    function oAuthDelete(path, params, token, callback){
      oAuth.delete(baseURI + path, token.oauth_token, token.oauth_token_secret, deleteCallback(callback));
    }

    // PUBLIC
    client.apiCall = function (method, path, params, callback) {
      var token = params.token;
      delete params.token;
      if (method === 'GET') get(path, params, token, callback);
      else if (method === 'POST') post(path, params, token, callback);
      else if (method === 'DELETE') oAuthDelete(path, params, token, callback);
    };

    client.getAccessToken = function (req, res, callback) {
      var sess;
      if(req.cookies && req.cookies.fitbit_client) {
        try { sess = serializer.parse(req.cookies.fitbit_client); }catch(E){ }
      }
      var qs = url.parse(req.url, true).query;

      var has_token = qs && qs.oauth_token,
          has_secret = sess && sess.token_secret;

      if(has_token &&  has_secret) { // Access token
        oAuth.getOAuthAccessToken(qs.oauth_token,
                                  sess.tokenSecret,
                                  qs.oauth_verifier,
        function (error, oauth_token, oauth_token_secret, additionalParameters) {
          if (error) return callback(error, null);
          callback(null, {oauth_token: oauth_token, oauth_token_secret: oauth_token_secret});
        });
      } else { // Request token
        oAuth.getOAuthRequestToken({oauth_callback: callbackURI},
          function (error, oauth_token, oauth_token_secret, oauth_authorize_url, additionalParameters) {
          if (error) return callback(error, null);
{'fraction_non_alphanumeric': 0.13228346456692913, 'fraction_numerical': 0.0034645669291338585, 'mean_word_length': 2.718384074941452, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7350567', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1885, 'n_words': 429}