<commit_before>const Api = require('./mirror/src/index.js');
const Base64 = require('Base64');
const superagent = require('superagent');

/**
 * Authorize all upcoming api calls with the specified access token
 * @param token {string}
 */
Api.setAccessToken = function(token) {
    let client = Api.ApiClient.instance;
    client.defaultHeaders['Authorization'] = 'Bearer ' + token;
};

/**
 * Fetches an access token with the specified key and secret and authorizes all 
 * all upcoming api calls with it. The key and secret should be obtained through 
 * the Västtrafik developer portal https://developer.vasttrafik.se/portal
 * 
 * @param key {string} The oauth 2 key
 * @param secret {string} The oauth 2 secret
 * @param deviceId {string?} If not set, the current time is used resulting in a new access token each call
 * @param callback
 * @returns Promise
 */
Api.authorize = function(key, secret, deviceId, callback) {
    deviceId = deviceId || new Date().getTime();
    
    let credentials = Base64.btoa(key + ':' + secret);

    let request = superagent('POST', 'https://api.vasttrafik.se/token');
    request.set({Authorization: 'Basic ' + credentials});
    request.send('grant_type=client_credentials&scope=device_' + deviceId);

    return request.then(res => {
        if (!res.ok) {
            if (callback) {
                callback(res.error, null);
            }
            return Promise.reject(res.error);
        } else {
            let token = res.body.access_token;
            Api.setAccessToken(token);

            if (callback) {
                callback(null, token);
            }
            
            return token;
        }
    });
};<commit_msg>Add vasttrafik api to exported modules...<commit_after>const Api = require('./mirror/src/index.js');
const Base64 = require('Base64');
const superagent = require('superagent');

/**
 * Authorize all upcoming api calls with the specified access token
 * @param token {string}
 */
Api.setAccessToken = function(token) {
    let client = Api.ApiClient.instance;
    client.defaultHeaders['Authorization'] = 'Bearer ' + token;
};

/**
 * Fetches an access token with the specified key and secret and authorizes all 
 * all upcoming api calls with it. The key and secret should be obtained through 
 * the Västtrafik developer portal https://developer.vasttrafik.se/portal
 * 
 * @param key {string} The oauth 2 key
 * @param secret {string} The oauth 2 secret
 * @param deviceId {string?} If not set, the current time is used resulting in a new access token each call
 * @param callback
 * @returns Promise
 */
Api.authorize = function(key, secret, deviceId, callback) {
    deviceId = deviceId || new Date().getTime();
    
    let credentials = Base64.btoa(key + ':' + secret);

    let request = superagent('POST', 'https://api.vasttrafik.se/token');
    request.set({Authorization: 'Basic ' + credentials});
    request.send('grant_type=client_credentials&scope=device_' + deviceId);

    return request.then(res => {
        if (!res.ok) {
            if (callback) {
                callback(res.error, null);
            }
            return Promise.reject(res.error);
        } else {
            let token = res.body.access_token;
            Api.setAccessToken(token);

            if (callback) {
                callback(null, token);
            }
            
            return token;
        }
    });
};

exports.module = Api;
{'fraction_non_alphanumeric': 0.11963816749343449, 'fraction_numerical': 0.0046688065363291505, 'mean_word_length': 2.70995670995671, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '4271777', 'n_tokens_mistral': 983, 'n_tokens_neox': 958, 'n_words': 291}