Skip to content

Commit

Permalink
metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Nov 4, 2024
1 parent ad509b5 commit fff2e3b
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions util/portal-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface Block {
}


export interface Metadata {
isRealTime: boolean
}


export interface PortalClientOptions {
url: string
http?: HttpClient
Expand All @@ -41,7 +46,7 @@ export class PortalClient {
this.bufferThreshold = options.bufferThreshold ?? 10 * 1024 * 1024
}

private getRouterUrl(path: string): string {
private getDatasetUrl(path: string): string {
let u = new URL(this.url)
if (this.url.pathname.endsWith('/')) {
u.pathname += path
Expand All @@ -52,7 +57,7 @@ export class PortalClient {
}

async getHeight(): Promise<number> {
let res: string = await this.http.get(this.getRouterUrl('height'), {
let res: string = await this.http.get(this.getDatasetUrl('height'), {
retryAttempts: 3,
httpTimeout: 10_000,
})
Expand All @@ -61,9 +66,19 @@ export class PortalClient {
return height
}

async getMetadata(): Promise<Metadata> {
let res: {real_time: boolean} = await this.http.get(this.getDatasetUrl('metadata'), {
retryAttempts: 3,
httpTimeout: 10_000,
})
return {
isRealTime: !!res.real_time
}
}

query<B extends Block = Block, Q extends PortalQuery = PortalQuery>(query: Q): Promise<B[]> {
return this.http
.request<Buffer>('POST', this.getRouterUrl(`stream`), {
.request<Buffer>('POST', this.getDatasetUrl(`stream`), {
json: query,
retryAttempts: 3,
httpTimeout: this.queryTimeout,
Expand Down Expand Up @@ -96,7 +111,7 @@ export class PortalClient {
let archiveQuery = {...query, fromBlock}

let res = await this.http
.request<NodeJS.ReadableStream>('POST', this.getRouterUrl(`stream`), {
.request<NodeJS.ReadableStream>('POST', this.getDatasetUrl(`stream`), {
json: archiveQuery,
retryAttempts: 3,
httpTimeout: this.queryTimeout,
Expand Down Expand Up @@ -156,8 +171,3 @@ export class PortalClient {
}
}
}

export function portal(url: string | PortalClientOptions) {
let options = typeof url == 'string' ? {url} : url
return new PortalClient(options)
}

0 comments on commit fff2e3b

Please sign in to comment.