Skip to content

Commit

Permalink
feat(api): update via SDK Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed May 8, 2024
1 parent 39b1bdf commit 751cfec
Show file tree
Hide file tree
Showing 23 changed files with 235 additions and 202 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 38
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/plastic-labs%2Fhoncho-72865b06002229b1e9c394214250259bfa3eb0053af2ab20aabe02940b6e40a8.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/plastic-labs%2Fhoncho-60707ccc3985eec6cb8cc1ba1bdd9eefb50e37aeeb524e34ad05dcfebcb12a77.yml
39 changes: 6 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ The full API of this library can be found in [api.md](api.md).
```js
import Honcho from 'honcho';

const honcho = new Honcho();
const honcho = new Honcho({
apiKey: process.env['HONCHO_AUTH_TOKEN'], // This is the default and can be omitted
});

async function main() {
const app = await honcho.apps.create({ name: 'string' });
Expand All @@ -44,7 +46,9 @@ This library includes TypeScript definitions for all request params and response
```ts
import Honcho from 'honcho';

const honcho = new Honcho();
const honcho = new Honcho({
apiKey: process.env['HONCHO_AUTH_TOKEN'], // This is the default and can be omitted
});

async function main() {
const params: Honcho.AppCreateParams = { name: 'string' };
Expand Down Expand Up @@ -134,37 +138,6 @@ On timeout, an `APIConnectionTimeoutError` is thrown.

Note that requests which time out will be [retried twice by default](#retries).

## Auto-pagination

List methods in the Honcho API are paginated.
You can use `for await … of` syntax to iterate through items across all pages:

```ts
async function fetchAllAppsUsers(params) {
const allAppsUsers = [];
// Automatically fetches more pages as needed.
for await (const user of honcho.apps.users.list('REPLACE_ME')) {
allAppsUsers.push(user);
}
return allAppsUsers;
}
```

Alternatively, you can make request a single page at a time:

```ts
let page = await honcho.apps.users.list('REPLACE_ME');
for (const user of page.items) {
console.log(user);
}

// Convenience methods are provided for manually paginating:
while (page.hasNextPage()) {
page = page.getNextPage();
// ...
}
```

## Advanced Usage

### Accessing raw Response data (e.g., headers)
Expand Down
14 changes: 7 additions & 7 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Methods:

- <code title="post /apps/{app_id}/users">client.apps.users.<a href="./src/resources/apps/users/users.ts">create</a>(appId, { ...params }) -> User</code>
- <code title="put /apps/{app_id}/users/{user_id}">client.apps.users.<a href="./src/resources/apps/users/users.ts">update</a>(appId, userId, { ...params }) -> User</code>
- <code title="get /apps/{app_id}/users">client.apps.users.<a href="./src/resources/apps/users/users.ts">list</a>(appId, { ...params }) -> UsersPage</code>
- <code title="get /apps/{app_id}/users">client.apps.users.<a href="./src/resources/apps/users/users.ts">list</a>(appId, { ...params }) -> PageUser</code>
- <code title="get /apps/{app_id}/users/{user_id}">client.apps.users.<a href="./src/resources/apps/users/users.ts">get</a>(appId, userId) -> User</code>
- <code title="get /apps/{app_id}/users/name/{name}">client.apps.users.<a href="./src/resources/apps/users/users.ts">getByName</a>(appId, name) -> User</code>
- <code title="get /apps/{app_id}/users/get_or_create/{name}">client.apps.users.<a href="./src/resources/apps/users/users.ts">getOrCreate</a>(appId, name) -> User</code>
Expand All @@ -42,11 +42,11 @@ Methods:

- <code title="post /apps/{app_id}/users/{user_id}/sessions">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">create</a>(appId, userId, { ...params }) -> Session</code>
- <code title="put /apps/{app_id}/users/{user_id}/sessions/{session_id}">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">update</a>(appId, userId, sessionId, { ...params }) -> Session</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">list</a>(appId, userId, { ...params }) -> SessionsPage</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">list</a>(appId, userId, { ...params }) -> PageSession</code>
- <code title="delete /apps/{app_id}/users/{user_id}/sessions/{session_id}">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">delete</a>(appId, userId, sessionId) -> unknown</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/chat">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">chat</a>(appId, userId, sessionId, { ...params }) -> AgentChat</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">get</a>(appId, userId, sessionId) -> Session</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/chat/stream">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">stream</a>(appId, userId, sessionId, { ...params }) -> string</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/chat/stream">client.apps.users.sessions.<a href="./src/resources/apps/users/sessions/sessions.ts">stream</a>(appId, userId, sessionId, { ...params }) -> unknown</code>

#### Messages

Expand All @@ -59,7 +59,7 @@ Methods:

- <code title="post /apps/{app_id}/users/{user_id}/sessions/{session_id}/messages">client.apps.users.sessions.messages.<a href="./src/resources/apps/users/sessions/messages.ts">create</a>(appId, userId, sessionId, { ...params }) -> Message</code>
- <code title="put /apps/{app_id}/users/{user_id}/sessions/{session_id}/messages/{message_id}">client.apps.users.sessions.messages.<a href="./src/resources/apps/users/sessions/messages.ts">update</a>(appId, userId, sessionId, messageId, { ...params }) -> Message</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/messages">client.apps.users.sessions.messages.<a href="./src/resources/apps/users/sessions/messages.ts">list</a>(appId, userId, sessionId, { ...params }) -> MessagesPage</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/messages">client.apps.users.sessions.messages.<a href="./src/resources/apps/users/sessions/messages.ts">list</a>(appId, userId, sessionId, { ...params }) -> PageMessage</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/messages/{message_id}">client.apps.users.sessions.messages.<a href="./src/resources/apps/users/sessions/messages.ts">get</a>(appId, userId, sessionId, messageId) -> Message</code>

#### Metamessages
Expand All @@ -73,7 +73,7 @@ Methods:

- <code title="post /apps/{app_id}/users/{user_id}/sessions/{session_id}/metamessages">client.apps.users.sessions.metamessages.<a href="./src/resources/apps/users/sessions/metamessages.ts">create</a>(appId, userId, sessionId, { ...params }) -> Metamessage</code>
- <code title="put /apps/{app_id}/users/{user_id}/sessions/{session_id}/metamessages/{metamessage_id}">client.apps.users.sessions.metamessages.<a href="./src/resources/apps/users/sessions/metamessages.ts">update</a>(appId, userId, sessionId, metamessageId, { ...params }) -> Metamessage</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/metamessages">client.apps.users.sessions.metamessages.<a href="./src/resources/apps/users/sessions/metamessages.ts">list</a>(appId, userId, sessionId, { ...params }) -> MetamessagesPage</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/metamessages">client.apps.users.sessions.metamessages.<a href="./src/resources/apps/users/sessions/metamessages.ts">list</a>(appId, userId, sessionId, { ...params }) -> PageMetamessage</code>
- <code title="get /apps/{app_id}/users/{user_id}/sessions/{session_id}/metamessages/{metamessage_id}">client.apps.users.sessions.metamessages.<a href="./src/resources/apps/users/sessions/metamessages.ts">get</a>(appId, userId, sessionId, metamessageId, { ...params }) -> Metamessage</code>

### Collections
Expand All @@ -89,7 +89,7 @@ Methods:

- <code title="post /apps/{app_id}/users/{user_id}/collections">client.apps.users.collections.<a href="./src/resources/apps/users/collections/collections.ts">create</a>(appId, userId, { ...params }) -> Collection</code>
- <code title="put /apps/{app_id}/users/{user_id}/collections/{collection_id}">client.apps.users.collections.<a href="./src/resources/apps/users/collections/collections.ts">update</a>(appId, userId, collectionId, { ...params }) -> Collection</code>
- <code title="get /apps/{app_id}/users/{user_id}/collections">client.apps.users.collections.<a href="./src/resources/apps/users/collections/collections.ts">list</a>(appId, userId, { ...params }) -> CollectionsPage</code>
- <code title="get /apps/{app_id}/users/{user_id}/collections">client.apps.users.collections.<a href="./src/resources/apps/users/collections/collections.ts">list</a>(appId, userId, { ...params }) -> PageCollection</code>
- <code title="delete /apps/{app_id}/users/{user_id}/collections/{collection_id}">client.apps.users.collections.<a href="./src/resources/apps/users/collections/collections.ts">delete</a>(appId, userId, collectionId) -> unknown</code>
- <code title="get /apps/{app_id}/users/{user_id}/collections/{collection_id}">client.apps.users.collections.<a href="./src/resources/apps/users/collections/collections.ts">get</a>(appId, userId, collectionId) -> Collection</code>
- <code title="get /apps/{app_id}/users/{user_id}/collections/name/{name}">client.apps.users.collections.<a href="./src/resources/apps/users/collections/collections.ts">getByName</a>(appId, userId, name) -> Collection</code>
Expand All @@ -107,6 +107,6 @@ Methods:

- <code title="post /apps/{app_id}/users/{user_id}/collections/{collection_id}/documents">client.apps.users.collections.documents.<a href="./src/resources/apps/users/collections/documents.ts">create</a>(appId, userId, collectionId, { ...params }) -> Document</code>
- <code title="put /apps/{app_id}/users/{user_id}/collections/{collection_id}/documents/{document_id}">client.apps.users.collections.documents.<a href="./src/resources/apps/users/collections/documents.ts">update</a>(appId, userId, collectionId, documentId, { ...params }) -> Document</code>
- <code title="get /apps/{app_id}/users/{user_id}/collections/{collection_id}/documents">client.apps.users.collections.documents.<a href="./src/resources/apps/users/collections/documents.ts">list</a>(appId, userId, collectionId, { ...params }) -> DocumentsPage</code>
- <code title="get /apps/{app_id}/users/{user_id}/collections/{collection_id}/documents">client.apps.users.collections.documents.<a href="./src/resources/apps/users/collections/documents.ts">list</a>(appId, userId, collectionId, { ...params }) -> PageDocument</code>
- <code title="delete /apps/{app_id}/users/{user_id}/collections/{collection_id}/documents/{document_id}">client.apps.users.collections.documents.<a href="./src/resources/apps/users/collections/documents.ts">delete</a>(appId, userId, collectionId, documentId) -> unknown</code>
- <code title="get /apps/{app_id}/users/{user_id}/collections/{collection_id}/documents/{document_id}">client.apps.users.collections.documents.<a href="./src/resources/apps/users/collections/documents.ts">get</a>(appId, userId, collectionId, documentId) -> Document</code>
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 Pagination from 'honcho/pagination';
import * as API from 'honcho/resources/index';

export interface ClientOptions {
/**
* Defaults to process.env['HONCHO_AUTH_TOKEN'].
*/
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 Honcho API. */
export class Honcho extends Core.APIClient {
apiKey: string;

private _options: ClientOptions;

/**
* API Client for interfacing with the Honcho API.
*
* @param {string | undefined} [opts.apiKey=process.env['HONCHO_AUTH_TOKEN'] ?? undefined]
* @param {string} [opts.baseURL=process.env['HONCHO_BASE_URL'] ?? https://localhost:8080/test-api] - 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 Honcho 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('HONCHO_BASE_URL'), ...opts }: ClientOptions = {}) {
constructor({
baseURL = Core.readEnv('HONCHO_BASE_URL'),
apiKey = Core.readEnv('HONCHO_AUTH_TOKEN'),
...opts
}: ClientOptions = {}) {
if (apiKey === undefined) {
throw new Errors.HonchoError(
"The HONCHO_AUTH_TOKEN environment variable is missing or empty; either provide it, or instantiate the Honcho client with an apiKey option, like new Honcho({ apiKey: 'My API Key' }).",
);
}

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

this.apiKey = apiKey;
}

apps: API.Apps = new API.Apps(this);
Expand All @@ -109,6 +130,10 @@ export class Honcho extends Core.APIClient {
};
}

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

static Honcho = this;

static HonchoError = Errors.HonchoError;
Expand Down
1 change: 0 additions & 1 deletion src/resources/apps/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export namespace Apps {
export import Users = UsersAPI.Users;
export import PageUser = UsersAPI.PageUser;
export import User = UsersAPI.User;
export import UsersPage = UsersAPI.UsersPage;
export import UserCreateParams = UsersAPI.UserCreateParams;
export import UserUpdateParams = UsersAPI.UserUpdateParams;
export import UserListParams = UsersAPI.UserListParams;
Expand Down
10 changes: 1 addition & 9 deletions src/resources/apps/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

export { App, AppCreateParams, AppUpdateParams, Apps } from './apps';
export {
PageUser,
User,
UserCreateParams,
UserUpdateParams,
UserListParams,
UsersPage,
Users,
} from './users/index';
export { PageUser, User, UserCreateParams, UserUpdateParams, UserListParams, Users } from './users/index';
40 changes: 19 additions & 21 deletions src/resources/apps/users/collections/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { APIResource } from 'honcho/resource';
import { isRequestOptions } from 'honcho/core';
import * as CollectionsAPI from 'honcho/resources/apps/users/collections/collections';
import * as DocumentsAPI from 'honcho/resources/apps/users/collections/documents';
import { Page, type PageParams } from 'honcho/pagination';

export class Collections extends APIResource {
documents: DocumentsAPI.Documents = new DocumentsAPI.Documents(this._client);
Expand Down Expand Up @@ -52,25 +51,18 @@ export class Collections extends APIResource {
userId: string,
query?: CollectionListParams,
options?: Core.RequestOptions,
): Core.PagePromise<CollectionsPage, Collection>;
list(
appId: string,
userId: string,
options?: Core.RequestOptions,
): Core.PagePromise<CollectionsPage, Collection>;
): Core.APIPromise<PageCollection>;
list(appId: string, userId: string, options?: Core.RequestOptions): Core.APIPromise<PageCollection>;
list(
appId: string,
userId: string,
query: CollectionListParams | Core.RequestOptions = {},
options?: Core.RequestOptions,
): Core.PagePromise<CollectionsPage, Collection> {
): Core.APIPromise<PageCollection> {
if (isRequestOptions(query)) {
return this.list(appId, userId, {}, query);
}
return this._client.getAPIList(`/apps/${appId}/users/${userId}/collections`, CollectionsPage, {
query,
...options,
});
return this._client.get(`/apps/${appId}/users/${userId}/collections`, { query, ...options });
}

/**
Expand Down Expand Up @@ -126,8 +118,6 @@ export class Collections extends APIResource {
}
}

export class CollectionsPage extends Page<Collection> {}

export interface Collection {
id: string;

Expand All @@ -143,13 +133,13 @@ export interface Collection {
export interface PageCollection {
items: Array<Collection>;

page: number;
page: number | null;

size: number;
size: number | null;

total: number;
total: number | null;

pages?: number;
pages?: number | null;
}

export type CollectionDeleteResponse = unknown;
Expand All @@ -168,10 +158,20 @@ export interface CollectionUpdateParams {
metadata?: unknown | null;
}

export interface CollectionListParams extends PageParams {
export interface CollectionListParams {
filter?: string | null;

/**
* Page number
*/
page?: number;

reverse?: boolean | null;

/**
* Page size
*/
size?: number;
}

export interface CollectionQueryParams {
Expand All @@ -187,7 +187,6 @@ export namespace Collections {
export import PageCollection = CollectionsAPI.PageCollection;
export import CollectionDeleteResponse = CollectionsAPI.CollectionDeleteResponse;
export import CollectionQueryResponse = CollectionsAPI.CollectionQueryResponse;
export import CollectionsPage = CollectionsAPI.CollectionsPage;
export import CollectionCreateParams = CollectionsAPI.CollectionCreateParams;
export import CollectionUpdateParams = CollectionsAPI.CollectionUpdateParams;
export import CollectionListParams = CollectionsAPI.CollectionListParams;
Expand All @@ -196,7 +195,6 @@ export namespace Collections {
export import Document = DocumentsAPI.Document;
export import PageDocument = DocumentsAPI.PageDocument;
export import DocumentDeleteResponse = DocumentsAPI.DocumentDeleteResponse;
export import DocumentsPage = DocumentsAPI.DocumentsPage;
export import DocumentCreateParams = DocumentsAPI.DocumentCreateParams;
export import DocumentUpdateParams = DocumentsAPI.DocumentUpdateParams;
export import DocumentListParams = DocumentsAPI.DocumentListParams;
Expand Down
Loading

0 comments on commit 751cfec

Please sign in to comment.