From 57d86a4afbca0075fd4d2c0a818739f8dd7efd25 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Wed, 11 Mar 2020 21:21:26 +0000 Subject: [PATCH] cleaning up http requests --- .../create_watch_service.js | 12 ++-- .../application/services/http_service.ts | 72 +++++++------------ .../services/ml_api_service/annotations.ts | 9 ++- .../ml_api_service/data_frame_analytics.ts | 9 ++- .../services/ml_api_service/datavisualizer.ts | 3 +- .../services/ml_api_service/index.ts | 18 ++--- .../services/ml_api_service/results.ts | 6 +- 7 files changed, 57 insertions(+), 72 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/application/jobs/jobs_list/components/create_watch_flyout/create_watch_service.js b/x-pack/legacy/plugins/ml/public/application/jobs/jobs_list/components/create_watch_flyout/create_watch_service.js index 32b5634b143dbf8..29e89022a550253 100644 --- a/x-pack/legacy/plugins/ml/public/application/jobs/jobs_list/components/create_watch_flyout/create_watch_service.js +++ b/x-pack/legacy/plugins/ml/public/application/jobs/jobs_list/components/create_watch_flyout/create_watch_service.js @@ -39,13 +39,12 @@ function randomNumber(min, max) { } function saveWatch(watchModel) { - const path = '/api/watcher'; - const url = `${path}/watch/${watchModel.id}`; + const path = `/api/watcher/watch/${watchModel.id}`; return http({ - url, + path, method: 'PUT', - data: watchModel.upstreamJSON, + body: JSON.stringify(watchModel.upstreamJSON), }); } @@ -187,10 +186,9 @@ class CreateWatchService { loadWatch(jobId) { const id = `ml-${jobId}`; - const path = '/api/watcher'; - const url = `${path}/watch/${id}`; + const path = `/api/watcher/watch/${id}`; return http({ - url, + path, method: 'GET', }); } diff --git a/x-pack/legacy/plugins/ml/public/application/services/http_service.ts b/x-pack/legacy/plugins/ml/public/application/services/http_service.ts index 976bab02a1eb5d5..0283041fa11485e 100644 --- a/x-pack/legacy/plugins/ml/public/application/services/http_service.ts +++ b/x-pack/legacy/plugins/ml/public/application/services/http_service.ts @@ -5,7 +5,7 @@ */ import { Observable } from 'rxjs'; -import { HttpFetchOptionsWithPath } from 'kibana/public'; +import { HttpFetchOptionsWithPath, HttpFetchOptions } from 'kibana/public'; import { getHttp } from '../util/dependency_cache'; function getResultHeaders(headers: HeadersInit): HeadersInit { @@ -16,64 +16,40 @@ function getResultHeaders(headers: HeadersInit): HeadersInit { } as HeadersInit; } -// interface HttpOptions { -// url: string; -// method: string; -// headers?: any; -// data?: any; -// } - -/** - * Function for making HTTP requests to Kibana's backend. - * Wrapper for Kibana's HttpHandler. - */ -export async function http(options: HttpFetchOptionsWithPath) { - if (!options?.path) { +function getFetchOptions( + options: HttpFetchOptionsWithPath +): { path: string; fetchOptions: HttpFetchOptions } { + if (!options.path) { throw new Error('URL path is missing'); } - - try { - let path = ''; - path = path + (options.path || ''); - const headers = getResultHeaders(options.headers ?? {}); - - const allHeaders = options.headers === undefined ? headers : { ...options.headers, ...headers }; - const body = options.body === undefined ? null : JSON.stringify(options.body); - - const payload: RequestInit = { - method: options.method || 'GET', - headers: allHeaders, + return { + path: options.path, + fetchOptions: { credentials: 'same-origin', - }; - - if (body !== null) { - payload.body = body; - } - - return await getHttp().fetch(path, payload); - } catch (e) { - throw new Error(e); - } + method: options.method || 'GET', + ...(options.body ? { body: options.body } : {}), + ...(options.query ? { query: options.query } : {}), + headers: getResultHeaders(options.headers ?? {}), + }, + }; } -interface RequestOptions extends RequestInit { - body: BodyInit | any; +/** + * Function for making HTTP requests to Kibana's backend. + * Wrapper for Kibana's HttpHandler. + */ +export async function http(options: HttpFetchOptionsWithPath): Promise { + const { path, fetchOptions } = getFetchOptions(options); + return getHttp().fetch(path, fetchOptions); } /** * Function for making HTTP requests to Kibana's backend which returns an Observable * with request cancellation support. */ -export function http$(path: string, options: RequestOptions): Observable { - const requestInit: RequestInit = { - ...options, - credentials: 'same-origin', - method: options.method || 'GET', - ...(options.body ? { body: JSON.stringify(options.body) as string } : {}), - headers: getResultHeaders(options.headers ?? {}), - }; - - return fromHttpHandler(path, requestInit); +export function http$(options: HttpFetchOptionsWithPath): Observable { + const { path, fetchOptions } = getFetchOptions(options); + return fromHttpHandler(path, fetchOptions); } /** diff --git a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/annotations.ts b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/annotations.ts index 28d3fbd642f3382..9c334136f226da6 100644 --- a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/annotations.ts +++ b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/annotations.ts @@ -15,16 +15,19 @@ export const annotations = { latestMs: number; maxAnnotations: number; }) { - return http$<{ annotations: Record }>(`${basePath()}/annotations`, { + const body = JSON.stringify(obj); + return http$<{ annotations: Record }>({ + path: `${basePath()}/annotations`, method: 'POST', - body: obj, + body, }); }, indexAnnotation(obj: any) { + const body = JSON.stringify(obj); return http({ path: `${basePath()}/annotations/index`, method: 'PUT', - body: obj, + body, }); }, deleteAnnotation(id: string) { diff --git a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/data_frame_analytics.ts b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/data_frame_analytics.ts index 80af9a66fa44c2d..f478a97c94ae483 100644 --- a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/data_frame_analytics.ts +++ b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/data_frame_analytics.ts @@ -47,24 +47,27 @@ export const dataFrameAnalytics = { }); }, createDataFrameAnalytics(analyticsId: string, analyticsConfig: any): Promise { + const body = JSON.stringify(analyticsConfig); return http({ path: `${basePath()}/data_frame/analytics/${analyticsId}`, method: 'PUT', - body: analyticsConfig, + body, }); }, evaluateDataFrameAnalytics(evaluateConfig: any): Promise { + const body = JSON.stringify(evaluateConfig); return http({ path: `${basePath()}/data_frame/_evaluate`, method: 'POST', - body: evaluateConfig, + body, }); }, explainDataFrameAnalytics(jobConfig: any): Promise { + const body = JSON.stringify(jobConfig); return http({ path: `${basePath()}/data_frame/analytics/_explain`, method: 'POST', - body: jobConfig, + body, }); }, deleteDataFrameAnalytics(analyticsId: string): Promise { diff --git a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/datavisualizer.ts b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/datavisualizer.ts index dabdb0156f2e331..93e391ce515b996 100644 --- a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/datavisualizer.ts +++ b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/datavisualizer.ts @@ -10,10 +10,11 @@ import { basePath } from './index'; export const fileDatavisualizer = { analyzeFile(file: string, params: Record = {}) { + const body = JSON.stringify(file); return http({ path: `${basePath()}/file_data_visualizer/analyze_file`, method: 'POST', - body: file, + body, query: params, }); }, diff --git a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/index.ts b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/index.ts index d1089cded7b4e8e..4b9ebcd408e5b8e 100644 --- a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/index.ts +++ b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/index.ts @@ -95,7 +95,7 @@ export const ml = { }, addJob({ jobId, job }: { jobId: string; job: Job }) { - const body = JSON.stringify({ job }); + const body = JSON.stringify(job); return http({ path: `${basePath()}/anomaly_detectors/${jobId}`, method: 'PUT', @@ -132,7 +132,7 @@ export const ml = { }, updateJob({ jobId, job }: { jobId: string; job: Job }) { - const body = JSON.stringify({ job }); + const body = JSON.stringify(job); return http({ path: `${basePath()}/anomaly_detectors/${jobId}/_update`, method: 'POST', @@ -159,8 +159,9 @@ export const ml = { }, validateCardinality$(job: CombinedJob): Observable { - const body = JSON.stringify({ job }); - return http$(`${basePath()}/validate/cardinality`, { + const body = JSON.stringify(job); + return http$({ + path: `${basePath()}/validate/cardinality`, method: 'POST', body, }); @@ -181,7 +182,7 @@ export const ml = { }, addDatafeed({ datafeedId, datafeedConfig }: { datafeedId: string; datafeedConfig: Datafeed }) { - const body = JSON.stringify({ datafeedConfig }); + const body = JSON.stringify(datafeedConfig); return http({ path: `${basePath()}/datafeeds/${datafeedId}`, method: 'PUT', @@ -190,7 +191,7 @@ export const ml = { }, updateDatafeed({ datafeedId, datafeedConfig }: { datafeedId: string; datafeedConfig: Datafeed }) { - const body = JSON.stringify({ datafeedConfig }); + const body = JSON.stringify(datafeedConfig); return http({ path: `${basePath()}/datafeeds/${datafeedId}/_update`, method: 'POST', @@ -240,7 +241,7 @@ export const ml = { }, validateDetector({ detector }: { detector: Detector }) { - const body = JSON.stringify({ detector }); + const body = JSON.stringify(detector); return http({ path: `${basePath()}/anomaly_detectors/_validate/detector`, method: 'POST', @@ -618,7 +619,8 @@ export const ml = { esSearch$(obj: any): Observable { const body = JSON.stringify(obj); - return http$(`${basePath()}/es_search`, { + return http$({ + path: `${basePath()}/es_search`, method: 'POST', body, }); diff --git a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/results.ts b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/results.ts index e61c5676912dc45..2e5cc8af04d3606 100644 --- a/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/results.ts +++ b/x-pack/legacy/plugins/ml/public/application/services/ml_api_service/results.ts @@ -42,7 +42,8 @@ export const results = { influencersFilterQuery, }); - return http$(`${basePath()}/results/anomalies_table_data`, { + return http$({ + path: `${basePath()}/results/anomalies_table_data`, method: 'POST', body, }); @@ -91,7 +92,8 @@ export const results = { latestMs: number ): Observable { const body = JSON.stringify({ jobId, searchTerm, criteriaFields, earliestMs, latestMs }); - return http$(`${basePath()}/results/partition_fields_values`, { + return http$({ + path: `${basePath()}/results/partition_fields_values`, method: 'POST', body, });