'use strict';

const { addOauthHeader } = require('@clevercloud/client/cjs/oauth.node.js');
const { prefixUrl } = require('@clevercloud/client/cjs/prefix-url.js');
const { request } = require('@clevercloud/client/cjs/request.superagent.js');
const { execWarpscript } = require('@clevercloud/client/cjs/request-warp10.superagent.js');
const { conf, loadOAuthConf } = require('../models/configuration.js');

async function loadTokens () {
  const tokens = await loadOAuthConf().toPromise();
  return {
    OAUTH_CONSUMER_KEY: conf.OAUTH_CONSUMER_KEY,
    OAUTH_CONSUMER_SECRET: conf.OAUTH_CONSUMER_SECRET,
    API_OAUTH_TOKEN: tokens.token,
    API_OAUTH_TOKEN_SECRET: tokens.secret,
  };
}

async function sendToApi (requestParams) {
  const tokens = await loadTokens();
  return Promise.resolve(requestParams)
    .then(prefixUrl(conf.API_HOST))
    .then(addOauthHeader(tokens))
    .then((requestParams) => request(requestParams, { retry: 1 }));
}

function sendToWarp10 (requestParams) {
  return Promise.resolve(requestParams)
    .then(prefixUrl(conf.WARP_10_EXEC_URL))
    .then((requestParams) => execWarpscript(requestParams, { retry: 1 }));
}

async function getHostAndTokens () {
  const tokens = await loadTokens();
  return {
    apiHost: conf.API_HOST,
    tokens,
  };
}

module.exports = { sendToApi, sendToWarp10, getHostAndTokens };

@@ -8,16 +8,56 @@
 rict';%0A%0A
+const Logger = require('../logger.js');%0A
 const %7B 
@@ -137,65 +137,64 @@
 t %7B 
-prefixUrl %7D = require('@clevercloud/client/cjs/prefix-url
+conf, loadOAuthConf %7D = require('../models/configuration
 .js'
@@ -204,22 +204,29 @@
 const %7B 
-reques
+execWarpscrip
 t %7D = re
@@ -263,16 +263,23 @@
 /request
+-warp10
 .superag
@@ -292,38 +292,33 @@
 ');%0Aconst %7B 
-execWarpscript
+prefixUrl
  %7D = require
@@ -347,104 +347,96 @@
 cjs/
+p
 re
-quest-warp10.superagent.js');%0Aconst %7B conf, loadOAuthConf %7D = require('../models/configuration
+fix-url.js');%0Aconst %7B request %7D = require('@clevercloud/client/cjs/request.superagent
 .js'
@@ -913,16 +913,261 @@
 okens))%0A
+    .then((requestParams) =%3E %7B%0A      if (process.env.CLEVER_VERBOSE) %7B%0A        Logger.debug(%60$%7BrequestParams.method.toUpperCase()%7D $%7BrequestParams.url%7D ? $%7BJSON.stringify(requestParams.queryParams)%7D%60);%0A      %7D%0A      return requestParams;%0A    %7D)%0A
     .the

Add logs in `sendToApi()` when `CLEVER_VERBOSE` is enabled