import { getAxiosReqParams , RequestConfiguration } from '../utils/cancelRequests' ; import { Quota , TPDICollections , TPDProvider , TPDISearchParams , TPDI_SERVICE_URL , Order , TPDSearchResult , OrderSearchParams , OrderSearchResult , } from './const' ; import { AirbusDataProvider } from './AirbusDataProvider' ; import { PlanetDataProvider } from './PlanetDataProvider' ; import { MaxarDataProvider } from './MaxarDataProvider' ; import { TPDProviderInterface } from './TPDProvider' ; import { ensureTimeout } from '../utils/ensureTimeout' ; import { getAuthToken } from '../auth' ; import axios , { AxiosRequestConfig } from 'axios' ; import { CACHE_CONFIG_NOCACHE } from '../utils/cacheHandlers' ; const dataProviders = [ new AirbusDataProvider ( ) , new PlanetDataProvider ( ) , new MaxarDataProvider ( ) ] ; function getThirdPartyDataProvider ( provider ) { const tpdp = dataProviders . find ( p => p . getProvider ( ) === provider ) ; if ( ! tpdp ) { throw new Error ( `Unknown data provider ${ provider }` ) ; } return tpdp ; } async function getQuotasInner ( TDPICollectionId ? , reqConfig ? ) { return await ensureTimeout ( async ( innerReqConfig ) => { const requestConfig = createRequestConfig ( innerReqConfig ) ; if ( ! ! TDPICollectionId ) { requestConfig . params = { collectionId : TDPICollectionId } ; } const res = await axios . get ( `${ TPDI_SERVICE_URL }/quotas` , requestConfig ) ; return res . data . data as Quota [ ] ; } , reqConfig ) ; } function createRequestConfig ( innerReqConfig ) { const authToken = innerReqConfig && innerReqConfig . authToken ? innerReqConfig . authToken : getAuthToken ( ) ; if ( ! authToken ) { throw new Error ( 'Must be authenticated to perform request' ) ; } const headers = { Authorization : `Bearer ${ authToken }` , } ; const requestConfig = { responseType : 'json' , headers : headers , ... getAxiosReqParams ( innerReqConfig , CACHE_CONFIG_NOCACHE ) , } ; return requestConfig ; } export class TPDI { public static async getQuota ( TDPICollectionId , reqConfig ? ) { if ( ! TDPICollectionId ) { throw new Error ( 'TDPICollectionId must be provided' ) ; } const quotas = await getQuotasInner ( TDPICollectionId , reqConfig ) ; return quotas . length ? quotas [ 0 ] : null ; } public static async getQuotas ( reqConfig ? ) { return await getQuotasInner ( null , reqConfig ) ; } public static async search ( provider , params , reqConfig ? , count = 10 , viewtoken = null ) { return await ensureTimeout ( async ( innerReqConfig ) => { const requestConfig = createRequestConfig ( innerReqConfig ) ; const tpdp = getThirdPartyDataProvider ( provider ) ; tpdp . addSearchPagination ( requestConfig , count , viewtoken ) ; const payload = tpdp . getSearchPayload ( params ) ; const response = await axios . post < TPDSearchResult > ( `${ TPDI_SERVICE_URL }/search` , payload , requestConfig ) ; return response . data ; } , reqConfig ) ; } public static async getThumbnail ( collectionId , productId , reqConfig ? ) { if ( ! collectionId ) { throw new Error ( 'collectionId must be provided' ) ; } if ( ! productId ) { throw new Error ( 'productId must be provided' ) ; } return await ensureTimeout ( async ( innerReqConfig ) => { const requestConfig = createRequestConfig ( innerReqConfig ) ; requestConfig . responseType = 'blob' ; const response = await axios . get < Blob > ( `${ TPDI_SERVICE_URL }/collections/${ collectionId }/products/${ productId }/thumbnail` , requestConfig ) ; const thumbnail = response . data ; return thumbnail ; } , reqConfig ) ; } public static async createOrder ( provider , name , collectionId , items , params , reqConfig ? ) { return await ensureTimeout ( async ( innerReqConfig ) => { const requestConfig = createRequestConfig ( innerReqConfig ) ; const tpdp = getThirdPartyDataProvider ( provider ) ; const payload = tpdp . getOrderPayload ( name , collectionId , items , params ) ; const response = await axios . post < Order > ( `${ TPDI_SERVICE_URL }/orders` , payload , requestConfig ) ; const order = response . data ; return order ; } , reqConfig ) ; } public static async getOrders ( params ? , reqConfig ? , count ? , viewtoken ? ) { return await ensureTimeout ( async ( innerReqConfig ) => { const requestConfig = createRequestConfig ( innerReqConfig ) ; let queryParams = { } ; if ( params ) { queryParams = { ... params } ; } if ( ! isNaN ( count ) && count !== null ) { queryParams . count = count ; } if ( viewtoken ) { queryParams . viewtoken = viewtoken ; } requestConfig . params = queryParams ; const { data } = await axios . get < OrderSearchResult > ( `${ TPDI_SERVICE_URL }/orders` , requestConfig ) ; return data ; } , reqConfig ) ; } public static async getOrder ( orderId , reqConfig ? ) { return await ensureTimeout ( async ( innerReqConfig ) => { const requestConfig = createRequestConfig ( innerReqConfig ) ; const response = await axios . get < Order > ( `${ TPDI_SERVICE_URL }/orders/${ orderId }` , requestConfig ) ; const order = response . data ; return order ; } , reqConfig ) ; } public static async deleteOrder ( orderId , reqConfig ? ) { return await ensureTimeout ( async ( innerReqConfig ) => { const requestConfig = createRequestConfig ( innerReqConfig ) ; await axios . delete ( `${ TPDI_SERVICE_URL }/orders/${ orderId }` , requestConfig ) ; } , reqConfig ) ; } public static async confirmOrder ( orderId , reqConfig ? ) { return await ensureTimeout ( async ( innerReqConfig ) => { const requestConfig = createRequestConfig ( innerReqConfig ) ; const response = await axios . post < Order > ( `${ TPDI_SERVICE_URL }/orders/${ orderId }/confirm` , { } , requestConfig ) ; const order = response . data ; return order ; } , reqConfig ) ; } }