Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/bw/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Readable } from "node:stream";
import { TokenManager } from "./token.js";
import { USER_AGENT } from "./user-agent.js";

/**
* Validate an identifier before interpolating it into a Bandwidth API URL path.
Expand Down Expand Up @@ -111,7 +112,7 @@ export function createBwClient(cfg: {
async createCall({ to, from, answerUrl }) {
const res = await fetchImpl(`${base}/accounts/${cfg.accountId}/calls`, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: await authHeader() },
headers: { "Content-Type": "application/json", Authorization: await authHeader(), "User-Agent": USER_AGENT },
body: JSON.stringify({ to, from, answerUrl, applicationId: cfg.applicationId }),
});
if (!res.ok)
Expand All @@ -122,15 +123,15 @@ export function createBwClient(cfg: {
async modifyCall(callId, opts) {
const res = await fetchImpl(`${base}/accounts/${cfg.accountId}/calls/${safeId(callId)}`, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: await authHeader() },
headers: { "Content-Type": "application/json", Authorization: await authHeader(), "User-Agent": USER_AGENT },
body: JSON.stringify(opts),
});
if (!res.ok)
throw new Error(`Bandwidth modifyCall failed: ${res.status} ${await res.text()}`);
},
async getCall(callId) {
const res = await fetchImpl(`${base}/accounts/${cfg.accountId}/calls/${safeId(callId)}`, {
headers: { Accept: "application/json", Authorization: await authHeader() },
headers: { Accept: "application/json", Authorization: await authHeader(), "User-Agent": USER_AGENT },
});
if (!res.ok)
throw new Error(`Bandwidth getCall failed: ${res.status} ${await res.text()}`);
Expand All @@ -140,7 +141,7 @@ export function createBwClient(cfg: {
},
async listRecordings(callId) {
const res = await fetchImpl(`${base}/accounts/${cfg.accountId}/calls/${safeId(callId)}/recordings`, {
headers: { Accept: "application/json", Authorization: await authHeader() },
headers: { Accept: "application/json", Authorization: await authHeader(), "User-Agent": USER_AGENT },
});
if (!res.ok)
throw new Error(`Bandwidth listRecordings failed: ${res.status} ${await res.text()}`);
Expand All @@ -150,7 +151,7 @@ export function createBwClient(cfg: {
async getRecording(callId, recordingId) {
const res = await fetchImpl(
`${base}/accounts/${cfg.accountId}/calls/${safeId(callId)}/recordings/${safeId(recordingId)}`,
{ headers: { Accept: "application/json", Authorization: await authHeader() } },
{ headers: { Accept: "application/json", Authorization: await authHeader(), "User-Agent": USER_AGENT } },
);
if (!res.ok)
throw new Error(`Bandwidth getRecording failed: ${res.status} ${await res.text()}`);
Expand All @@ -160,7 +161,7 @@ export function createBwClient(cfg: {
async getRecordingMedia(callId, recordingId) {
const res = await fetchImpl(
`${base}/accounts/${cfg.accountId}/calls/${safeId(callId)}/recordings/${safeId(recordingId)}/media`,
{ headers: { Authorization: await authHeader() } },
{ headers: { Authorization: await authHeader(), "User-Agent": USER_AGENT } },
);
if (!res.ok || !res.body)
throw new Error(`Bandwidth getRecordingMedia failed: ${res.status} ${await res.text()}`);
Expand All @@ -173,7 +174,7 @@ export function createBwClient(cfg: {
async updateRecording(callId, state) {
const res = await fetchImpl(`${base}/accounts/${cfg.accountId}/calls/${safeId(callId)}/recording`, {
method: "PUT",
headers: { "Content-Type": "application/json", Authorization: await authHeader() },
headers: { "Content-Type": "application/json", Authorization: await authHeader(), "User-Agent": USER_AGENT },
body: JSON.stringify({ state }),
});
if (!res.ok)
Expand Down
8 changes: 7 additions & 1 deletion src/bw/token.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { USER_AGENT } from "./user-agent.js";

export interface TokenManagerOpts {
clientId: string;
clientSecret: string;
Expand Down Expand Up @@ -40,7 +42,11 @@ export class TokenManager {
const basic = "Basic " + Buffer.from(`${this.opts.clientId}:${this.opts.clientSecret}`).toString("base64");
const res = await this.fetchImpl(`${this.opts.apiHost}/api/v1/oauth2/token`, {
method: "POST",
headers: { Authorization: basic, "Content-Type": "application/x-www-form-urlencoded" },
headers: {
Authorization: basic,
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": USER_AGENT,
},
body: "grant_type=client_credentials",
});
if (!res.ok)
Expand Down
7 changes: 7 additions & 0 deletions src/bw/user-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pkg from "../../package.json" with { type: "json" };

/**
* Client identifier sent on every outbound Bandwidth request so BW can
* attribute traffic to this adapter and version. Format: `bw-voice-adapter/<version>`.
*/
export const USER_AGENT = `bw-voice-adapter/${pkg.version}`;