-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
286 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,86 @@ | ||
import * as grpc from "@grpc/grpc-js"; | ||
import * as jspb from "google-protobuf"; | ||
import { KWArgs } from "../utils/types"; | ||
import { BaseClient } from "./base"; | ||
import { StatusCode } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_code_pb"; | ||
import { Status } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_pb"; | ||
|
||
export class Lister extends BaseClient { | ||
defaultPageSize: number; | ||
|
||
constructor( | ||
kwargs: | ||
| { | ||
userId: string; | ||
appId: string; | ||
pat: string; | ||
token?: string; | ||
base?: string; | ||
ui?: string; | ||
} | ||
| Record<string, never> = {}, | ||
pageSize: number = 16, | ||
) { | ||
constructor({ | ||
kwargs = {}, | ||
pageSize = 16, | ||
}: { | ||
kwargs?: KWArgs; | ||
pageSize?: number; | ||
}) { | ||
super(kwargs); | ||
this.defaultPageSize = pageSize; | ||
} | ||
|
||
/** | ||
* TODO: Implement the actual pagination logic | ||
*/ | ||
listPagesGenerator() { | ||
throw new Error("Not implemented"); | ||
async *listPagesGenerator< | ||
TRequest extends jspb.Message, | ||
TResponseObject extends { status?: Status.AsObject }, | ||
TResponse extends { | ||
toObject: (arg?: boolean) => TResponseObject; | ||
}, | ||
>( | ||
endpoint: ( | ||
request: TRequest, | ||
metadata: grpc.Metadata, | ||
options: Partial<grpc.CallOptions>, | ||
) => Promise<TResponse>, | ||
requestData: TRequest, | ||
pageNo: number = 1, | ||
perPage: number = this.defaultPageSize, | ||
) { | ||
// Initial setup | ||
let page = pageNo; | ||
|
||
while (true) { | ||
// Prepare request data | ||
// @ts-expect-error - TS doesn't know that the method exists | ||
requestData["page"] = page; | ||
if (perPage) { | ||
// @ts-expect-error - TS doesn't know that the method exists | ||
requestData["per_page"] = perPage; | ||
} | ||
|
||
// Perform gRPC request | ||
const response = await this.grpcRequest(endpoint, requestData); | ||
|
||
// Check response status | ||
if (response.toObject().status?.code !== StatusCode.SUCCESS) { | ||
throw new Error(`Listing failed with response ${response}`); | ||
} | ||
|
||
// Process and yield response items | ||
if (Object.keys(response).length === 1) { | ||
break; | ||
} else { | ||
yield response; | ||
// const listingResource = Object.keys(response)[1]; | ||
// for (const item of response[listingResource]) { | ||
// if (listingResource === "dataset_inputs") { | ||
// yield this.processResponseKeys( | ||
// item["input"], | ||
// listingResource.slice(0, -1), | ||
// ); | ||
// } else { | ||
// yield this.processResponseKeys(item, listingResource.slice(0, -1)); | ||
// } | ||
// } | ||
} | ||
|
||
// Exit loop if pagination is not to be continued | ||
if (pageNo !== undefined || perPage !== undefined) { | ||
break; | ||
} | ||
page += 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// import { StatusCode } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_code_pb"; | ||
import { Lister } from "./lister"; | ||
import { KWArgs } from "../utils/types"; | ||
import { ListAppsRequest } from "clarifai-nodejs-grpc/proto/clarifai/api/service_pb"; | ||
import { mapParamsToRequest, promisifyGrpcCall } from "../utils/misc"; | ||
|
||
// interface UserAppID { | ||
// userId?: string; | ||
// appId?: string; | ||
// } | ||
|
||
// interface AppInfo { | ||
// // Define properties based on the Python code's usage | ||
// } | ||
|
||
// interface RunnerInfo { | ||
// // Define properties based on the Python code's usage | ||
// } | ||
|
||
export class User extends Lister { | ||
// private logger: Logger; | ||
|
||
constructor(kwargs: KWArgs = {}) { | ||
super({ kwargs }); | ||
// this.logger = getLogger("INFO", __filename); | ||
} | ||
|
||
// Convert generator functions to async functions returning Promises of arrays | ||
async *listApps({ | ||
params = {}, | ||
pageNo, | ||
perPage, | ||
}: { | ||
params?: | ||
| Omit<ListAppsRequest.AsObject, "userAppId"> | ||
| Record<string, never>; | ||
pageNo?: number; | ||
perPage?: number; | ||
}) { | ||
const listApps = promisifyGrpcCall(this.STUB.client.listApps); | ||
const request = new ListAppsRequest(); | ||
mapParamsToRequest(params, request); | ||
|
||
yield this.listPagesGenerator(listApps, request, pageNo, perPage); | ||
} | ||
|
||
// async listRunners( | ||
// filterBy: Record<string, any> = {}, | ||
// pageNo?: number, | ||
// perPage?: number, | ||
// ) {} | ||
|
||
// async createApp( | ||
// appId: string, | ||
// baseWorkflow: string = "Empty", | ||
// kwargs: Record<string, any> = {}, | ||
// ) {} | ||
|
||
// TypeScript does not have a direct equivalent to Python's __getattr__, so this functionality may need to be implemented differently if required. | ||
|
||
toString() { | ||
// Implementation of a method to return user details as a string | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.