Skip to content

Commit

Permalink
add support for project service accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
skyscrapr committed Aug 27, 2024
1 parent 01a4291 commit c2c2413
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion openai/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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.
//
Expand Down Expand Up @@ -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
}

0 comments on commit c2c2413

Please sign in to comment.