<commit_before>/* @flow */

/**
 * This will log a message to the syslog variable
 */
export const logMessage = (additionalLogvalues: ?{[key: string]: string}): void => {
	const apigeeVariables: Array<string> = [
		'client.port',
		'client.received.start.time',
		'client.received.start.timestamp',
		'message.path',
		'message.querystring',
		'message.uri',
		'message.verb',
		'message.version',
		'message.reason.phrase',
		'message.status.code',
		'proxy.basepath',
		'proxy.client.ip',
		'proxy.pathsuffix',
		'proxy.url',
		'request.path',
		'request.querystring',
		'request.uri',
		'request.url',
		'request.verb',
		'request.version',
		'servicecallout.requesturi',
		'target.basepath',
		'target.copy.pathsuffix',
		'target.copy.queryparams',
		'target.name',
		'response.reason.phrase',
		'response.status.code',
		'target.received.end.time',
		'target.received.end.timestamp',
		'target.received.start.time',
		'target.received.start.timestamp',
		'target.host',
		'target.ip',
		'target.port',
		'target.scheme',
		'error.content',
		'error.message',
		'error.state',
		'log.message',
		'application.basepath',
		'proxy.name',
		'proxy.basepath',
		'proxy.pathsuffix',
		'target.basepath',
		'target.url',
		'organization.name',
		'apiproxy.name',
		'apiproxy.revision',
		'environment.name',
		'messageid',
	];
	let syslogMessage: Object = apigeeVariables.reduce((message: Object, key: string): Object => ({
		...message,
		[key]: context.getVariable(key),
	}), {});

	if (additionalLogvalues) {
		syslogMessage = {
			...syslogMessage,
			...additionalLogvalues,
		};
	}

	context.setVariable('log.syslog.message', JSON.stringify(syslogMessage));
};

/**
 * This will get the response from the proxy
 * @param  settings                   Object containing the settings for getting the response from the proxy
 * @param  settings.characterEncoding Optionally convert the response to UTF-8
 * @return                            The response from the targetted API
 */
export const getProxyResponse = ({characterEncoding = ''}: {characterEncoding?: string} = {characterEncoding: ''}): any => {
	const proxyResponse: string = characterEncoding === 'UTF-8' ? unescape(encodeURIComponent(context.proxyResponse.content)) : context.proxyResponse.content;

	return JSON.parse(proxyResponse);
};

/**
 * This will set the response to the provided content
 * @param  content              The response to set for the proxy
 * @param  settings             Object containing the settings for setting the response
 * @param  settings.contentType An optional contenttype header to set for the response
 */
export const setResponse = (content: any, {
	contentType,
}: {
	contentType: ?string,
} = {
	contentType: undefined,
}): void => {
	context.proxyResponse.content = JSON.stringify(content);

	if (contentType) {
		context.setVariable('response.header.content-type', contentType);
	}
};
// {contentType: string}

/**
 * This will get a value from the Apigee flow
 * @param  key                    The key the value to get is stored with
 * @param  settings               Object containing the settings for getting the variable
 * @param  settings.prefix        A prefix which is used to store the value with
 * @param  settings.defaultValue  The value to return when no value is found<commit_msg>Fixed issue with invalid JSON being returned by endpoint<commit_after>/* @flow */

/**
 * This will log a message to the syslog variable
 */
export const logMessage = (additionalLogvalues: ?{[key: string]: string}): void => {
	const apigeeVariables: Array<string> = [
		'client.port',
		'client.received.start.time',
		'client.received.start.timestamp',
		'message.path',
		'message.querystring',
		'message.uri',
		'message.verb',
		'message.version',
		'message.reason.phrase',
		'message.status.code',
		'proxy.basepath',
		'proxy.client.ip',
		'proxy.pathsuffix',
		'proxy.url',
		'request.path',
		'request.querystring',
		'request.uri',
		'request.url',
		'request.verb',
		'request.version',
		'servicecallout.requesturi',
		'target.basepath',
		'target.copy.pathsuffix',
		'target.copy.queryparams',
		'target.name',
		'response.reason.phrase',
		'response.status.code',
		'target.received.end.time',
		'target.received.end.timestamp',
		'target.received.start.time',
		'target.received.start.timestamp',
		'target.host',
		'target.ip',
		'target.port',
		'target.scheme',
		'error.content',
		'error.message',
		'error.state',
		'log.message',
		'application.basepath',
		'proxy.name',
		'proxy.basepath',
		'proxy.pathsuffix',
		'target.basepath',
		'target.url',
		'organization.name',
		'apiproxy.name',
		'apiproxy.revision',
		'environment.name',
		'messageid',
	];
	let syslogMessage: Object = apigeeVariables.reduce((message: Object, key: string): Object => ({
		...message,
		[key]: context.getVariable(key),
	}), {});

	if (additionalLogvalues) {
		syslogMessage = {
			...syslogMessage,
			...additionalLogvalues,
		};
	}

	context.setVariable('log.syslog.message', JSON.stringify(syslogMessage));
};

/**
 * This will get the response from the proxy
 * @param  settings                   Object containing the settings for getting the response from the proxy
 * @param  settings.characterEncoding Optionally convert the response to UTF-8
 * @return                            The response from the targetted API
 */
export const getProxyResponse = ({characterEncoding = ''}: {characterEncoding?: string} = {characterEncoding: ''}): any => {
	const proxyResponse: string = characterEncoding === 'UTF-8' ? unescape(encodeURIComponent(context.proxyResponse.content)) : context.proxyResponse.content;

	try {
		return JSON.parse(proxyResponse);
	} catch (e) {
		return {};
	}
};

/**
 * This will set the response to the provided content
 * @param  content              The response to set for the proxy
 * @param  settings             Object containing the settings for setting the response
 * @param  settings.contentType An optional contenttype header to set for the response
 */
export const setResponse = (content: any, {
	contentType,
}: {
	contentType: ?string,
} = {
	contentType: undefined,
}): void => {
	context.proxyResponse.content = JSON.stringify(content);

	if (contentType) {
		context.setVariable('response.header.content-type', contentType);
	}
};
// {contentType: string}

/**
 * This will get a value from the Apigee flow
 * @param  key                    The key the value to get is stored with
 * @param  settings               Object containing the settings for getting the variable
 * @param  settings.prefix        A prefix which is used to store the value with
 * @param  settings.defaultValue  The value to return when no value is found
{'fraction_non_alphanumeric': 0.13791554357592092, 'fraction_numerical': 0.0005989817310572028, 'mean_word_length': 3.644645340751043, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '2708709', 'n_tokens_mistral': 2146, 'n_tokens_neox': 1991, 'n_words': 519}