mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import { DDO, DID, Logger } from '@oceanprotocol/lib'
|
|
import {
|
|
QueryResult,
|
|
SearchQuery
|
|
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
|
|
import axios, { CancelToken, AxiosResponse } from 'axios'
|
|
|
|
// TODO: import directly from ocean.js somehow.
|
|
// Transforming Aquarius' direct response is needed for getting actual DDOs
|
|
// and not just strings of DDOs. For now, taken from
|
|
// https://github.com/oceanprotocol/ocean.js/blob/main/src/metadatacache/MetadataCache.ts#L361-L375
|
|
export function transformQueryResult(
|
|
{
|
|
results,
|
|
page,
|
|
total_pages: totalPages,
|
|
total_results: totalResults
|
|
}: any = {
|
|
result: [],
|
|
page: 0,
|
|
total_pages: 0,
|
|
total_results: 0
|
|
}
|
|
): QueryResult {
|
|
return {
|
|
results: (results || []).map((ddo: DDO) => new DDO(ddo as DDO)),
|
|
page,
|
|
totalPages,
|
|
totalResults
|
|
}
|
|
}
|
|
|
|
export async function queryMetadata(
|
|
query: SearchQuery,
|
|
metadataCacheUri: string,
|
|
cancelToken: CancelToken
|
|
): Promise<QueryResult> {
|
|
try {
|
|
const response: AxiosResponse<QueryResult> = await axios.post(
|
|
`${metadataCacheUri}/api/v1/aquarius/assets/ddo/query`,
|
|
{ ...query, cancelToken }
|
|
)
|
|
if (!response || response.status !== 200 || !response.data) return
|
|
|
|
return transformQueryResult(response.data)
|
|
} catch (error) {
|
|
if (axios.isCancel(error)) {
|
|
Logger.log(error.message)
|
|
} else {
|
|
Logger.error(error.message)
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function retrieveDDO(
|
|
did: string | DID,
|
|
metadataCacheUri: string,
|
|
cancelToken: CancelToken
|
|
): Promise<DDO> {
|
|
try {
|
|
const response: AxiosResponse<DDO> = await axios.get(
|
|
`${metadataCacheUri}/api/v1/aquarius/assets/ddo/${did}`,
|
|
{ cancelToken }
|
|
)
|
|
if (!response || response.status !== 200 || !response.data) return
|
|
|
|
return new DDO(response.data)
|
|
} catch (error) {
|
|
if (axios.isCancel(error)) {
|
|
Logger.log(error.message)
|
|
} else {
|
|
Logger.error(error.message)
|
|
}
|
|
}
|
|
}
|