2017-09-20 14:25:27 -07:00
|
|
|
import Ember from 'ember';
|
|
|
|
import { IDataset, IDatasetGetResponse } from 'wherehows-web/typings/api/datasets/dataset';
|
|
|
|
import { getHeaders, getJSON } from 'wherehows-web/utils/api/fetcher';
|
|
|
|
import { datasetsUrlRoot, datasetUrlById } from 'wherehows-web/utils/api/datasets/shared';
|
|
|
|
import { ApiStatus } from 'wherehows-web/utils/api';
|
|
|
|
|
|
|
|
const { Logger: { warn } } = Ember;
|
|
|
|
// TODO: DSS-6122 Create and move to Error module
|
|
|
|
const datasetApiException = 'An error occurred with the dataset api';
|
2017-09-20 16:10:52 -07:00
|
|
|
const datasetIdException = 'Could not find a valid id for the dataset requested';
|
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>}
|
|
|
|
*/
|
2017-09-20 16:10:52 -07:00
|
|
|
const readDataset = async (id: number | string): Promise<IDataset> => {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const { status, dataset } = await getJSON<IDatasetGetResponse>({ url: datasetUrlById(id) });
|
|
|
|
|
|
|
|
if (status === ApiStatus.OK && dataset) {
|
|
|
|
return dataset;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(datasetApiException);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a url to get a dataset id given a dataset urn
|
|
|
|
* @param {string} urn
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
const datasetIdTranslationUrlByUrn = (urn: string): string => {
|
|
|
|
return `${datasetsUrlRoot}/urntoid/${encodeURIComponent(urn)}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
export { readDataset, datasetUrnToId };
|