Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/api keys #122

Merged
merged 20 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/apis/apiKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { ApiResource } from "../apiResource";
import { APIResponseType, ApiClientInterface } from "../_types/generalTypes";
import { APIPromise, RequestOptions } from "../baseClient";
import { createHeaders } from "./createHeaders";
import { toQueryParams } from "../utils";

export interface ApiKeysAddParams {
type?: string;
"sub-type"?: string;
name?: string;
description?: string;
workspace_id?:string;
user_id?:string;
rate_limits?: Record<string, any>[];
usage_limits?: Record<string, any>;
scopes: string[];
defaults?: Record<string, any>;
}
export interface ApiKeysAddResponse extends APIResponseType {
id?: string;
key?: string;
object?: string;
}
export interface ApiKeysGetParams {
id?: string;
}
export interface ApiKeysGetResponse extends APIResponseType {
id?: string;
key?: string;
name?: string;
description?: string;
type?: string;
organisation_id?: string;
workspace_id?: string;
user_id?: string;
status?: string;
created_at?: Date;
last_updated_at?: Date;
creation_mode?: string;
rate_limits?: Record<string, any>[];
usage_limits?: Record<string, any>;
reset_usage?:number;
scopes?: string[];
defaults?: Record<string, any>;
object?: string;
}
export interface ApiKeysUpdateParams {
id?: string;
name?: string;
description?: string;
rate_limits?: Record<string, any>[];
usage_limits?: Record<string, any>;
scopes?: string[];
defaults?: Record<string, any>;
}
export interface ApiKeysListParams {
page_size?: number;
current_page?: number;
workspace_id?: string;
}
export interface ApiKeysListResponse extends APIResponseType {
total?: number;
object?: string;
data?: Record<string, any>[];
}
export interface ApiKeysDeleteParams {
id?: string;
}
export class ApiKeys extends ApiResource {
create(
_body: ApiKeysAddParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<ApiKeysAddResponse> {
const body = _body;
const type = body.type;
const subType = body["sub-type"];
if (params) {
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params }) }
}
const response = this.post<ApiKeysAddResponse>(`/api-keys/${type}/${subType}`, { body, ...opts });
return response;
}

retrieve(
_body: ApiKeysGetParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<ApiKeysGetResponse> {
const body = _body;
const id = body.id;
if (params) {
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params }) }
}
const response = this.getMethod<ApiKeysGetResponse>(`/api-keys/${id}`, { ...opts });
return response;
}

update(
_body: ApiKeysUpdateParams,
params?: ApiClientInterface,
opts?: RequestOptions
): APIPromise<any> {
const body = _body;
const id = body.id;
if (params) {
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params }) }
}
const response = this.put<any>(`/api-keys/${id}`, { body, ...opts });
return response;
}
list(
_body: ApiKeysListParams,
params?: ApiClientInterface,
opts?: RequestOptions
):APIPromise<ApiKeysListResponse>{
const body = _body;
if (params) {
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params }) }
}
const query = toQueryParams(body);
const response = this.getMethod<ApiKeysListResponse>(`/api-keys${query}`, {...opts });
return response;
}
delete(
_body: ApiKeysDeleteParams,
params?: ApiClientInterface,
opts?: RequestOptions
):APIPromise<any>{
const body = _body;
const id=body.id;
if (params) {
this.client.customHeaders = { ...this.client.customHeaders, ...createHeaders({ ...params }) }
}
const response = this.deleteMethod<any>(`/api-keys/${id}`, { body, ...opts });
return response;
}
}
1 change: 1 addition & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { Audio } from "./audio"
export { VectorStores } from "./vectorStores"
export { BetaChat } from "./betaChat"
export { Uploads } from "./uploads"
export { ApiKeys } from "./apiKeys"
26 changes: 13 additions & 13 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export class Portkey extends ApiClient {
azureResourceName?: string | null | undefined;
azureDeploymentId?: string | null | undefined;
azureApiVersion?: string | null | undefined;
azureEndpointName?: string | null | undefined;
azureEndpointName?: string | null | undefined;
huggingfaceBaseUrl?: string | null | undefined;
forwardHeaders?: Array<string> | null | undefined;
requestTimeout?: number | null | undefined;
cacheNamespace?: string | null | undefined;
strictOpenAiCompliance?: boolean | null | undefined;
anthropicBeta?: string | null | undefined;
anthropicVersion?: string | null | undefined;
mistralFimCompletion?: string | null | undefined;
anthropicVersion?: string | null | undefined;
mistralFimCompletion?: string | null | undefined;
constructor({
apiKey = readEnv("PORTKEY_API_KEY") ?? null,
baseURL = readEnv("PORTKEY_BASE_URL") ?? null,
Expand All @@ -63,15 +63,15 @@ export class Portkey extends ApiClient {
azureResourceName,
azureDeploymentId,
azureApiVersion,
azureEndpointName,
azureEndpointName,
huggingfaceBaseUrl,
forwardHeaders,
cacheNamespace,
requestTimeout,
strictOpenAiCompliance,
anthropicBeta,
anthropicVersion,
mistralFimCompletion
anthropicVersion,
mistralFimCompletion
}: ApiClientInterface) {

super({
Expand Down Expand Up @@ -99,14 +99,14 @@ export class Portkey extends ApiClient {
azureResourceName,
azureDeploymentId,
azureApiVersion,
azureEndpointName,
azureEndpointName,
huggingfaceBaseUrl,
forwardHeaders,
requestTimeout,
strictOpenAiCompliance,
anthropicBeta,
anthropicVersion,
mistralFimCompletion
anthropicVersion,
mistralFimCompletion
});

this.apiKey = apiKey;
Expand Down Expand Up @@ -135,14 +135,14 @@ export class Portkey extends ApiClient {
this.azureResourceName = azureResourceName;
this.azureDeploymentId = azureDeploymentId;
this.azureApiVersion = azureApiVersion;
this.azureEndpointName = azureEndpointName;
this.azureEndpointName = azureEndpointName;
this.huggingfaceBaseUrl = huggingfaceBaseUrl;
this.forwardHeaders = forwardHeaders;
this.requestTimeout = requestTimeout;
this.strictOpenAiCompliance = strictOpenAiCompliance;
this.anthropicBeta = anthropicBeta;
this.anthropicVersion = anthropicVersion;
this.mistralFimCompletion = mistralFimCompletion;
this.anthropicVersion = anthropicVersion;
this.mistralFimCompletion = mistralFimCompletion;
}

completions: API.Completions = new API.Completions(this);
Expand All @@ -160,6 +160,7 @@ export class Portkey extends ApiClient {
audio = new API.Audio(this);
uploads = new API.Uploads(this);
admin = new API.Admin(this);
apiKeys = new API.ApiKeys(this);
configs = new API.Configs(this);
beta = {
assistants: new API.Assistants(this),
Expand All @@ -168,7 +169,6 @@ export class Portkey extends ApiClient {
chat: new API.BetaChat(this),
};


post = (
url: string,
_body: PostBodyParams,
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createResponseHeaders } from "./streaming";
import OpenAI from "openai";
import type { Portkey } from "./index";
import { UserInviteListParams, UsersListParams, WorkspaceMemberListParams, WorkspacesListParams } from "./apis/admin";
import { ApiKeysListParams } from "./apis/apiKeys";
import { CongfigsListParams } from "./apis/configs";

type PlatformProperties = {
Expand Down Expand Up @@ -132,7 +133,7 @@ export function initOpenAIClient(client: Portkey){
maxRetries: 0
})
}
export function toQueryParams(params?: (UsersListParams | UserInviteListParams | WorkspacesListParams | WorkspaceMemberListParams | CongfigsListParams)): string {
export function toQueryParams(params?: (UsersListParams | UserInviteListParams | WorkspacesListParams | WorkspaceMemberListParams | ApiKeysListParams | CongfigsListParams)): string {
if (!params) {
return '';
}
Expand All @@ -142,4 +143,4 @@ export function toQueryParams(params?: (UsersListParams | UserInviteListParams |
.join('&');

return queryParams ? `?${queryParams}` : '';
}
}
Loading