2017-10-24 00:11:23 -07:00
|
|
|
import { warn } from '@ember/debug';
|
2017-10-25 02:11:28 -07:00
|
|
|
import {
|
|
|
|
IDataset,
|
|
|
|
IDatasetGetResponse,
|
2018-02-14 16:00:32 -08:00
|
|
|
IDatasetsGetResponse,
|
2017-10-25 02:11:28 -07:00
|
|
|
IDatasetView,
|
2018-02-14 16:00:32 -08:00
|
|
|
IDatasetViewGetResponse,
|
|
|
|
IReadDatasetsOptionBag
|
2017-10-25 02:11:28 -07:00
|
|
|
} from 'wherehows-web/typings/api/datasets/dataset';
|
2017-09-20 14:25:27 -07:00
|
|
|
import { getHeaders, getJSON } from 'wherehows-web/utils/api/fetcher';
|
2018-02-14 16:00:32 -08:00
|
|
|
import {
|
|
|
|
datasetsCountUrl,
|
|
|
|
datasetsUrl,
|
|
|
|
datasetsUrlRoot,
|
2018-02-14 20:47:26 -08:00
|
|
|
datasetUrlById,
|
|
|
|
datasetUrlByUrn
|
2018-02-14 16:00:32 -08:00
|
|
|
} from 'wherehows-web/utils/api/datasets/shared';
|
2017-09-20 14:25:27 -07:00
|
|
|
import { ApiStatus } from 'wherehows-web/utils/api';
|
|
|
|
|
|
|
|
// TODO: DSS-6122 Create and move to Error module
|
|
|
|
const datasetApiException = 'An error occurred with the dataset api';
|
2017-09-20 18:29:06 -07:00
|
|
|
const datasetIdException = 'Dataset reference in unexpected format. Expected a urn or dataset id.';
|
2017-09-20 14:25:27 -07:00
|
|
|
|
2017-10-25 02:11:28 -07:00
|
|
|
/**
|
|
|
|
* Constructs the dataset view endpoint url from the dataset id
|
|
|
|
* @param {number} id the dataset id
|
|
|
|
*/
|
|
|
|
const datasetViewUrlById = (id: number) => `${datasetUrlById(id)}/view`;
|
|
|
|
|
2017-09-20 14:25:27 -07:00
|
|
|
/**
|
|
|
|
* Reads the dataset object from the get endpoint for the given dataset id
|
|
|
|
* @param {number} id the id of the dataset
|
|
|
|
* @return {Promise<IDataset>}
|
|
|
|
*/
|
2018-02-14 20:47:26 -08:00
|
|
|
const readDatasetById = async (id: number | string): Promise<IDataset> => {
|
2017-09-20 16:10:52 -07:00
|
|
|
id = parseInt(id + '', 10);
|
2017-09-20 14:25:27 -07:00
|
|
|
// if id is less than or equal 0, throw illegal dataset error
|
2017-09-20 16:10:52 -07:00
|
|
|
if (id <= 0 || !Number.isInteger(id)) {
|
|
|
|
throw new TypeError(datasetIdException);
|
2017-09-20 14:25:27 -07:00
|
|
|
}
|
|
|
|
|
2017-11-06 09:48:37 -08:00
|
|
|
const { status, dataset, message } = await getJSON<IDatasetGetResponse>({ url: datasetUrlById(id) });
|
|
|
|
let errorMessage = message || datasetApiException;
|
2017-09-20 14:25:27 -07:00
|
|
|
|
|
|
|
if (status === ApiStatus.OK && dataset) {
|
|
|
|
return dataset;
|
|
|
|
}
|
|
|
|
|
2017-11-06 09:48:37 -08:00
|
|
|
throw new Error(errorMessage);
|
2017-09-20 14:25:27 -07:00
|
|
|
};
|
|
|
|
|
2018-02-14 20:47:26 -08:00
|
|
|
/**
|
|
|
|
* Reads a dataset by urn, in the li format
|
|
|
|
* @param {string} urn
|
|
|
|
* @returns {Promise<IDatasetView>}
|
|
|
|
*/
|
|
|
|
const readDatasetByUrn = async (urn: string): Promise<IDatasetView> => {
|
|
|
|
const { dataset } = await getJSON<Pick<IDatasetViewGetResponse, 'dataset'>>({ url: datasetUrlByUrn(urn) });
|
|
|
|
return dataset!;
|
|
|
|
};
|
|
|
|
|
2017-10-25 02:11:28 -07:00
|
|
|
/**
|
|
|
|
* Reads the response from the datasetView endpoint for the provided dataset id
|
2018-02-14 16:00:32 -08:00
|
|
|
* @param {number} id
|
|
|
|
* @returns {Promise<IDatasetView>}
|
2017-10-25 02:11:28 -07:00
|
|
|
*/
|
|
|
|
const readDatasetView = async (id: number): Promise<IDatasetView> => {
|
|
|
|
const { status, dataset } = await getJSON<IDatasetViewGetResponse>({ url: datasetViewUrlById(id) });
|
|
|
|
|
|
|
|
if (status === ApiStatus.OK && dataset) {
|
|
|
|
return dataset;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(datasetApiException);
|
|
|
|
};
|
|
|
|
|
2017-09-20 14:25:27 -07:00
|
|
|
/**
|
|
|
|
* Constructs a url to get a dataset id given a dataset urn
|
|
|
|
* @param {string} urn
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
const datasetIdTranslationUrlByUrn = (urn: string): string => {
|
2018-02-14 16:00:32 -08:00
|
|
|
return `${datasetsUrlRoot('v1')}/urntoid/${encodeURIComponent(urn)}`;
|
2017-09-20 14:25:27 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates a dataset urn string to a dataset id, using the endpoint at datasetIdTranslationUrlByUrn()
|
|
|
|
* if a dataset id is not found
|
|
|
|
* or an exception occurs, the value returned is zero, which is an illegal dataset id
|
|
|
|
* and should be treated as an exception.
|
|
|
|
* @param {string} urn
|
|
|
|
* @return {Promise<number>}
|
|
|
|
*/
|
|
|
|
const datasetUrnToId = async (urn: string): Promise<number> => {
|
|
|
|
let datasetId = 0;
|
|
|
|
|
|
|
|
try {
|
2017-09-20 16:10:52 -07:00
|
|
|
// The headers object is a Header
|
2017-09-20 14:25:27 -07:00
|
|
|
const headers = await getHeaders({ url: datasetIdTranslationUrlByUrn(urn) });
|
2017-09-20 16:10:52 -07:00
|
|
|
const stringId = headers.get('datasetid');
|
2017-09-20 14:25:27 -07:00
|
|
|
|
|
|
|
// If stringId is not falsey, parse as int and return, otherwise use default
|
|
|
|
if (stringId) {
|
|
|
|
datasetId = parseInt(stringId, 10);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
warn(`Exception occurred translating datasetUrn: ${e.message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return datasetId;
|
|
|
|
};
|
|
|
|
|
2018-02-14 16:00:32 -08:00
|
|
|
/**
|
|
|
|
* Fetches the datasets for a platform, and prefix and returns the list of datasets in the
|
|
|
|
* response
|
|
|
|
* @param {IReadDatasetsOptionBag} {
|
|
|
|
* platform,
|
|
|
|
* prefix
|
|
|
|
* }
|
|
|
|
* @returns {Promise<IDatasetsGetResponse['elements']>}
|
|
|
|
*/
|
|
|
|
const readDatasets = async ({
|
|
|
|
platform,
|
|
|
|
prefix
|
|
|
|
}: IReadDatasetsOptionBag): Promise<IDatasetsGetResponse['elements']> => {
|
|
|
|
const url = datasetsUrl({ platform, prefix });
|
|
|
|
const response = await getJSON<IDatasetsGetResponse>({ url });
|
|
|
|
|
|
|
|
return response ? [...response.elements] : [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of datasets, if provided, using the platform and prefix also
|
|
|
|
* @param {Partial<IReadDatasetsOptionBag>} { platform, prefix }
|
|
|
|
* @returns {Promise<number>}
|
|
|
|
*/
|
|
|
|
const readDatasetsCount = async ({ platform, prefix }: Partial<IReadDatasetsOptionBag>): Promise<number> => {
|
|
|
|
const url = datasetsCountUrl({ platform, prefix });
|
|
|
|
return await getJSON<number>({ url });
|
|
|
|
};
|
|
|
|
|
2018-02-14 20:47:26 -08:00
|
|
|
export { readDatasetById, datasetUrnToId, readDatasetView, readDatasets, readDatasetsCount, readDatasetByUrn };
|