Skip to content

Commit

Permalink
Merge pull request #142 from Holo-Host/api-v2-zome-call-externio
Browse files Browse the repository at this point in the history
[B-06282] Make zome calls to /api/v2/
  • Loading branch information
peeech authored Jan 18, 2024
2 parents 2700050 + 7a2f766 commit 207ea2b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"email-validator": "^2.0.4",
"fast-json-stable-stringify": "^2.1.0",
"js-sha512": "^0.8.0",
"@msgpack/msgpack": "^2.8.0",
"moment": "^2.29.4",
"os-browserify": "^0.3.0",
"papaparse": "^5.4.1",
Expand Down
45 changes: 37 additions & 8 deletions src/interfaces/HposInterface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable camelcase */
import { decode } from "@msgpack/msgpack"
import axios from 'axios'
import { decodeAgentId } from '../../ui-common-library/src/utils/agent'
import { kAuthTokenLSKey, kCoreAppVersionLSKey } from '@/constants'
Expand Down Expand Up @@ -145,11 +146,14 @@ type HposHolochainCallResponse =
| ReserveSettingsResponse
| EUserKycLevel
| ServiceLogsResponse
| ZomeCallResponse

type HposAdminCallResponse = HposConfigResponse

type ServiceLogsResponse = Array<Record<string, unknown>>

type ZomeCallResponse = Uint8Array | null

export interface UsageResponse {
totalHostedHapps: number
totalHostedAgents: number
Expand Down Expand Up @@ -277,6 +281,7 @@ interface HposCallArgs {
method: string
path: string
headers?: Record<string, unknown>
responseType?: string
params?: Record<string, unknown>
}

Expand Down Expand Up @@ -337,6 +342,7 @@ export function useHposInterface(): HposInterface {
method = 'get',
path,
headers: userHeaders = {},
responseType = 'json',
params
}: HposCallArgs): Promise<HposHolochainCallResponse & HposAdminCallResponse> {
const fullUrl = `${HPOS_API_URL}${pathPrefix}${path}`
Expand All @@ -357,7 +363,8 @@ export function useHposInterface(): HposInterface {
return response.data

case 'post':
response = await axios.post(fullUrl, params, { headers })
response = await axios
.post(fullUrl, params, { responseType, headers })
return response.data

case 'put':
Expand Down Expand Up @@ -517,6 +524,8 @@ export function useHposInterface(): HposInterface {
await hposHolochainCall({
method: 'post',
path: '/zome_call',
pathPrefix: '/api/v2',
responseType: 'arraybuffer',
params
})

Expand Down Expand Up @@ -592,6 +601,8 @@ export function useHposInterface(): HposInterface {
await hposHolochainCall({
method: 'post',
path: '/zome_call',
pathPrefix: '/api/v2',
responseType: 'arraybuffer',
params
})

Expand Down Expand Up @@ -626,10 +637,12 @@ export function useHposInterface(): HposInterface {
const hostPreferences = await hposHolochainCall({
method: 'post',
path: '/zome_call',
pathPrefix: '/api/v2',
responseType: 'arraybuffer',
params
})

return hostPreferences
return decode(hostPreferences)
} catch (error) {
console.error('getHostPreferences encountered an error: ', error)
return false
Expand Down Expand Up @@ -732,15 +745,17 @@ export function useHposInterface(): HposInterface {
const response = await hposHolochainCall({
method: 'post',
path: '/zome_call',
pathPrefix: '/api/v2',
responseType: 'arraybuffer',
params
})

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { agent_address: agentAddress, nickname, avatar_url: avatarUrl } = response
const { agent_address: agentAddress, nickname, avatar_url: avatarUrl } = decode(response)

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
return { agentAddress: Uint8Array.from(agentAddress.data), nickname, avatarUrl }
return { agentAddress, nickname, avatarUrl }
}

function getHoloFuelProfile(): unknown {
Expand Down Expand Up @@ -786,6 +801,8 @@ export function useHposInterface(): HposInterface {
await hposHolochainCall({
method: 'post',
path: '/zome_call',
pathPrefix: '/api/v2',
responseType: 'arraybuffer',
params
})

Expand Down Expand Up @@ -885,11 +902,17 @@ export function useHposInterface(): HposInterface {
}

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const reserveAccountsDetails: ReserveSettingsResponse = (await hposHolochainCall({
const response = await hposHolochainCall({
method: 'post',
path: '/zome_call',
pathPrefix: '/api/v2',
responseType: 'arraybuffer',
params: getReserveDetailsParams
})) as ReserveSettingsResponse
})

const reserveAccountsDetails: ReserveSettingsResponse = decode(
response
) as ReserveSettingsResponse

if (reserveAccountsDetails[0]) {
const initiateRedemptionParams = {
Expand All @@ -906,11 +929,17 @@ export function useHposInterface(): HposInterface {
}

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const transaction: RedemptionTransaction = (await hposHolochainCall({
const response = await hposHolochainCall({
method: 'post',
path: '/zome_call',
pathPrefix: '/api/v2',
responseType: 'arraybuffer',
params: initiateRedemptionParams
})) as RedemptionTransaction
})

const transaction: RedemptionTransaction = decode(
response
) as RedemptionTransaction

return transaction
}
Expand Down
2 changes: 1 addition & 1 deletion ui-common-library
Submodule ui-common-library updated 0 files
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,11 @@
"@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14"

"@msgpack/msgpack@^2.8.0":
version "2.8.0"
resolved "https://registry.yarnpkg.com/@msgpack/msgpack/-/msgpack-2.8.0.tgz#4210deb771ee3912964f14a15ddfb5ff877e70b9"
integrity sha512-h9u4u/jiIRKbq25PM+zymTyW6bhTzELvOoUd+AvYriWOAKpLGnIamaET3pnHYoI5iYphAHBI4ayx0MehR+VVPQ==

"@ndelangen/get-tarball@^3.0.7":
version "3.0.7"
resolved "https://registry.yarnpkg.com/@ndelangen/get-tarball/-/get-tarball-3.0.7.tgz#87c7aef2df4ff4fbdbab6ac9ed32cee142c4b1a3"
Expand Down

0 comments on commit 207ea2b

Please sign in to comment.