Skip to content

Commit

Permalink
feat(api): update via SDK Studio (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored Mar 27, 2024
1 parent 1dd6bc4 commit f810f4e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 19 deletions.
27 changes: 26 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import * as qs from 'qs';
import * as API from 'retell-sdk/resources/index';

export interface ClientOptions {
/**
* Defaults to process.env['RETELL_API_KEY'].
*/
apiKey?: string | undefined;

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*
Expand Down Expand Up @@ -67,11 +72,14 @@ export interface ClientOptions {

/** API Client for interfacing with the Retell API. */
export class Retell extends Core.APIClient {
apiKey: string;

private _options: ClientOptions;

/**
* API Client for interfacing with the Retell API.
*
* @param {string | undefined} [opts.apiKey=process.env['RETELL_API_KEY'] ?? undefined]
* @param {string} [opts.baseURL=process.env['RETELL_BASE_URL'] ?? https://api.retellai.com] - Override the default base URL for the API.
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
Expand All @@ -80,8 +88,19 @@ export class Retell extends Core.APIClient {
* @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API.
* @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.
*/
constructor({ baseURL = Core.readEnv('RETELL_BASE_URL'), ...opts }: ClientOptions = {}) {
constructor({
baseURL = Core.readEnv('RETELL_BASE_URL'),
apiKey = Core.readEnv('RETELL_API_KEY'),
...opts
}: ClientOptions = {}) {
if (apiKey === undefined) {
throw new Errors.RetellError(
"The RETELL_API_KEY environment variable is missing or empty; either provide it, or instantiate the Retell client with an apiKey option, like new Retell({ apiKey: 'My API Key' }).",
);
}

const options: ClientOptions = {
apiKey,
...opts,
baseURL: baseURL || `https://api.retellai.com`,
};
Expand All @@ -94,6 +113,8 @@ export class Retell extends Core.APIClient {
fetch: options.fetch,
});
this._options = options;

this.apiKey = apiKey;
}

call: API.Call = new API.Call(this);
Expand All @@ -112,6 +133,10 @@ export class Retell extends Core.APIClient {
};
}

protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
return { Authorization: `Bearer ${this.apiKey}` };
}

protected override stringifyQuery(query: Record<string, unknown>): string {
return qs.stringify(query, { arrayFormat: 'comma' });
}
Expand Down
5 changes: 4 additions & 1 deletion tests/api-resources/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import Retell from 'retell-sdk';
import { Response } from 'node-fetch';

const retell = new Retell({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' });
const retell = new Retell({
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

describe('resource agent', () => {
test('create: only required params', async () => {
Expand Down
5 changes: 4 additions & 1 deletion tests/api-resources/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import Retell from 'retell-sdk';
import { Response } from 'node-fetch';

const retell = new Retell({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' });
const retell = new Retell({
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

describe('resource call', () => {
test('create: only required params', async () => {
Expand Down
5 changes: 4 additions & 1 deletion tests/api-resources/llm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import Retell from 'retell-sdk';
import { Response } from 'node-fetch';

const retell = new Retell({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' });
const retell = new Retell({
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

describe('resource llm', () => {
test('create', async () => {
Expand Down
5 changes: 4 additions & 1 deletion tests/api-resources/phone-number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import Retell from 'retell-sdk';
import { Response } from 'node-fetch';

const retell = new Retell({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010' });
const retell = new Retell({
apiKey: 'My API Key',
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
});

describe('resource phoneNumber', () => {
test('create: only required params', async () => {
Expand Down
54 changes: 40 additions & 14 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('instantiate client', () => {
const client = new Retell({
baseURL: 'http://localhost:5000/',
defaultHeaders: { 'X-My-Default-Header': '2' },
apiKey: 'My API Key',
});

test('they are used in the request', () => {
Expand Down Expand Up @@ -51,27 +52,37 @@ describe('instantiate client', () => {

describe('defaultQuery', () => {
test('with null query params given', () => {
const client = new Retell({ baseURL: 'http://localhost:5000/', defaultQuery: { apiVersion: 'foo' } });
const client = new Retell({
baseURL: 'http://localhost:5000/',
defaultQuery: { apiVersion: 'foo' },
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo');
});

test('multiple default query params', () => {
const client = new Retell({
baseURL: 'http://localhost:5000/',
defaultQuery: { apiVersion: 'foo', hello: 'world' },
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo&hello=world');
});

test('overriding with `undefined`', () => {
const client = new Retell({ baseURL: 'http://localhost:5000/', defaultQuery: { hello: 'world' } });
const client = new Retell({
baseURL: 'http://localhost:5000/',
defaultQuery: { hello: 'world' },
apiKey: 'My API Key',
});
expect(client.buildURL('/foo', { hello: undefined })).toEqual('http://localhost:5000/foo');
});
});

test('custom fetch', async () => {
const client = new Retell({
baseURL: 'http://localhost:5000/',
apiKey: 'My API Key',
fetch: (url) => {
return Promise.resolve(
new Response(JSON.stringify({ url, custom: true }), {
Expand All @@ -88,6 +99,7 @@ describe('instantiate client', () => {
test('custom signal', async () => {
const client = new Retell({
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
apiKey: 'My API Key',
fetch: (...args) => {
return new Promise((resolve, reject) =>
setTimeout(
Expand All @@ -112,12 +124,12 @@ describe('instantiate client', () => {

describe('baseUrl', () => {
test('trailing slash', () => {
const client = new Retell({ baseURL: 'http://localhost:5000/custom/path/' });
const client = new Retell({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'My API Key' });
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
});

test('no trailing slash', () => {
const client = new Retell({ baseURL: 'http://localhost:5000/custom/path' });
const client = new Retell({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'My API Key' });
expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo');
});

Expand All @@ -126,41 +138,55 @@ describe('instantiate client', () => {
});

test('explicit option', () => {
const client = new Retell({ baseURL: 'https://example.com' });
const client = new Retell({ baseURL: 'https://example.com', apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://example.com');
});

test('env variable', () => {
process.env['RETELL_BASE_URL'] = 'https://example.com/from_env';
const client = new Retell({});
const client = new Retell({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://example.com/from_env');
});

test('empty env variable', () => {
process.env['RETELL_BASE_URL'] = ''; // empty
const client = new Retell({});
const client = new Retell({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://api.retellai.com');
});

test('blank env variable', () => {
process.env['RETELL_BASE_URL'] = ' '; // blank
const client = new Retell({});
const client = new Retell({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://api.retellai.com');
});
});

test('maxRetries option is correctly set', () => {
const client = new Retell({ maxRetries: 4 });
const client = new Retell({ maxRetries: 4, apiKey: 'My API Key' });
expect(client.maxRetries).toEqual(4);

// default
const client2 = new Retell({});
const client2 = new Retell({ apiKey: 'My API Key' });
expect(client2.maxRetries).toEqual(2);
});

test('with environment variable arguments', () => {
// set options via env var
process.env['RETELL_API_KEY'] = 'My API Key';
const client = new Retell();
expect(client.apiKey).toBe('My API Key');
});

test('with overriden environment variable arguments', () => {
// set options via env var
process.env['RETELL_API_KEY'] = 'another My API Key';
const client = new Retell({ apiKey: 'My API Key' });
expect(client.apiKey).toBe('My API Key');
});
});

describe('request building', () => {
const client = new Retell({});
const client = new Retell({ apiKey: 'My API Key' });

describe('Content-Length', () => {
test('handles multi-byte characters', () => {
Expand Down Expand Up @@ -202,7 +228,7 @@ describe('retries', () => {
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new Retell({ timeout: 10, fetch: testFetch });
const client = new Retell({ apiKey: 'My API Key', timeout: 10, fetch: testFetch });

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });
expect(count).toEqual(2);
Expand All @@ -229,7 +255,7 @@ describe('retries', () => {
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new Retell({ fetch: testFetch });
const client = new Retell({ apiKey: 'My API Key', fetch: testFetch });

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });
expect(count).toEqual(2);
Expand All @@ -256,7 +282,7 @@ describe('retries', () => {
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new Retell({ fetch: testFetch });
const client = new Retell({ apiKey: 'My API Key', fetch: testFetch });

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });
expect(count).toEqual(2);
Expand Down

0 comments on commit f810f4e

Please sign in to comment.