Skip to content

Commit

Permalink
add client get balances method
Browse files Browse the repository at this point in the history
  • Loading branch information
kirahsapong committed Mar 27, 2024
1 parent ef61bb3 commit 14c8e0a
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/http-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { JwtPayload } from '@web5/crypto'
import type { ErrorDetail } from './types.js'
import type { DidDocument, BearerDid } from '@web5/dids'
import {
Balance,
Close,
MessageModel,
Order,
Expand Down Expand Up @@ -165,6 +166,40 @@ export class TbdexHttpClient {
return data
}

/**
* gets balances from the pfi provided
* @param opts - options
* @beta
*/
static async getBalances(opts: GetBalancesOptions): Promise<Balance[]> {
const { pfiDid, did } = opts

const pfiServiceEndpoint = await TbdexHttpClient.getPfiServiceEndpoint(pfiDid)
const apiRoute = `${pfiServiceEndpoint}/balances`
const requestToken = await TbdexHttpClient.generateRequestToken({ requesterDid: did, pfiDid })

let response: Response
try {
response = await fetch(apiRoute, {
headers: {
authorization: `Bearer ${requestToken}`
}
})
} catch (e) {
throw new RequestError({ message: `Failed to get balances from ${pfiDid}`, recipientDid: pfiDid, url: apiRoute, cause: e })
}

if (!response.ok) {
const errorDetails = await response.json() as ErrorDetail[]
throw new ResponseError({ statusCode: response.status, details: errorDetails, recipientDid: pfiDid, url: apiRoute })
}

const responseBody = await response.json() as { data: Balance[] }
const data: Balance[] = responseBody.data

return data
}

/**
* get a specific exchange from the pfi provided
* @param opts - options
Expand Down Expand Up @@ -370,6 +405,16 @@ export type GetOfferingsOptions = {
}
}

/**
* options passed to {@link TbdexHttpClient.getBalances} method
* @beta
*/
export type GetBalancesOptions = {
/** the DID of the PFI from whom you want to get balances */
pfiDid: string
did: BearerDid
}

/**
* options passed to {@link TbdexHttpClient.getExchange} method
* @beta
Expand Down

0 comments on commit 14c8e0a

Please sign in to comment.