Skip to content

Commit

Permalink
[ResponseOps][Connectors] Common request axios utility function acros…
Browse files Browse the repository at this point in the history
…s frameworks (#139549)

* Adding common axios util

* Fixing tests

* Removing patch

* Moving config to be first param
  • Loading branch information
doakalexi authored Aug 30, 2022
1 parent f9e4c64 commit eee492a
Show file tree
Hide file tree
Showing 30 changed files with 90 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import axios, { AxiosError, AxiosResponse } from 'axios';

import { createExternalService } from './service';
import { request, createAxiosResponse } from '../lib/axios_utils';
import { request, createAxiosResponse } from '../../lib/axios_utils';
import { CasesWebhookMethods, CasesWebhookPublicConfigurationType, ExternalService } from './types';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
import { actionsConfigMock } from '../../actions_config.mock';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
jest.mock('../../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from './types';

import * as i18n from './translations';
import { request } from '../lib/axios_utils';
import { request } from '../../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../../actions_config';

export const createExternalService = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { AxiosResponse, AxiosError } from 'axios';
import { isEmpty, isObjectLike, get } from 'lodash';
import { getErrorMessage } from '../lib/axios_utils';
import { getErrorMessage } from '../../lib/axios_utils';
import * as i18n from './translations';

export const createServiceError = (error: AxiosError, message: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import axios from 'axios';

import { createExternalService } from './service';
import { request, createAxiosResponse } from '../lib/axios_utils';
import { request, createAxiosResponse } from '../../lib/axios_utils';
import { ExternalService } from './types';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
Expand All @@ -20,8 +20,8 @@ interface ResponseError extends Error {
}

jest.mock('axios');
jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
jest.mock('../../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from './types';

import * as i18n from './translations';
import { request, getErrorMessage, throwIfResponseIsNotValid } from '../lib/axios_utils';
import { request, getErrorMessage, throwIfResponseIsNotValid } from '../../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../../actions_config';

const VERSION = '2';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import axios, { AxiosResponse } from 'axios';
import { Logger } from '@kbn/core/server';
import { Services } from '../../types';
import { request } from './axios_utils';
import { request } from '../../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../../actions_config';

interface PostPagerdutyOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import axios, { AxiosResponse } from 'axios';
import { Logger } from '@kbn/core/server';
import { request } from './axios_utils';
import { request } from '../../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../../actions_config';

interface PostXmattersOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import qs from 'query-string';
import axios from 'axios';
import stringify from 'json-stable-stringify';
import { Logger } from '@kbn/core/server';
import { request } from './axios_utils';
import { request } from '../../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../../actions_config';
import { AsApiContract } from '../../../common';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import stringify from 'json-stringify-safe';
import axios, { AxiosInstance, AxiosResponse } from 'axios';
import { Logger } from '@kbn/core/server';
import { request } from './axios_utils';
import { request } from '../../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../../actions_config';
import { SendEmailOptions } from './send_email';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import axios from 'axios';

import { createExternalService, getValueTextContent, formatUpdateRequest } from './service';
import { request, createAxiosResponse } from '../lib/axios_utils';
import { request, createAxiosResponse } from '../../lib/axios_utils';
import { ExternalService } from './types';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
Expand All @@ -18,8 +18,8 @@ import { actionsConfigMock } from '../../actions_config.mock';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

jest.mock('axios');
jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
jest.mock('../../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from './types';

import * as i18n from './translations';
import { getErrorMessage, request, throwIfResponseIsNotValid } from '../lib/axios_utils';
import { getErrorMessage, request, throwIfResponseIsNotValid } from '../../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../../actions_config';

const VIEW_INCIDENT_URL = `#incidents`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import axios, { AxiosResponse } from 'axios';

import { createExternalService } from './service';
import * as utils from '../lib/axios_utils';
import * as utils from '../../lib/axios_utils';
import { ExternalService, ServiceNowITSMIncident } from './types';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
Expand All @@ -18,8 +18,8 @@ import { snExternalServiceConfig } from './config';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

jest.mock('axios');
jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
jest.mock('../../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

import * as i18n from './translations';
import { ServiceNowPublicConfigurationType, ServiceNowSecretConfigurationType } from './types';
import { request } from '../lib/axios_utils';
import { request } from '../../lib/axios_utils';
import { createServiceError, getPushedDate, prepareIncident } from './utils';

export const SYS_DICTIONARY_ENDPOINT = `api/now/table/sys_dictionary`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import axios from 'axios';

import { createExternalServiceITOM } from './service_itom';
import * as utils from '../lib/axios_utils';
import * as utils from '../../lib/axios_utils';
import { ExternalServiceITOM } from './types';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
Expand All @@ -19,8 +19,8 @@ import { itomEventParams, serviceNowChoices } from './mocks';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

jest.mock('axios');
jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
jest.mock('../../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { ServiceFactory, ExternalServiceITOM, ExecutorSubActionAddEventParams } from './types';

import { request } from '../lib/axios_utils';
import { request } from '../../lib/axios_utils';
import { createExternalService } from './service';
import { createServiceError } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import axios from 'axios';

import { createExternalServiceSIR } from './service_sir';
import * as utils from '../lib/axios_utils';
import * as utils from '../../lib/axios_utils';
import { ExternalServiceSIR } from './types';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
Expand All @@ -19,8 +19,8 @@ import { snExternalServiceConfig } from './config';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

jest.mock('axios');
jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
jest.mock('../../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { Observable, ExternalServiceSIR, ObservableResponse, ServiceFactory } from './types';

import { request } from '../lib/axios_utils';
import { request } from '../../lib/axios_utils';
import { createExternalService } from './service';
import { createServiceError } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
ServiceNowSecretConfigurationType,
} from './types';
import { FIELD_PREFIX } from './config';
import { addTimeZoneToDate, getErrorMessage } from '../lib/axios_utils';
import { addTimeZoneToDate, getErrorMessage } from '../../lib/axios_utils';
import * as i18n from './translations';
import { ActionsConfigurationUtilities } from '../../actions_config';
import { ConnectorTokenClientContract } from '../../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import axios from 'axios';
import { loggingSystemMock } from '@kbn/core/server/mocks';
import { Logger } from '@kbn/core/server';
import { actionsConfigMock } from '../../actions_config.mock';
import { request, createAxiosResponse } from '../lib/axios_utils';
import { request, createAxiosResponse } from '../../lib/axios_utils';
import { createExternalService } from './service';
import { mappings } from './mocks';
import { ExternalService } from './types';

const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

jest.mock('axios');
jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
jest.mock('../../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Logger } from '@kbn/logging';
import axios from 'axios';

import { ActionsConfigurationUtilities } from '../../actions_config';
import { getErrorMessage, request, throwIfResponseIsNotValid } from '../lib/axios_utils';
import { getErrorMessage, request, throwIfResponseIsNotValid } from '../../lib/axios_utils';
import { getBodyForEventAction } from './helpers';
import {
CreateCommentParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { ActionParamsType, ActionTypeSecretsType, getActionType, TeamsActionType
import { actionsConfigMock } from '../actions_config.mock';
import { actionsMock } from '../mocks';
import { createActionTypeRegistry } from './index.test';
import * as utils from './lib/axios_utils';
import * as utils from '../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../actions_config';

jest.mock('axios');
jest.mock('./lib/axios_utils', () => {
const originalUtils = jest.requireActual('./lib/axios_utils');
jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { map, getOrElse } from 'fp-ts/lib/Option';
import { Logger } from '@kbn/core/server';
import { getRetryAfterIntervalFromHeaders } from './lib/http_rersponse_retry_header';
import { isOk, promiseResult, Result } from './lib/result_type';
import { request } from './lib/axios_utils';
import { request } from '../lib/axios_utils';
import {
ActionType,
ActionTypeExecutorOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import {
WebhookMethods,
} from './webhook';

import * as utils from './lib/axios_utils';
import * as utils from '../lib/axios_utils';
import { ActionsConfigurationUtilities } from '../actions_config';

jest.mock('axios');
jest.mock('./lib/axios_utils', () => {
const originalUtils = jest.requireActual('./lib/axios_utils');
jest.mock('../lib/axios_utils', () => {
const originalUtils = jest.requireActual('../lib/axios_utils');
return {
...originalUtils,
request: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/builtin_action_types/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
ValidatorServices,
} from '../types';
import { ActionsConfigurationUtilities } from '../actions_config';
import { request } from './lib/axios_utils';
import { request } from '../lib/axios_utils';
import { renderMustacheString } from '../lib/mustache_renderer';
import {
AlertingConnectorFeatureId,
Expand Down Expand Up @@ -190,7 +190,7 @@ export async function executor(
url,
logger,
...basicAuth,
headers,
headers: headers ? headers : {},
data,
configurationUtilities,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { duration as momentDuration } from 'moment';
import { schema } from '@kbn/config-schema';
import getPort from 'get-port';

import { request } from '../builtin_action_types/lib/axios_utils';
import { request } from '../lib/axios_utils';
import { ByteSizeValue } from '@kbn/config-schema';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { duration as momentDuration } from 'moment';
import { schema } from '@kbn/config-schema';
import getPort from 'get-port';

import { request } from '../builtin_action_types/lib/axios_utils';
import { request } from '../lib/axios_utils';
import { ByteSizeValue } from '@kbn/config-schema';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
createAxiosResponse,
} from './axios_utils';
import { loggingSystemMock } from '@kbn/core/server/mocks';
import { actionsConfigMock } from '../../actions_config.mock';
import { getCustomAgents } from './get_custom_agents';
import { actionsConfigMock } from '../actions_config.mock';
import { getCustomAgents } from '../builtin_action_types/lib/get_custom_agents';

const TestUrl = 'https://elastic.co/foo/bar/baz';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

import { isObjectLike, isEmpty } from 'lodash';
import { AxiosInstance, Method, AxiosResponse, AxiosBasicCredentials } from 'axios';
import { AxiosInstance, Method, AxiosResponse, AxiosRequestConfig } from 'axios';
import { Logger } from '@kbn/core/server';
import { getCustomAgents } from './get_custom_agents';
import { ActionsConfigurationUtilities } from '../../actions_config';
import { getCustomAgents } from '../builtin_action_types/lib/get_custom_agents';
import { ActionsConfigurationUtilities } from '../actions_config';

export const request = async <T = unknown>({
axios,
Expand All @@ -19,24 +19,21 @@ export const request = async <T = unknown>({
data,
configurationUtilities,
headers,
...rest
...config
}: {
axios: AxiosInstance;
url: string;
logger: Logger;
method?: Method;
data?: T;
params?: unknown;
configurationUtilities: ActionsConfigurationUtilities;
headers?: Record<string, string> | null;
validateStatus?: (status: number) => boolean;
auth?: AxiosBasicCredentials;
}): Promise<AxiosResponse> => {
} & AxiosRequestConfig): Promise<AxiosResponse> => {
const { httpAgent, httpsAgent } = getCustomAgents(configurationUtilities, logger, url);
const { maxContentLength, timeout } = configurationUtilities.getResponseSettings();

return await axios(url, {
...rest,
...config,
method,
// Axios doesn't support `null` value for `headers` property.
headers: headers ?? undefined,
Expand Down
Loading

0 comments on commit eee492a

Please sign in to comment.