Skip to content

Commit

Permalink
feat: setup upload inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniAkash committed Mar 13, 2024
1 parent 8c3ead6 commit 7411872
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/client/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {
import { parse } from "csv-parse";
import { finished } from "stream/promises";
import { uuid } from "uuidv4";
import { PostInputsRequest } from "clarifai-nodejs-grpc/proto/clarifai/api/service_pb";
import { promisifyGrpcCall } from "../utils/misc";
import { StatusCode } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_code_pb";

interface CSVRecord {
inputid: string;
Expand Down Expand Up @@ -741,4 +744,45 @@ export class Input extends Lister {
.setData(new Data().setRegionsList(regions));
return inputMaskProto;
}

async uploadInputs({
inputs,
showLog = true,
}: {
inputs: GrpcInput[];
showLog?: boolean;
}): Promise<string> {
if (!Array.isArray(inputs)) {
throw new Error("inputs must be an array of Input objects");
}
const inputJobId = uuid(); // generate a unique id for this job
const request = new PostInputsRequest()
.setUserAppId(this.userAppId)
.setInputsList(inputs)
.setInputsAddJobId(inputJobId);

const postInputs = promisifyGrpcCall(
this.STUB.client.postInputs,
this.STUB.client,
);

const response = await this.grpcRequest(postInputs, request);
const responseObject = response.toObject();
if (responseObject.status?.code !== StatusCode.SUCCESS) {
if (showLog) {
console.warn(responseObject.status?.description);
}
throw new Error(
`Inputs upload failed with response ${responseObject.status?.description}`,
);
} else {
if (showLog) {
console.info(
"\nInputs Uploaded\n%s",
responseObject.status?.description,
);
}
}
return inputJobId;
}
}

0 comments on commit 7411872

Please sign in to comment.