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> {
|
async getEndpoints(providerUri: string, fetchMethod: any): Promise<any> {
|
||||||
try {
|
try {
|
||||||
const endpoints = await await fetchMethod.get(providerUri).json()
|
const endpoints = await await fetchMethod(providerUri).json()
|
||||||
return endpoints
|
return endpoints
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error('Finding the service endpoints failed:', e)
|
LoggerInstance.error('Finding the service endpoints failed:', e)
|
||||||
@ -80,7 +80,7 @@ export class Provider {
|
|||||||
: null
|
: null
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
try {
|
try {
|
||||||
const response = await fetchMethod.get(path + `?userAddress=${consumerAddress}`)
|
const response = await fetchMethod(path + `?userAddress=${consumerAddress}`)
|
||||||
return String((await response.json()).nonce)
|
return String((await response.json()).nonce)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error(e)
|
LoggerInstance.error(e)
|
||||||
@ -108,13 +108,6 @@ export class Provider {
|
|||||||
providerUri,
|
providerUri,
|
||||||
providerEndpoints
|
providerEndpoints
|
||||||
)
|
)
|
||||||
// await this.getNonce(
|
|
||||||
// providerUri,
|
|
||||||
// accountId,
|
|
||||||
// fetchMethod,
|
|
||||||
// providerEndpoints,
|
|
||||||
// serviceEndpoints
|
|
||||||
// )
|
|
||||||
const args = {
|
const args = {
|
||||||
documentId: did,
|
documentId: did,
|
||||||
document: JSON.stringify(document),
|
document: JSON.stringify(document),
|
||||||
@ -125,7 +118,7 @@ export class Provider {
|
|||||||
: null
|
: null
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
try {
|
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
|
return (await response.json()).encryptedDocument
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error(e)
|
LoggerInstance.error(e)
|
||||||
@ -156,7 +149,7 @@ export class Provider {
|
|||||||
: null
|
: null
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
try {
|
try {
|
||||||
const response = await fetchMethod.post(path, JSON.stringify(args))
|
const response = await fetchMethod(path, JSON.stringify(args))
|
||||||
const results: FileMetadata[] = await response.json()
|
const results: FileMetadata[] = await response.json()
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
files.push(result)
|
files.push(result)
|
||||||
@ -204,7 +197,7 @@ export class Provider {
|
|||||||
if (userCustomParameters)
|
if (userCustomParameters)
|
||||||
initializeUrl += '&userdata=' + encodeURI(JSON.stringify(userCustomParameters))
|
initializeUrl += '&userdata=' + encodeURI(JSON.stringify(userCustomParameters))
|
||||||
try {
|
try {
|
||||||
const response = await fetchMethod.get(initializeUrl)
|
const response = await fetchMethod(initializeUrl)
|
||||||
return await response.text()
|
return await response.text()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error(e)
|
LoggerInstance.error(e)
|
||||||
@ -219,7 +212,7 @@ export class Provider {
|
|||||||
*/
|
*/
|
||||||
public async isValidProvider(url: string, fetchMethod: any): Promise<boolean> {
|
public async isValidProvider(url: string, fetchMethod: any): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const response = await fetchMethod.get(url)
|
const response = await fetchMethod(url)
|
||||||
if (response?.ok) {
|
if (response?.ok) {
|
||||||
const params = await response.json()
|
const params = await response.json()
|
||||||
if (params && params.providerAddress) return true
|
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 './Logger'
|
||||||
export * from './DatatokenName'
|
export * from './DatatokenName'
|
||||||
export * from './ContractParams'
|
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 { Config } from '../../src/models/Config'
|
||||||
import { LoggerInstance, LogLevel } from '../../src/utils'
|
|
||||||
|
|
||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
|
|
||||||
LoggerInstance.setLevel(LogLevel.Error)
|
|
||||||
const web3 = new Web3('http://127.0.0.1:8545')
|
const web3 = new Web3('http://127.0.0.1:8545')
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
metadataCacheUri: 'http://aquarius:5000',
|
metadataCacheUri: 'http://aquarius:5000',
|
||||||
providerUri: 'http://localhost:8030',
|
providerUri: 'http://localhost:8030',
|
||||||
nodeUri: `http://localhost:${process.env.ETH_PORT || 8545}`,
|
nodeUri: `http://localhost:${process.env.ETH_PORT || 8545}`,
|
||||||
verbose: LogLevel.Error,
|
|
||||||
web3Provider: web3
|
web3Provider: web3
|
||||||
} as Config
|
} 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 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 MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json'
|
||||||
|
|
||||||
import { TestContractHandler } from '../TestContractHandler'
|
import { TestContractHandler } from '../../TestContractHandler'
|
||||||
import { NFTFactory, NFTCreateData } from '../../src/factories/NFTFactory'
|
import { NFTFactory, NFTCreateData } from '../../../src/factories/NFTFactory'
|
||||||
import {
|
import {
|
||||||
Datatoken,
|
Datatoken,
|
||||||
NFTDatatoken,
|
NFTDatatoken,
|
||||||
OrderParams,
|
OrderParams,
|
||||||
DispenserParams
|
DispenserParams
|
||||||
} from '../../src/datatokens'
|
} from '../../../src/datatokens'
|
||||||
import { AbiItem } from 'web3-utils'
|
import { AbiItem } from 'web3-utils'
|
||||||
import { LoggerInstance } from '../../src/utils'
|
import { LoggerInstance } from '../../../src/utils'
|
||||||
import { FreCreationParams, FreOrderParams } from '../../src/interfaces'
|
import { FreCreationParams, FreOrderParams } from '../../../src/interfaces'
|
||||||
|
|
||||||
const web3 = new Web3('http://127.0.0.1:8545')
|
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 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 MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json'
|
||||||
|
|
||||||
import { TestContractHandler } from '../TestContractHandler'
|
import { TestContractHandler } from '../../TestContractHandler'
|
||||||
import { NFTFactory, NFTCreateData } from '../../src/factories/NFTFactory'
|
import { NFTFactory, NFTCreateData } from '../../../src/factories/NFTFactory'
|
||||||
import { NFTDatatoken } from '../../src/datatokens/NFTDatatoken'
|
import { NFTDatatoken } from '../../../src/datatokens/NFTDatatoken'
|
||||||
import { AbiItem } from 'web3-utils'
|
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')
|
const web3 = new Web3('http://127.0.0.1:8545')
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user