// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from '../resource'; import { isRequestOptions } from '../core'; import * as Core from '../core'; export class Objects extends APIResource { /** * Creates an Object. * * The request body is any JSON object. This is schemaless. * * Returns a JSON object with an 'id' set to the newly created ID. The 'Location' * HTTP response header is set to the URL of the new Object, which contains the * same ID. * * This is an asynchronous operation. If the Object already exists, a duplicate * will be created with a new ID. * * ## Using your own IDs * * This operation adds the object to the catalog and creates a unique ID for the * object. You must store this ID in order to refer to the object in future * operations (e.g. DELETE / PATCH). If you wish to create an Object with an ID of * your choice, you must * [use a PUT instead](/apis/ingestion/partially-update-object-in-the-object-store) * -- this creates an ID and returns it to you. */ create(body?: ObjectCreateParams, options?: Core.RequestOptions): Core.APIPromise; create(options?: Core.RequestOptions): Core.APIPromise; create( body?: ObjectCreateParams | Core.RequestOptions, options?: Core.RequestOptions, ): Core.APIPromise { if (isRequestOptions(body)) { return this.create(undefined, body); } return this._client.post('/objects', { body, ...options }); } /** * Upserts the Object to the Object Store. * * This creates the Object if it does not exist, or replaces its contents entirely * if it does exist. This is schemaless. If you want to specify the ID at creation * time, you must use this PUT instead of the POST, because the POST endpoint * creates an ID. * * Returns an empty JSON object (`{}`). Sets the 'Location' HTTP response header to * the URL of the upserted Object. * * This is an asynchronous operation. * * ## Examples * * When upserting objects, it is important to explicitly set the `id` field in your * request. If no `id` is set the upsert endpoint will also generate a unique ID * for the Object. The `id` field is used to match the Object to the existing * Object in the Object Store. * * In these examples, we'll be using `bash` commands. But they should be adaptable * to any programming language. * * ### Upserting a single Object * * ```bash * #!/bin/bash * API_KEY='YOUR_API_KEY' * OBJECT_ID='sku_123456' * OBJECT='{"name": "White T-Shirt", "color": "white", "size": "medium", "price": 10.99}' * * curl -X PUT "https://api.objective.inc/v1/objects/$OBJECT_ID" -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" -d "$OBJECT" * ``` * * ### Upserting multiple Objects from JSON data with existing IDs * * Often times your data will already have an ID field. In this case, you can use * your existing ID field to upsert the Object to the Object Store. In this * example, we will use the `product_id` field as the `id` for upserting the * Objects. For this to work, you'll want to make sure your system has * [jq](https://jqlang.github.io/jq/) installed. * * ```bash * API_KEY="YOUR_API_KEY" * # JSON data containing multiple objects * JSON_DATA='[ * {"product_id": "sku_123456", "name": "White T-Shirt", "color": "white", "size": "medium", "price": 10.99}, * {"product_id": "sku_123457", "name": "Black T-Shirt", "color": "black", "size": "large", "price": 11.99} * ]' * * # Loop through each object in the JSON array * echo "$JSON_DATA" | jq -c '.[]' | while read -r object; do * # Extract the id from the object * id=$(echo "$object" | jq -r '.product_id') * * # Make the curl request * response=$(curl -s -o /dev/null -w "%{http_code}" -X PUT "https://api.objective.inc/v1/objects/$id" -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" -d "$object") * done * ``` * * ### Batch Upserting Objects from JSON data * * Building on the previous example, this one downloads a JSON file with 10,000 * objects and upserts them to the Object Store in parallel. * * ```bash * #!/bin/bash * API_KEY='YOUR_API_KEY' * # Download JSON data from the provided URL * JSON_DATA=$(curl -s https://d11p8vtjlacpl4.cloudfront.net/demos/ecommerce/hm-10k.json) * * # Check if the download was successful * if [ $? -ne 0 ]; then * exit 1 * fi * * process_object() { * local object="$1" * id=$(echo "$object" | jq -r '.article_id') * status_code=$(curl -s -o /dev/null -w "%{http_code}" -X PUT "https://api.objective.inc/v1/objects/$id" -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" -d "$object") * if [ "$status_code" != "202" ]; then * echo "Error processing object $id: Status code $status_code" * fi * } * * export -f process_object * export API_KEY * * # Process objects in parallel, 100 at a time * echo "$JSON_DATA" | jq -c '.[]' | xargs -P 100 -I -J{} bash -c 'process_object "{}"' * ``` */ update( objectId: string, body?: ObjectUpdateParams, options?: Core.RequestOptions, ): Core.APIPromise; update(objectId: string, options?: Core.RequestOptions): Core.APIPromise; update( objectId: string, body?: ObjectUpdateParams | Core.RequestOptions, options?: Core.RequestOptions, ): Core.APIPromise { if (isRequestOptions(body)) { return this.update(objectId, undefined, body); } return this._client.put(`/objects/${objectId}`, { body, ...options }); } /** * List all objects in the object store. */ list(query?: ObjectListParams, options?: Core.RequestOptions): Core.APIPromise; list(options?: Core.RequestOptions): Core.APIPromise; list( query: ObjectListParams | Core.RequestOptions = {}, options?: Core.RequestOptions, ): Core.APIPromise { if (isRequestOptions(query)) { return this.list({}, query); } return this._client.get('/objects', { query, ...options }); } /** * Schedules an Object in the Object Store for deletion. * * This is an asynchronous operation. If no such Object exists, this does nothing. * * Search results and other APIs are cached for several minutes not just by this * API but possibly by third- party servers out of our control. See * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control * regarding the standard HTTP caching mechanism that we use to improve * performance. * * The same ID may be used later to create a new Object, the same or different. * * Returns an empty JSON object (`{}`). */ delete(objectId: string, options?: Core.RequestOptions): Core.APIPromise { return this._client.delete(`/objects/${objectId}`, options); } /** * Batch operations on objects. */ batch(body: ObjectBatchParams, options?: Core.RequestOptions): Core.APIPromise { return this._client.post('/objects:batch', { body, ...options }); } /** * Schedules all Objects in the Object Store for deletion. * * This is an asynchronous operation. If no Objects exist, this does nothing. * * Returns a `request-id` JSON object (`{"request-id": "..."}`). * * ## Example * * > This command will delete all Objects from your Object Store. This operation is * > not reversable. * * ```bash * curl -X POST -H "Authorization: Bearer YOUR_API_KEY" https://api.objective.inc/v1/objects:deleteAll * ``` */ deleteAll(options?: Core.RequestOptions): Core.APIPromise { return this._client.post('/objects:deleteAll', options); } /** * Get an Object by ID. * * ## Status * * Objects within an index can have 4 different status types. * * 1. `UPLOADED` - The state of an object that is in the object store but is * pending processing. * 2. `PROCESSING` - The state of an object that is currently being indexed. * 3. `READY` - The state of an object that is live and searchable. * 4. `ERROR` - The state of an object that has encountered errors during * processing; you can find out more information about the error by using the * object status API. * 5. `INCOMPLETE` - The state of an object that is only partially indexed but * still live and searchable. This means that at least one component (e.g., a * text embedding, an image embedding, or a lexical vector) has been * successfully processed, while other components failed. Objects in this state * can still appear in search results based on the successfully indexed parts. */ get(objectId: string, options?: Core.RequestOptions): Core.APIPromise { return this._client.get(`/objects/${objectId}`, options); } /** * Get an Object's indexing status by ID. Used to view the indexing status of an * individual Object across all Indexes that are marked to index the object. * * ## Status * * Objects within an index can have 4 different status types. * * 1. `UPLOADED` - The state of an object that is in the object store but is * pending processing. * 2. `PROCESSING` - The state of an object that is currently being indexed. * 3. `READY` - The state of an object that is live and searchable. * 4. `ERROR` - The state of an object that has encountered errors during * processing; you can find out more information about the error by using the * object status API. */ status( objectId: string, query?: ObjectStatusParams | null | undefined, options?: Core.RequestOptions, ): Core.APIPromise { return this._client.get(`/objects/${objectId}/status`, options); } } export interface ObjectCreateResponse { id: string; } export interface ObjectUpdateResponse { id: string; } export interface ObjectListResponse { metadata: ObjectListResponse.Metadata; objects: Array; pagination: ObjectListResponse.Pagination; } export namespace ObjectListResponse { export interface Metadata { count: number; } export interface Object { id: string; date_created: string; date_updated: string; status: Object.Status; object?: unknown; } export namespace Object { export interface Status { indexes: Array; } export namespace Status { export interface Index { /** * Index ID */ id: string; /** * Index Status Type */ status: 'UPLOADED' | 'PROCESSING' | 'READY' | 'ERROR' | 'INCOMPLETE'; } } } export interface Pagination { next: string | null; prev: string | null; } } export interface ObjectDeleteResponse { id: string; } export interface ObjectBatchResponse { results: Array; } export namespace ObjectBatchResponse { export interface Result { method: 'PUT' | 'POST' | 'DELETE'; object_id: string; } } export interface ObjectDeleteAllResponse { 'request-id': string; } export interface ObjectGetResponse { id: string; date_created: string; date_updated: string; status: ObjectGetResponse.Status; object?: unknown; } export namespace ObjectGetResponse { export interface Status { indexes: Array; } export namespace Status { export interface Index { /** * Index ID */ id: string; /** * Index Status Type */ status: 'UPLOADED' | 'PROCESSING' | 'READY' | 'ERROR' | 'INCOMPLETE'; } } } export interface ObjectStatusResponse { indexes: Array; } export namespace ObjectStatusResponse { export interface Index { /** * Index ID */ id: string; /** * Index Status Type */ status: 'UPLOADED' | 'PROCESSING' | 'READY' | 'ERROR' | 'INCOMPLETE'; message?: string; } } export type ObjectCreateParams = unknown; export type ObjectUpdateParams = unknown; export interface ObjectListParams { cursor?: string; include_metadata?: boolean; include_object?: boolean; limit?: number; } export interface ObjectBatchParams { operations: Array< ObjectBatchParams.PutOperation | ObjectBatchParams.PostOperation | ObjectBatchParams.DeleteOperation >; } export namespace ObjectBatchParams { /** * Upsert an object */ export interface PutOperation { method: 'PUT'; object: Record; object_id: string; } /** * Create an object */ export interface PostOperation { method: 'POST'; object: Record; } /** * Delete an object */ export interface DeleteOperation { method: 'DELETE'; object_id: string; } } export interface ObjectStatusParams {} export declare namespace Objects { export { type ObjectCreateResponse as ObjectCreateResponse, type ObjectUpdateResponse as ObjectUpdateResponse, type ObjectListResponse as ObjectListResponse, type ObjectDeleteResponse as ObjectDeleteResponse, type ObjectBatchResponse as ObjectBatchResponse, type ObjectDeleteAllResponse as ObjectDeleteAllResponse, type ObjectGetResponse as ObjectGetResponse, type ObjectStatusResponse as ObjectStatusResponse, type ObjectCreateParams as ObjectCreateParams, type ObjectUpdateParams as ObjectUpdateParams, type ObjectListParams as ObjectListParams, type ObjectBatchParams as ObjectBatchParams, type ObjectStatusParams as ObjectStatusParams, }; }