import { ConseilServerInfo } from '../types/conseil/QueryTypes' ; import { ConseilRequestError } from '../types/conseil/ConseilErrorTypes' ; import { PlatformDefinition , NetworkDefinition , EntityDefinition , AttributeDefinition } from '../types/conseil/MetadataTypes' ; import FetchSelector from '../utils/FetchSelector' ; import LogSelector from '../utils/LoggerSelector' ; const log = LogSelector . log ; const fetch = FetchSelector . fetch ; /**
 * Utility functions for querying backend Conseil v2 API for metadata
 */ export namespace ConseilMetadataClient { export async function executeMetadataQuery ( serverInfo , route ) { return fetch ( `${ serverInfo . url }/v2/metadata/${ route }` , { method : 'GET' , headers : { 'apiKey' : serverInfo . apiKey } } ) . then ( r => { if ( ! r . ok ) { throw new ConseilRequestError ( r . status , r . statusText , `${ serverInfo . url }/v2/metadata/${ route }` , null ) ; } return r ; } ) . then ( r => r . json ( ) . catch ( error => { log . error ( `ConseilMetadataClient.executeMetadataQuery parsing failed for ${ serverInfo . url }/v2/metadata/${ route } with ${ error }` ) ; } ) ) ; } param serverInfo export async function getPlatforms ( serverInfo ) { return executeMetadataQuery ( serverInfo , 'platforms' ) ; } param serverInfo param platform see getPlatforms export async function getNetworks ( serverInfo , platform ) { return executeMetadataQuery ( serverInfo , `${ platform }/networks` ) ; } param serverInfo param platform param network see getNetworks export async function getEntities ( serverInfo , platform , network ) { return executeMetadataQuery ( serverInfo , `${ platform }/${ network }/entities` ) ; } param serverInfo param platform param network param entity see getEntities export async function getAttributes ( serverInfo , platform , network , entity ) { return executeMetadataQuery ( serverInfo , `${ platform }/${ network }/${ entity }/attributes` ) ; } param serverInfo param platform param network param entity param attribute see getAttributes export async function getAttributeValues ( serverInfo , platform , network , entity , attribute ) { return executeMetadataQuery ( serverInfo , `${ platform }/${ network }/${ entity }/${ attribute }` ) ; } param serverInfo param platform param network param entity param attribute param prefix see getAttributes export async function getAttributeValuesForPrefix ( serverInfo , platform , network , entity , attribute , prefix ) { return executeMetadataQuery ( serverInfo , `${ platform }/${ network }/${ entity }/${ attribute }/${ encodeURIComponent ( prefix ) }` ) ; } }