Skip to content

Commit

Permalink
feat(apps/hermes/client): allow authorization headers
Browse files Browse the repository at this point in the history
  • Loading branch information
cctdaniel committed Jun 27, 2024
1 parent 35ee573 commit 8c2d2b0
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions apps/hermes/client/js/src/HermesClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ export class HermesClient {
this.httpRetries = config?.httpRetries ?? DEFAULT_HTTP_RETRIES;
}

private extractCredentials(url: URL): { headers: HeadersInit; url: URL } {
const headers: HeadersInit = {};
if (url.username && url.password) {
headers["Authorization"] = `Basic ${btoa(
`${url.username}:${url.password}`
)}`;
url.username = "";
url.password = "";
}
return { headers, url };
}

private async httpRequest<ResponseData>(
url: string,
schema: z.ZodSchema<ResponseData>,
Expand Down Expand Up @@ -100,13 +112,16 @@ export class HermesClient {
query?: string;
filter?: string;
}): Promise<PriceFeedMetadata[]> {
const url = new URL("v2/price_feeds", this.baseURL);
const baseURL = new URL(this.baseURL);
const { headers, url: cleanedBaseURL } = this.extractCredentials(baseURL);
const url = new URL("v2/price_feeds", cleanedBaseURL);
if (options) {
this.appendUrlSearchParams(url, options);
}
return await this.httpRequest(
url.toString(),
schemas.PriceFeedMetadata.array()
schemas.PriceFeedMetadata.array(),
{ headers }
);
}

Expand All @@ -129,7 +144,9 @@ export class HermesClient {
parsed?: boolean;
}
): Promise<PriceUpdate> {
const url = new URL(`v2/updates/price/latest`, this.baseURL);
const baseURL = new URL(this.baseURL);
const { headers, url: cleanedBaseURL } = this.extractCredentials(baseURL);
const url = new URL("v2/updates/price/latest", cleanedBaseURL);
for (const id of ids) {
url.searchParams.append("ids[]", id);
}
Expand All @@ -138,7 +155,7 @@ export class HermesClient {
this.appendUrlSearchParams(url, options);
}

return this.httpRequest(url.toString(), schemas.PriceUpdate);
return this.httpRequest(url.toString(), schemas.PriceUpdate, { headers });
}

/**
Expand All @@ -162,7 +179,9 @@ export class HermesClient {
parsed?: boolean;
}
): Promise<PriceUpdate> {
const url = new URL(`v2/updates/price/${publishTime}`, this.baseURL);
const baseURL = new URL(this.baseURL);
const { headers, url: cleanedBaseURL } = this.extractCredentials(baseURL);
const url = new URL(`v2/updates/price/${publishTime}`, cleanedBaseURL);
for (const id of ids) {
url.searchParams.append("ids[]", id);
}
Expand All @@ -171,7 +190,7 @@ export class HermesClient {
this.appendUrlSearchParams(url, options);
}

return this.httpRequest(url.toString(), schemas.PriceUpdate);
return this.httpRequest(url.toString(), schemas.PriceUpdate, { headers });
}

/**
Expand All @@ -198,7 +217,9 @@ export class HermesClient {
benchmarksOnly?: boolean;
}
): Promise<EventSource> {
const url = new URL("v2/updates/price/stream", this.baseURL);
const baseURL = new URL(this.baseURL);
const { headers, url: cleanedBaseURL } = this.extractCredentials(baseURL);
const url = new URL("v2/updates/price/stream", cleanedBaseURL);
ids.forEach((id) => {
url.searchParams.append("ids[]", id);
});
Expand All @@ -208,7 +229,7 @@ export class HermesClient {
this.appendUrlSearchParams(url, transformedOptions);
}

return new EventSource(url.toString());
return new EventSource(url.toString(), { headers });
}

private appendUrlSearchParams(
Expand Down

0 comments on commit 8c2d2b0

Please sign in to comment.