From bc758309df008271f8f4f35f18a45edc7424dcaa Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Tue, 27 Apr 2021 13:58:02 -0600 Subject: [PATCH] Revise index calls to use kibana https fetch. Remove http service --- .../utils/http_service.ts | 63 ------------------- .../utils/indexing_service.ts | 27 ++++---- 2 files changed, 15 insertions(+), 75 deletions(-) delete mode 100644 x-pack/plugins/maps/public/classes/layers/new_vector_layer_wizard/utils/http_service.ts diff --git a/x-pack/plugins/maps/public/classes/layers/new_vector_layer_wizard/utils/http_service.ts b/x-pack/plugins/maps/public/classes/layers/new_vector_layer_wizard/utils/http_service.ts deleted file mode 100644 index 5db916acc951b3..00000000000000 --- a/x-pack/plugins/maps/public/classes/layers/new_vector_layer_wizard/utils/http_service.ts +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { i18n } from '@kbn/i18n'; -import { HttpFetchOptions } from 'kibana/public'; -import { getHttp } from '../../../../kibana_services'; - -export interface HttpOptions { - url: string; - method: string; - headers?: { - [key: string]: any; - }; - data?: unknown; - query?: any; -} - -export async function http(options: HttpOptions) { - if (!(options && options.url)) { - throw i18n.translate('xpack.maps.layers.newVectorLayerWizard.httpService.noUrl', { - defaultMessage: 'No URL provided', - }); - } - const url: string = options.url || ''; - const headers = { - 'Content-Type': 'application/json', - ...options.headers, - }; - - const allHeaders = options.headers === undefined ? headers : { ...options.headers, ...headers }; - const body = options.data === undefined ? null : JSON.stringify(options.data); - - const payload: HttpFetchOptions = { - method: options.method || 'GET', - headers: allHeaders, - credentials: 'same-origin', - query: options.query, - }; - - if (body !== null) { - payload.body = body; - } - return await doFetch(url, payload); -} - -async function doFetch(url: string, payload: HttpFetchOptions) { - try { - return await getHttp().fetch(url, payload); - } catch (err) { - return { - failures: [ - i18n.translate('xpack.maps.layers.newVectorLayerWizard.httpService.fetchError', { - defaultMessage: 'Error performing fetch: {error}', - values: { error: err.message }, - }), - ], - }; - } -} diff --git a/x-pack/plugins/maps/public/classes/layers/new_vector_layer_wizard/utils/indexing_service.ts b/x-pack/plugins/maps/public/classes/layers/new_vector_layer_wizard/utils/indexing_service.ts index 2720524894a730..be5c0b57a509d0 100644 --- a/x-pack/plugins/maps/public/classes/layers/new_vector_layer_wizard/utils/indexing_service.ts +++ b/x-pack/plugins/maps/public/classes/layers/new_vector_layer_wizard/utils/indexing_service.ts @@ -5,23 +5,22 @@ * 2.0. */ -import { http as httpService } from './http_service'; -import { getSavedObjectsClient } from '../../../../kibana_services'; +import { getSavedObjectsClient, getHttp } from '../../../../kibana_services'; import { INDEX_FEATURE_PATH, INDEX_SOURCE_API_PATH } from '../../../../../common'; export const getExistingIndexNames = async () => { - const indexes = await httpService({ - url: `/api/index_management/indices`, + const indexes = await getHttp().fetch({ + path: `/api/index_management/indices`, method: 'GET', }); return indexes ? indexes.map(({ name }: { name: string }) => name) : []; }; export const createNewIndexAndPattern = async (indexName: string) => { - return await httpService({ - url: `/${INDEX_SOURCE_API_PATH}`, + return await getHttp().fetch({ + path: `/${INDEX_SOURCE_API_PATH}`, method: 'POST', - data: { + body: convertObjectToBlob({ index: indexName, // Initially set to static mappings mappings: { @@ -31,23 +30,27 @@ export const createNewIndexAndPattern = async (indexName: string) => { }, }, }, - }, + }), }); }; export const addFeatureToIndex = async (indexName: string, geometry: unknown) => { - return await httpService({ - url: `/${INDEX_FEATURE_PATH}`, + return await getHttp().fetch({ + path: `/${INDEX_FEATURE_PATH}`, method: 'POST', - data: { + body: convertObjectToBlob({ index: indexName, data: { coordinates: geometry, }, - }, + }), }); }; +const convertObjectToBlob = (obj: unknown) => { + return new Blob([JSON.stringify(obj)], { type: 'application/json' }); +}; + export const getExistingIndexPatternNames = async () => { const indexPatterns = await getSavedObjectsClient() .find({