Skip to content

Commit

Permalink
Merge pull request #1 from XYOracleNetwork/feature/api-call-index-tests
Browse files Browse the repository at this point in the history
API Call Index Tests
  • Loading branch information
JoelBCarter authored Dec 3, 2023
2 parents a82864b + 9b2d379 commit a0a405a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"Merkle",
"microether",
"milliether",
"offchain",
"pako",
"payloadset",
"Promisable",
Expand Down
11 changes: 8 additions & 3 deletions packages/payloadset/packages/api/src/Payload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hash } from '@xylabs/hex'
import { AsObjectFactory } from '@xyo-network/object'
import { AsObjectFactory, JsonArray, JsonObject } from '@xyo-network/object'
import { isPayloadOfSchemaType, Payload } from '@xyo-network/payload-model'

export const ApiCallSchema = 'network.xyo.api.call'
Expand Down Expand Up @@ -38,7 +38,9 @@ export interface HttpMeta {
status?: number
}

export type ApiCallJsonResult<T extends object | [] = object> = Payload<
export type ApiCallJsonResultType = JsonArray | JsonObject

export type ApiCallJsonResult<T extends ApiCallJsonResultType = ApiCallJsonResultType> = Payload<
{
call: Hash
contentType: 'application/json'
Expand All @@ -64,7 +66,10 @@ export type ApiCallErrorResult = Payload<
ApiCallResultSchema
>

export type ApiCallResult = ApiCallBase64Result | ApiCallJsonResult | ApiCallErrorResult
export type ApiCallResult<TJson extends JsonArray | JsonObject = JsonArray | JsonObject> =
| ApiCallBase64Result
| ApiCallJsonResult<TJson>
| ApiCallErrorResult

export const isApiCall = isPayloadOfSchemaType(ApiCallSchema)
export const asApiCall = AsObjectFactory.create(isApiCall)
Expand Down
4 changes: 2 additions & 2 deletions packages/payloadset/packages/api/src/Witness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Axios, AxiosError, AxiosJson } from '@xylabs/axios'
import { Hash } from '@xylabs/hex'
import { AbstractWitness } from '@xyo-network/abstract-witness'
import { PayloadHasher } from '@xyo-network/hash'
import { JsonArray, JsonObject } from '@xyo-network/object'
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
import { WitnessParams } from '@xyo-network/witness-model'
import { fromByteArray } from 'base64-js'
Expand All @@ -16,6 +15,7 @@ import {
ApiCallBase64Result,
ApiCallErrorResult,
ApiCallJsonResult,
ApiCallJsonResultType,
ApiCallResult,
ApiCallResultSchema,
ApiCallSchema,
Expand Down Expand Up @@ -116,7 +116,7 @@ export class ApiCallWitness<TParams extends ApiCallWitnessParams = ApiCallWitnes
switch (this.accept) {
case 'application/json': {
const axios = new AxiosJson({ headers: this.params.headers, timeout: this.timeout })
const response = await axios.get<JsonArray | JsonObject>(url)
const response = await axios.get<ApiCallJsonResultType>(url)
if (response.status >= 200 && response.status < 300) {
const jsonResult = result as ApiCallJsonResult
jsonResult.data = response.data
Expand Down
65 changes: 59 additions & 6 deletions packages/payloadset/packages/api/src/spec/opensea.nft-call.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable max-statements */

import { describeIf } from '@xylabs/jest-helpers'
import { HDWallet } from '@xyo-network/account'
import { ManifestWrapper, PackageManifestPayload } from '@xyo-network/manifest'
Expand All @@ -8,7 +7,7 @@ import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
import { asSentinelInstance } from '@xyo-network/sentinel-model'
import { asWitnessInstance } from '@xyo-network/witness-model'

import { ApiCallJsonResult, ApiCallResult, ApiCallResultSchema, ApiCallSchema, ApiUriTemplateCall } from '../Payload'
import { ApiCallJsonResult, ApiCallResultSchema, ApiCallSchema, ApiUriTemplateCall } from '../Payload'
import { ApiCallWitness } from '../Witness'
import openseaNftsManifest from './opensea.nft-call.json'

Expand All @@ -18,6 +17,60 @@ describe('OpenSeaApi', () => {
const apiKey = process.env.OPENSEA_API_KEY

describeIf(apiKey)('report', () => {
type OpenSeaNft = {
/*
* Collection slug. A unique string to identify a collection on OpenSea
*/
collection: string
/*
* The unique public blockchain identifier for the contract
*/
contract: string
/**
* @deprecated
*/
created_at: string
/*
* Description of the NFT
*/
description: string
/*
* The NFT's unique identifier within the smart contract (also referred to as token_id)
*/
identifier: string
/*
* Link to the image associated with the NFT
*/
image_url: string
/*
* If the item is currently able to be bought or sold using OpenSea
*/
is_disabled: boolean
/*
* If the item is currently classified as 'Not Safe for Work' by OpenSea as defined in OpenSea's NSFW Policy.
*/
is_nsfw: boolean
/*
* Link to the offchain metadata store
*/
metadata_url: string
/*
* Name of the NFT
*/
name: string
/*
* ERC standard of the token (erc721, erc1155)
*/
token_standard: string
/*
* Last time that the NFT's metadata was updated by OpenSea
*/
updated_at: string
}
type OpenSeaListNftsByAccountResponse = {
next: string
nfts: OpenSeaNft[]
}
it('specifying address', async () => {
const mnemonic = 'later puppy sound rebuild rebuild noise ozone amazing hope broccoli crystal grief'
const wallet = await HDWallet.fromPhrase(mnemonic)
Expand Down Expand Up @@ -51,11 +104,11 @@ describe('OpenSeaApi', () => {

const report = await sentinel?.report([call])

const apiCallResult = report?.find(isPayloadOfSchemaType(ApiCallResultSchema)) as ApiCallResult | undefined

const apiCallResult = report?.find(isPayloadOfSchemaType<ApiCallJsonResult<OpenSeaListNftsByAccountResponse>>(ApiCallResultSchema))
expect(apiCallResult).toBeDefined()
expect(apiCallResult?.schema).toBeString()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((apiCallResult as ApiCallJsonResult<any>)?.data.nfts).toBeArrayOfSize(1)
expect(apiCallResult?.data.nfts).toBeArrayOfSize(1)
expect(apiCallResult?.data.nfts[0].collection).toBeString()
})
})
})

0 comments on commit a0a405a

Please sign in to comment.