adds validators for new urn format. requests v2 dataset if urn in li format.

This commit is contained in:
Seyi Adebajo 2018-02-14 20:47:26 -08:00
parent 1456cbe7a8
commit 1dbcaf1476
5 changed files with 65 additions and 14 deletions

View File

@ -1,3 +1,4 @@
// TODO: obsolete
import { debug } from '@ember/debug';
import { createAction } from 'redux-actions';

View File

@ -15,8 +15,13 @@ import {
import { readDatasetOwners, getUserEntities } from 'wherehows-web/utils/api/datasets/owners';
import { isRequiredMinOwnersNotConfirmed } from 'wherehows-web/constants/datasets/owner';
import { readDataset, datasetUrnToId, readDatasetView } from 'wherehows-web/utils/api/datasets/dataset';
import isDatasetUrn from 'wherehows-web/utils/validators/urn';
import {
readDatasetById,
datasetUrnToId,
readDatasetView,
readDatasetByUrn
} from 'wherehows-web/utils/api/datasets/dataset';
import { isWhUrn, isLiUrn } from 'wherehows-web/utils/validators/urn';
import { checkAclAccess } from 'wherehows-web/utils/api/datasets/acl-access';
import { currentUser } from 'wherehows-web/utils/api/authentication';
@ -53,14 +58,18 @@ export default Route.extend({
* @param {string} [urn] optional urn identifier for dataset
* @return {Promise<IDataset>}
*/
async model({ dataset_id, urn }) {
let datasetId = dataset_id;
async model({ dataset_id: datasetId, urn }) {
if (datasetId === 'urn') {
if (isWhUrn(urn)) {
return readDatasetById(await datasetUrnToId(urn));
}
if (datasetId === 'urn' && isDatasetUrn(urn)) {
datasetId = await datasetUrnToId(urn);
if (isLiUrn(urn)) {
return await readDatasetByUrn(urn);
}
}
return await readDataset(datasetId);
return await readDatasetById(datasetId);
},
/**

View File

@ -12,7 +12,8 @@ import {
datasetsCountUrl,
datasetsUrl,
datasetsUrlRoot,
datasetUrlById
datasetUrlById,
datasetUrlByUrn
} from 'wherehows-web/utils/api/datasets/shared';
import { ApiStatus } from 'wherehows-web/utils/api';
@ -31,7 +32,7 @@ const datasetViewUrlById = (id: number) => `${datasetUrlById(id)}/view`;
* @param {number} id the id of the dataset
* @return {Promise<IDataset>}
*/
const readDataset = async (id: number | string): Promise<IDataset> => {
const readDatasetById = async (id: number | string): Promise<IDataset> => {
id = parseInt(id + '', 10);
// if id is less than or equal 0, throw illegal dataset error
if (id <= 0 || !Number.isInteger(id)) {
@ -48,6 +49,16 @@ const readDataset = async (id: number | string): Promise<IDataset> => {
throw new Error(errorMessage);
};
/**
* 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!;
};
/**
* Reads the response from the datasetView endpoint for the provided dataset id
* @param {number} id
@ -128,4 +139,4 @@ const readDatasetsCount = async ({ platform, prefix }: Partial<IReadDatasetsOpti
return await getJSON<number>({ url });
};
export { readDataset, datasetUrnToId, readDatasetView, readDatasets, readDatasetsCount };
export { readDatasetById, datasetUrnToId, readDatasetView, readDatasets, readDatasetsCount, readDatasetByUrn };

View File

@ -14,6 +14,14 @@ export const datasetsUrlRoot = (version: ApiVersion) => `${getApiRoot(version)}/
*/
export const datasetUrlById = (id: number): string => `${datasetsUrlRoot('v1')}/${id}`;
/**
* Composes a url to get a specific dataset by urn
* @param {string} urn
* @returns {string}
*/
export const datasetUrlByUrn = (urn: string): string => `${getApiRoot('v2')}/dataset/${urn}`;
//FIXME api plurality ^^^^^^^^^^
// export const datasetUrlByUrn = (urn: string): string => `${datasetsUrlRoot('v2')}/${urn}`;
/**
* Composes the datasets count url from a given platform and or prefix if provided
* @param {Partial<IReadDatasetsOptionBag>} [{ platform, prefix }={}]

View File

@ -4,7 +4,14 @@
* The value following the urn key is retained
* @type {RegExp}
*/
const urnRegex = /([a-z_]+):\/{3}([a-z0-9_\-/{}]*)/i;
const datasetUrnRegexWH = /([a-z_]+):\/{3}([a-z0-9_\-/{}]*)/i;
/**
* Matches a urn string that follows the pattern captures, the comma delimited platform, segment and fabric
* e.g urn:li:dataset:(urn:li:dataPlatform:PLATFORM,SEGMENT,FABRIC)
* @type {RegExp}
*/
const datasetUrnRegexLI = /urn:li:dataset:\(urn:li:dataPlatform:(\w+),([\w.\-]+),(\w+)\)/;
/**
* Matches urn's that occur in flow urls
@ -12,11 +19,25 @@ const urnRegex = /([a-z_]+):\/{3}([a-z0-9_\-/{}]*)/i;
*/
const specialFlowUrnRegex = /(?:\?urn=)([a-z0-9_\-/{}\s]+)/i;
/**
* Checks if a string matches the datasetUrnRegexWH
* @param {string} candidateUrn
* @returns {boolean}
*/
const isWhUrn = (candidateUrn: string): boolean => datasetUrnRegexWH.test(String(candidateUrn));
/**
* Checks if a string matches the datasetUrnRegexLI
* @param {string} candidateUrn
* @returns {boolean}
*/
const isLiUrn = (candidateUrn: string): boolean => datasetUrnRegexLI.test(String(candidateUrn));
/**
* Asserts that a provided string matches the urn pattern above
* @param {string} candidateUrn the string to test on
*/
const isUrn = (candidateUrn: string) => urnRegex.test(String(candidateUrn));
const isUrn = (candidateUrn: string) => isLiUrn(candidateUrn) || isWhUrn(candidateUrn);
/**
* Extracts the platform string from the candidate urn string
@ -24,7 +45,8 @@ const isUrn = (candidateUrn: string) => urnRegex.test(String(candidateUrn));
* @returns {string | void}
*/
const getPlatformFromUrn = (candidateUrn: string) => {
const matches = urnRegex.exec(candidateUrn);
const matches = datasetUrnRegexWH.exec(candidateUrn);
if (matches) {
const [, platform] = matches;
return platform.toUpperCase();
@ -33,4 +55,4 @@ const getPlatformFromUrn = (candidateUrn: string) => {
export default isUrn;
export { urnRegex, specialFlowUrnRegex, getPlatformFromUrn };
export { datasetUrnRegexWH, datasetUrnRegexLI, isWhUrn, isLiUrn, specialFlowUrnRegex, getPlatformFromUrn };