Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: append custom user agent string to smapi client #323

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions lib/clients/http-client.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const os = require('os');
const R = require('ramda');
const requestLib = require('request');

const DynamicConfig = require('@src/utils/dynamic-config');
const logger = require('@src/utils/logger-utility');
const urlUtils = require('@src/utils/url-utils');
const stringUtils = require('@src/utils/string-utils');
const CONSTANTS = require('@src/utils/constants');
const pkg = require('@root/package.json');

module.exports = {
request,
Expand Down Expand Up @@ -46,7 +45,7 @@ function request(options, operation, doDebug, callback) {
if (!requestOptions.headers) {
requestOptions.headers = {};
}
requestOptions.headers['User-Agent'] = resolveUserAgent();
requestOptions.headers['User-Agent'] = DynamicConfig.userAgent;

// Make request
requestLib(requestOptions, (error, response) => {
Expand Down Expand Up @@ -83,19 +82,6 @@ function putByUrl(url, payload, operation, doDebug, callback) {
});
}

/**
* Resolve User-Agent for CLI by the following the chain:
* (CLI Client)? - CLI Version - Node Verson - OS Type/Release
* CLI downstream's status is decided by the ENV_VAR "ASK_DOWNSTREAM_CLIENT"
*/
function resolveUserAgent() {
const cliUserAgentStr = `ask-cli/${pkg.version} Node/${process.version} ${os.type()}/${os.release()}`;
if (stringUtils.isNonBlankString(process.env.ASK_DOWNSTREAM_CLIENT)) {
return `${process.env.ASK_DOWNSTREAM_CLIENT} (ask-cli downstream client) ${cliUserAgentStr}`;
}
return cliUserAgentStr;
}

/**
* Form the debug info object according to the error and response from each http request
* @param {string} operation
Expand Down
1 change: 1 addition & 0 deletions lib/commands/smapi/smapi-command-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ const smapiCommandHandler = (swaggerApiOperationName, flatParamsMap, commanderTo
.withAuthEndpoint(authEndpoint)
.withApiEndpoint(smapiEndpoint)
.withRefreshTokenConfig(refreshTokenConfig)
.withCustomUserAgent(DynamicConfig.userAgent)
.client();

const paramsObject = _mapToParams(inputOpts, flatParamsMap, commanderToApiCustomizationMap);
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/dynamic-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const os = require('os');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this file not have unit test?


const CONSTANTS = require('@src/utils/constants');
const pkg = require('@root/package.json');
const stringUtils = require('@src/utils/string-utils');

function resolve(chain) {
Expand Down Expand Up @@ -40,6 +43,14 @@ class DynamicConfig {
gitCredentialHelper: `${baseUrl}/helpers/prod/git-credential-helper`,
}
}

static get userAgent() {
const cliUserAgentStr = `ask-cli/${pkg.version} Node/${process.version} ${os.type()}/${os.release()}`;
if (stringUtils.isNonBlankString(process.env.ASK_DOWNSTREAM_CLIENT)) {
return `${process.env.ASK_DOWNSTREAM_CLIENT} (ask-cli downstream client) ${cliUserAgentStr}`;
}
return cliUserAgentStr;
}
}

module.exports = DynamicConfig;