From c2c2413fcc5784e0cbb404086684ce0d4a6e9dc4 Mon Sep 17 00:00:00 2001 From: Richard Weerasinghe Date: Tue, 27 Aug 2024 14:22:18 +1200 Subject: [PATCH] add support for project service accounts --- openai/projects.go | 72 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/openai/projects.go b/openai/projects.go index 675f352..7d569e2 100644 --- a/openai/projects.go +++ b/openai/projects.go @@ -20,7 +20,6 @@ func (c *Client) Projects() *ProjectsEndpoint { return &ProjectsEndpoint{newOrganizationEndpoint(c, ProjectsEndpointPath)} } -// Project - OpenAPI Project. type Project struct { ID string `json:"id"` Object string `json:"object"` @@ -40,6 +39,34 @@ type ProjectRequest struct { Name *string `json:"name"` } +type ProjectServiceAccount struct { + ID string `json:"id"` + ProjectID string `json:"-"` + Object string `json:"object"` + Name string `json:"name"` + Role string `json:"role"` + CreatedAt int64 `json:"created_at"` + ApiKey *ProjectApiKey `json:"api_key,omitempty"` +} + +type ProjectApiKey struct { + ID string `json:"id"` + Object string `json:"object"` + Value string `json:"value"` + Name *string `json:"name"` + CreatedAt int64 `json:"created_at"` +} + +type ProjectServiceAccounts struct { + Object string `json:"object"` + Data []ProjectServiceAccount `json:"data"` +} + +type ProjectServiceAccountRequest struct { + // The name of the service account being created. + Name *string `json:"name"` +} + // Lists the currently available projects, // and provides basic information about each one. // @@ -88,3 +115,46 @@ func (e *ProjectsEndpoint) ArchiveProject(id string) (*Project, error) { err := e.do(e, "POST", url.QueryEscape(id)+"/archive", nil, nil, &project) return &project, err } + +// Returns a list of service accounts in the project. +// [OpenAI Documentation]: https://platform.openai.com/docs/api-reference/project-service-accounts/list +func (e *ProjectsEndpoint) ListProjectServiceAccounts(projectId string) ([]ProjectServiceAccount, error) { + var projectServiceAccounts ProjectServiceAccounts + err := e.do(e, "GET", url.QueryEscape(projectId)+"/service_accounts", nil, nil, &projectServiceAccounts) + if err == nil && projectServiceAccounts.Object != "list" { + err = fmt.Errorf("expected 'list' object type, got %s", projectServiceAccounts.Object) + } + return projectServiceAccounts.Data, err +} + +// Creates a new service account in the project. This also returns an unredacted API key for the service account. +// [OpenAI Documentation]: https://platform.openai.com/docs/api-reference/project-service-accounts/create +func (e *ProjectsEndpoint) CreateProjectServiceAccount(projectId string, req *ProjectServiceAccountRequest) (*ProjectServiceAccount, error) { + var projectServiceAccount ProjectServiceAccount + err := e.do(e, "POST", url.QueryEscape(projectId)+"/service_accounts", req, nil, &projectServiceAccount) + return &projectServiceAccount, err +} + +// Retrieves a service account in the project. +// [OpenAI Documentation]: https://platform.openai.com/docs/api-reference/project-service-accounts/retrieve +func (e *ProjectsEndpoint) RetrieveProjectServiceAccount(projectId string, id string) (*ProjectServiceAccount, error) { + var projectServiceAccount ProjectServiceAccount + err := e.do(e, "GET", url.QueryEscape(projectId)+"/service_accounts/"+id, nil, nil, &projectServiceAccount) + return &projectServiceAccount, err +} + +// Deletes a service account from the project. +// [OpenAI Documentation]: https://platform.openai.com/docs/api-reference/project-service-accounts/delete +func (e *ProjectsEndpoint) DeleteProjectServiceAccount(projectId string, id string) (bool, error) { + type DeleteResponse struct { + Id string `json:"id"` + Object string `json:"object"` + Deleted bool `json:"deleted"` + } + var resp DeleteResponse + err := e.do(e, "DELETE", url.QueryEscape(projectId)+"/service_accounts/"+id, nil, nil, &resp) + if err != nil { + return false, err + } + return resp.Deleted, nil +}