const superagent = require('superagent');
const Api = require('./mirror/src/index.js');

async function example() {
    let deviceId = new Date().getTime();
    let key = '8aOzt2RmMIG0OXSyIgjM2IkHvAoa';
    let secret = 'OMxjxjaXblXdpn8E1gYFehHyx3Ea';
    
    await authorize(key, secret, deviceId);
    
    let api = new Api.SysteminfoApi();
    api.getSystemInfo().then((response) => {
        console.log(response.text);
    });
}

example();

function authorize(key, secret, deviceId) {
    let auth = new Buffer(`${key}:${secret}`).toString('base64');

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

    return request.then(res => {
        if (!res.ok) {
            return Promise.reject(res.error);
        } else {
            let client = Api.ApiClient.instance;
            let token = res.body.access_token;
            client.defaultHeaders['Authorization'] = `Bearer ${token}`;
        }
    });
}
@@ -442,16 +442,353 @@
 ple();%0A%0A
+/**%0A * Helper method which authorizes the Api client with the specified credentials %0A * %0A * @param key %7Bstring%7D Application key (OAuth2)%0A * @param secret %7Bstring%7D Application secret (OAuth2)%0A * @param deviceId %7Bstring%7Cnumber%7D Unique device id%0A * @returns Promise which resolves when the api client is authorized and ready to be used%0A */%0A
 function
@@ -831,20 +831,27 @@
     let 
-auth
+credentials
  = new B
@@ -1011,20 +1011,27 @@
 Basic $%7B
-auth
+credentials
 %7D%60%7D);%0A  

Add some example comments