| 
									
										
										
										
											2018-07-19 16:18:06 -07:00
										 |  |  | import { isNotFoundApiError, isServerExceptionApiError } from 'wherehows-web/utils/api'; | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  | import { datasetUrlByUrn } from 'wherehows-web/utils/api/datasets/shared'; | 
					
						
							|  |  |  | import { deleteJSON, getJSON, putJSON } from 'wherehows-web/utils/api/fetcher'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   IAccessControlEntry, | 
					
						
							|  |  |  |   IGetAclsResponse, | 
					
						
							|  |  |  |   IRequestAccessControlEntry | 
					
						
							|  |  |  | } from 'wherehows-web/typings/api/datasets/aclaccess'; | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  |  * Returns the dataset acls url by urn | 
					
						
							|  |  |  |  * @param {string} urn the urn for the dataset | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  | const datasetAclsUrlByUrn = (urn: string): string => `${datasetUrlByUrn(urn)}/acl`; | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  |  * Gets the url for the adding an entry to the acl | 
					
						
							|  |  |  |  * @param {string} urn the urn for the dataset | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  | const addDatasetAclUrnByUrn = (urn: string): string => `${datasetAclsUrlByUrn(urn)}/add`; | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  |  * Gets the url to remove the current user from the acl | 
					
						
							|  |  |  |  * @param {string} urn datasets urn | 
					
						
							|  |  |  |  * @return {string} | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  | const deleteDatasetAclUrnByUrn = (urn: string): string => `${datasetAclsUrlByUrn(urn)}/remove`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Requests access to the acl for the currently logged in user | 
					
						
							|  |  |  |  * @param {string} urn dataset urn | 
					
						
							|  |  |  |  * @param {IRequestAccessControlEntry} props | 
					
						
							|  |  |  |  * @return {Promise<void>} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const requestAclAccess = (urn: string, props: IRequestAccessControlEntry): Promise<void> => { | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     return putJSON<void>({ url: addDatasetAclUrnByUrn(urn), data: props }); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							| 
									
										
										
										
											2018-07-19 16:18:06 -07:00
										 |  |  |     if (isServerExceptionApiError(e)) { | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  |       // TODO: retry with exponential back-off
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     throw e; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  |  * Removes access to the acl for the currently logged in user | 
					
						
							|  |  |  |  * @param {string} urn | 
					
						
							|  |  |  |  * @return {Promise<void>} | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  | const removeAclAccess = (urn: string): Promise<void> => { | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     return deleteJSON<void>({ url: deleteDatasetAclUrnByUrn(urn) }); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							| 
									
										
										
										
											2018-07-19 16:18:06 -07:00
										 |  |  |     if (isServerExceptionApiError(e)) { | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  |       // TODO: retry with exponential back-off
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     throw e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Gets the list of current acl entries | 
					
						
							|  |  |  |  * @param {string} urn | 
					
						
							|  |  |  |  * @return {Promise<Array<IAccessControlEntry>>} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const readDatasetAcls = async (urn: string): Promise<Array<IAccessControlEntry>> => { | 
					
						
							|  |  |  |   let acls: Array<IAccessControlEntry> = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     return getJSON<IGetAclsResponse>({ url: datasetAclsUrlByUrn(urn) }); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							| 
									
										
										
										
											2018-07-19 16:18:06 -07:00
										 |  |  |     if (isNotFoundApiError(e)) { | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  |       return acls; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     throw e; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-20 11:32:51 -07:00
										 |  |  | export { requestAclAccess, removeAclAccess, readDatasetAcls }; |