mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
updated fetch decouple strategy and added some tests
This commit is contained in:
parent
e08c15d4e7
commit
bb828b954c
@ -20,7 +20,7 @@ export class Provider {
|
||||
*/
|
||||
async getEndpoints(providerUri: string, fetchMethod: any): Promise<any> {
|
||||
try {
|
||||
const endpoints = await await fetchMethod.get(providerUri).json()
|
||||
const endpoints = await await fetchMethod(providerUri).json()
|
||||
return endpoints
|
||||
} catch (e) {
|
||||
LoggerInstance.error('Finding the service endpoints failed:', e)
|
||||
@ -80,7 +80,7 @@ export class Provider {
|
||||
: null
|
||||
if (!path) return null
|
||||
try {
|
||||
const response = await fetchMethod.get(path + `?userAddress=${consumerAddress}`)
|
||||
const response = await fetchMethod(path + `?userAddress=${consumerAddress}`)
|
||||
return String((await response.json()).nonce)
|
||||
} catch (e) {
|
||||
LoggerInstance.error(e)
|
||||
@ -108,13 +108,6 @@ export class Provider {
|
||||
providerUri,
|
||||
providerEndpoints
|
||||
)
|
||||
// await this.getNonce(
|
||||
// providerUri,
|
||||
// accountId,
|
||||
// fetchMethod,
|
||||
// providerEndpoints,
|
||||
// serviceEndpoints
|
||||
// )
|
||||
const args = {
|
||||
documentId: did,
|
||||
document: JSON.stringify(document),
|
||||
@ -125,7 +118,7 @@ export class Provider {
|
||||
: null
|
||||
if (!path) return null
|
||||
try {
|
||||
const response = await fetchMethod.post(path, decodeURI(JSON.stringify(args)))
|
||||
const response = await fetchMethod(path, decodeURI(JSON.stringify(args)))
|
||||
return (await response.json()).encryptedDocument
|
||||
} catch (e) {
|
||||
LoggerInstance.error(e)
|
||||
@ -156,7 +149,7 @@ export class Provider {
|
||||
: null
|
||||
if (!path) return null
|
||||
try {
|
||||
const response = await fetchMethod.post(path, JSON.stringify(args))
|
||||
const response = await fetchMethod(path, JSON.stringify(args))
|
||||
const results: FileMetadata[] = await response.json()
|
||||
for (const result of results) {
|
||||
files.push(result)
|
||||
@ -204,7 +197,7 @@ export class Provider {
|
||||
if (userCustomParameters)
|
||||
initializeUrl += '&userdata=' + encodeURI(JSON.stringify(userCustomParameters))
|
||||
try {
|
||||
const response = await fetchMethod.get(initializeUrl)
|
||||
const response = await fetchMethod(initializeUrl)
|
||||
return await response.text()
|
||||
} catch (e) {
|
||||
LoggerInstance.error(e)
|
||||
@ -219,7 +212,7 @@ export class Provider {
|
||||
*/
|
||||
public async isValidProvider(url: string, fetchMethod: any): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetchMethod.get(url)
|
||||
const response = await fetchMethod(url)
|
||||
if (response?.ok) {
|
||||
const params = await response.json()
|
||||
if (params && params.providerAddress) return true
|
||||
|
@ -0,0 +1 @@
|
||||
export * from './Provider'
|
12
src/utils/FetchHelper.ts
Normal file
12
src/utils/FetchHelper.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import fetch from 'cross-fetch'
|
||||
import LoggerInstance from './Logger'
|
||||
|
||||
export async function fetchData(url: string, opts: RequestInit): Promise<Response> {
|
||||
const result = await fetch(url, opts)
|
||||
if (!result.ok) {
|
||||
LoggerInstance.error(`Error requesting [${opts.method}] ${url}`)
|
||||
LoggerInstance.error(`Response message: \n${await result.text()}`)
|
||||
throw result
|
||||
}
|
||||
return result
|
||||
}
|
@ -3,3 +3,4 @@ export * from './GasUtils'
|
||||
export * from './Logger'
|
||||
export * from './DatatokenName'
|
||||
export * from './ContractParams'
|
||||
export * from './FetchHelper'
|
||||
|
6
test/integration/.mocharc.json
Normal file
6
test/integration/.mocharc.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"require": ["ts-node/register", "source-map-support/register", "mock-local-storage"],
|
||||
"full-trace": true,
|
||||
"exit": true,
|
||||
"timeout": "300000"
|
||||
}
|
25
test/integration/Provider.test.ts
Normal file
25
test/integration/Provider.test.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import config from './config'
|
||||
import { Provider } from '../../src/provider/Provider'
|
||||
import { assert } from 'chai'
|
||||
import { fetchData } from '../../src/utils'
|
||||
|
||||
describe('Provider tests', () => {
|
||||
let providerInstance: Provider
|
||||
|
||||
it('Initialize Ocean', async () => {
|
||||
providerInstance = new Provider()
|
||||
})
|
||||
|
||||
it('Alice tests invalid provider', async () => {
|
||||
const valid = await providerInstance.isValidProvider('http://example.net', fetchData)
|
||||
assert(valid === false)
|
||||
})
|
||||
|
||||
it('Alice tests valid provider', async () => {
|
||||
const valid = await providerInstance.isValidProvider(
|
||||
'http://127.0.0.1:8030',
|
||||
fetchData
|
||||
)
|
||||
assert(valid === true)
|
||||
})
|
||||
})
|
@ -1,15 +1,12 @@
|
||||
import { Config } from '../../src/models/Config'
|
||||
import { LoggerInstance, LogLevel } from '../../src/utils'
|
||||
|
||||
import Web3 from 'web3'
|
||||
|
||||
LoggerInstance.setLevel(LogLevel.Error)
|
||||
const web3 = new Web3('http://127.0.0.1:8545')
|
||||
|
||||
export default {
|
||||
metadataCacheUri: 'http://aquarius:5000',
|
||||
providerUri: 'http://localhost:8030',
|
||||
nodeUri: `http://localhost:${process.env.ETH_PORT || 8545}`,
|
||||
verbose: LogLevel.Error,
|
||||
web3Provider: web3
|
||||
} as Config
|
||||
|
@ -12,17 +12,17 @@ import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedR
|
||||
import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json'
|
||||
import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json'
|
||||
|
||||
import { TestContractHandler } from '../TestContractHandler'
|
||||
import { NFTFactory, NFTCreateData } from '../../src/factories/NFTFactory'
|
||||
import { TestContractHandler } from '../../TestContractHandler'
|
||||
import { NFTFactory, NFTCreateData } from '../../../src/factories/NFTFactory'
|
||||
import {
|
||||
Datatoken,
|
||||
NFTDatatoken,
|
||||
OrderParams,
|
||||
DispenserParams
|
||||
} from '../../src/datatokens'
|
||||
} from '../../../src/datatokens'
|
||||
import { AbiItem } from 'web3-utils'
|
||||
import { LoggerInstance } from '../../src/utils'
|
||||
import { FreCreationParams, FreOrderParams } from '../../src/interfaces'
|
||||
import { LoggerInstance } from '../../../src/utils'
|
||||
import { FreCreationParams, FreOrderParams } from '../../../src/interfaces'
|
||||
|
||||
const web3 = new Web3('http://127.0.0.1:8545')
|
||||
|
@ -12,11 +12,11 @@ import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedR
|
||||
import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json'
|
||||
import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json'
|
||||
|
||||
import { TestContractHandler } from '../TestContractHandler'
|
||||
import { NFTFactory, NFTCreateData } from '../../src/factories/NFTFactory'
|
||||
import { NFTDatatoken } from '../../src/datatokens/NFTDatatoken'
|
||||
import { TestContractHandler } from '../../TestContractHandler'
|
||||
import { NFTFactory, NFTCreateData } from '../../../src/factories/NFTFactory'
|
||||
import { NFTDatatoken } from '../../../src/datatokens/NFTDatatoken'
|
||||
import { AbiItem } from 'web3-utils'
|
||||
import { LoggerInstance } from '../../src/utils'
|
||||
import { LoggerInstance } from '../../../src/utils'
|
||||
|
||||
const web3 = new Web3('http://127.0.0.1:8545')
|
||||
|
Loading…
x
Reference in New Issue
Block a user