From 584300acb230f50cfff940fab922993f7590a33d Mon Sep 17 00:00:00 2001 From: Austin DeNoble Date: Sun, 16 Jun 2024 13:17:25 -0400 Subject: [PATCH] update authorization flows allowing assistants to share the same auth paths as indexes - login vs. api key should both work for both if provided, get rid of the boolean controlling auth --- .../pkg/assistants/assistant_chat_completions.go | 6 ++---- internal/pkg/assistants/assistant_file_delete.go | 1 - internal/pkg/assistants/assistant_file_get.go | 1 - internal/pkg/assistants/assistant_file_upload.go | 1 - internal/pkg/assistants/assistant_files_list.go | 1 - internal/pkg/assistants/create.go | 1 - internal/pkg/assistants/delete.go | 1 - internal/pkg/assistants/get.go | 1 - internal/pkg/assistants/list.go | 1 - internal/pkg/dashboard/keys_delete.go | 1 - internal/pkg/dashboard/keys_list.go | 2 +- internal/pkg/dashboard/keys_post.go | 2 +- internal/pkg/dashboard/organizations_get.go | 2 +- internal/pkg/dashboard/organizations_list.go | 2 +- internal/pkg/dashboard/project_delete.go | 2 +- internal/pkg/dashboard/projects_create.go | 1 - internal/pkg/utils/network/request.go | 16 ++++++++-------- internal/pkg/utils/network/request_delete.go | 4 ++-- internal/pkg/utils/network/request_get.go | 4 ++-- internal/pkg/utils/network/request_post.go | 8 ++++---- internal/pkg/utils/oauth2/client.go | 4 ++-- internal/pkg/utils/sdk/client.go | 2 +- 22 files changed, 26 insertions(+), 38 deletions(-) diff --git a/internal/pkg/assistants/assistant_chat_completions.go b/internal/pkg/assistants/assistant_chat_completions.go index 2a40269..2da9d02 100644 --- a/internal/pkg/assistants/assistant_chat_completions.go +++ b/internal/pkg/assistants/assistant_chat_completions.go @@ -49,7 +49,6 @@ func GetAssistantChatCompletions(asstName string, msg string, stream bool) (*mod resp, err = network.PostAndDecode[models.ChatCompletionRequest, models.ChatCompletionModel]( assistantDataUrl, fmt.Sprintf(URL_ASSISTANT_CHAT_COMPLETIONS, asstName), - true, body, ) if err != nil { @@ -59,7 +58,6 @@ func GetAssistantChatCompletions(asstName string, msg string, stream bool) (*mod resp, err = PostAndStreamChatResponse[models.ChatCompletionRequest]( assistantDataUrl, fmt.Sprintf(URL_ASSISTANT_CHAT_COMPLETIONS, asstName), - false, body, ) if err != nil { @@ -75,8 +73,8 @@ func GetAssistantChatCompletions(asstName string, msg string, stream bool) (*mod return resp, nil } -func PostAndStreamChatResponse[B any](baseUrl string, path string, useApiKey bool, body B) (*models.ChatCompletionModel, error) { - resp, err := network.RequestWithBody[B](baseUrl, path, http.MethodPost, useApiKey, body) +func PostAndStreamChatResponse[B any](baseUrl string, path string, body B) (*models.ChatCompletionModel, error) { + resp, err := network.RequestWithBody[B](baseUrl, path, http.MethodPost, body) if err != nil { return nil, err } diff --git a/internal/pkg/assistants/assistant_file_delete.go b/internal/pkg/assistants/assistant_file_delete.go index 4e6a1dd..91a9305 100644 --- a/internal/pkg/assistants/assistant_file_delete.go +++ b/internal/pkg/assistants/assistant_file_delete.go @@ -23,7 +23,6 @@ func DeleteAssistantFile(asstName string, fileId string) (*DeleteAssistantFileRe assistantDataUrl, pcio.Sprintf(URL_DELETE_ASSISTANT_FILE, asstName, fileId), http.MethodDelete, - false, ) if err != nil { return nil, err diff --git a/internal/pkg/assistants/assistant_file_get.go b/internal/pkg/assistants/assistant_file_get.go index feea79f..79ba900 100644 --- a/internal/pkg/assistants/assistant_file_get.go +++ b/internal/pkg/assistants/assistant_file_get.go @@ -21,7 +21,6 @@ func DescribeAssistantFile(name string, fileId string) (*AssistantFileModel, err resp, err := network.GetAndDecode[AssistantFileModel]( assistantDataUrl, fmt.Sprintf(URL_DESCRIBE_ASSISTANT_FILE, name, fileId), - false, ) if err != nil { exit.Error(err) diff --git a/internal/pkg/assistants/assistant_file_upload.go b/internal/pkg/assistants/assistant_file_upload.go index b833409..496e7a5 100644 --- a/internal/pkg/assistants/assistant_file_upload.go +++ b/internal/pkg/assistants/assistant_file_upload.go @@ -19,7 +19,6 @@ func UploadAssistantFile(name string, filePath string) (*AssistantFileModel, err resp, err := network.PostMultipartFormDataAndDecode[AssistantFileModel]( assistantDataUrl, fmt.Sprintf(URL_ASSISTANT_FILE_UPLOAD, name), - false, filePath, ) if err != nil { diff --git a/internal/pkg/assistants/assistant_files_list.go b/internal/pkg/assistants/assistant_files_list.go index 1ed74a8..bd5c3fe 100644 --- a/internal/pkg/assistants/assistant_files_list.go +++ b/internal/pkg/assistants/assistant_files_list.go @@ -42,7 +42,6 @@ func ListAssistantFiles(name string) (*ListAssistantFilesResponse, error) { resp, err := network.GetAndDecode[ListAssistantFilesResponse]( assistantDataUrl, fmt.Sprintf(URL_LIST_ASSISTANT_FILES, name), - false, ) if err != nil { return nil, err diff --git a/internal/pkg/assistants/create.go b/internal/pkg/assistants/create.go index 1dae687..807d773 100644 --- a/internal/pkg/assistants/create.go +++ b/internal/pkg/assistants/create.go @@ -26,7 +26,6 @@ func CreateAssistant(name string) (*AssistantModel, error) { resp, err := network.PostAndDecode[CreateAssistantRequest, AssistantModel]( assistantControlUrl, URL_CREATE_ASSISTANT, - false, body, ) if err != nil { diff --git a/internal/pkg/assistants/delete.go b/internal/pkg/assistants/delete.go index fff03bd..fb00fbc 100644 --- a/internal/pkg/assistants/delete.go +++ b/internal/pkg/assistants/delete.go @@ -26,7 +26,6 @@ func DeleteAssistant(name string) (*DeleteAssistantResponse, error) { assistantControlUrl, pcio.Sprintf(URL_DELETE_ASSISTANT, name), http.MethodDelete, - false, ) if err != nil { return nil, err diff --git a/internal/pkg/assistants/get.go b/internal/pkg/assistants/get.go index cb91860..e3ff668 100644 --- a/internal/pkg/assistants/get.go +++ b/internal/pkg/assistants/get.go @@ -20,7 +20,6 @@ func DescribeAssistant(name string) (*AssistantModel, error) { resp, err := network.GetAndDecode[AssistantModel]( assistantControlUrl, fmt.Sprintf(URL_DESCRIBE_ASSISTANT, name), - false, ) if err != nil { return nil, err diff --git a/internal/pkg/assistants/list.go b/internal/pkg/assistants/list.go index 6dee1c6..cea4097 100644 --- a/internal/pkg/assistants/list.go +++ b/internal/pkg/assistants/list.go @@ -51,7 +51,6 @@ func ListAssistants() (*ListAssistantsResponse, error) { resp, err := network.GetAndDecode[ListAssistantsResponse]( assistantControlUrl, URL_LIST_ASSISTANTS, - false, ) if err != nil { return nil, err diff --git a/internal/pkg/dashboard/keys_delete.go b/internal/pkg/dashboard/keys_delete.go index 09c43f7..9db3089 100644 --- a/internal/pkg/dashboard/keys_delete.go +++ b/internal/pkg/dashboard/keys_delete.go @@ -36,7 +36,6 @@ func DeleteApiKey(projId string, key Key) (*DeleteApiKeyResponse, error) { dashboardUrl, path, http.MethodDelete, - false, body, ) if err != nil { diff --git a/internal/pkg/dashboard/keys_list.go b/internal/pkg/dashboard/keys_list.go index 019e26f..18cd0c1 100644 --- a/internal/pkg/dashboard/keys_list.go +++ b/internal/pkg/dashboard/keys_list.go @@ -31,5 +31,5 @@ func GetApiKeysById(projectId string) (*KeyResponse, error) { } url := pcio.Sprintf(URL_GET_API_KEYS, projectId) - return network.GetAndDecode[KeyResponse](dashboardUrl, url, false) + return network.GetAndDecode[KeyResponse](dashboardUrl, url) } diff --git a/internal/pkg/dashboard/keys_post.go b/internal/pkg/dashboard/keys_post.go index 280221a..a38d226 100644 --- a/internal/pkg/dashboard/keys_post.go +++ b/internal/pkg/dashboard/keys_post.go @@ -28,7 +28,7 @@ func CreateApiKey(projId string, keyName string) (*CreateApiKeyResponse, error) return nil, err } - resp, err := network.PostAndDecode[CreateApiKeyRequest, CreateApiKeyResponse](dashboardUrl, path, false, body) + resp, err := network.PostAndDecode[CreateApiKeyRequest, CreateApiKeyResponse](dashboardUrl, path, body) if err != nil { return nil, err } diff --git a/internal/pkg/dashboard/organizations_get.go b/internal/pkg/dashboard/organizations_get.go index b03ae2c..c359b6c 100644 --- a/internal/pkg/dashboard/organizations_get.go +++ b/internal/pkg/dashboard/organizations_get.go @@ -28,7 +28,7 @@ func DescribeOrganization(orgId string) (*DescribeOrganizationResponse, error) { if err != nil { return nil, err } - resp, err := network.GetAndDecode[DescribeOrganizationResponse](dashboardUrl, path, false) + resp, err := network.GetAndDecode[DescribeOrganizationResponse](dashboardUrl, path) if err != nil { return nil, err } diff --git a/internal/pkg/dashboard/organizations_list.go b/internal/pkg/dashboard/organizations_list.go index 3adde72..e297c21 100644 --- a/internal/pkg/dashboard/organizations_list.go +++ b/internal/pkg/dashboard/organizations_list.go @@ -38,7 +38,7 @@ func ListOrganizations() (*OrganizationsResponse, error) { return nil, err } - resp, err := network.GetAndDecode[OrganizationsResponse](dashboardUrl, URL_LIST_ORGANIZATIONS, false) + resp, err := network.GetAndDecode[OrganizationsResponse](dashboardUrl, URL_LIST_ORGANIZATIONS) if err != nil { return nil, err } diff --git a/internal/pkg/dashboard/project_delete.go b/internal/pkg/dashboard/project_delete.go index 46306c9..cac2f24 100644 --- a/internal/pkg/dashboard/project_delete.go +++ b/internal/pkg/dashboard/project_delete.go @@ -20,7 +20,7 @@ func DeleteProject(orgId string, projId string) (*DeletePostResponse, error) { return nil, err } - resp, err := network.DeleteAndDecode[DeletePostResponse](dashboardUrl, path, false) + resp, err := network.DeleteAndDecode[DeletePostResponse](dashboardUrl, path) if err != nil { return nil, err } diff --git a/internal/pkg/dashboard/projects_create.go b/internal/pkg/dashboard/projects_create.go index 0b0911c..5821bc4 100644 --- a/internal/pkg/dashboard/projects_create.go +++ b/internal/pkg/dashboard/projects_create.go @@ -36,7 +36,6 @@ func CreateProject(orgId string, projName string, podQuota int32) (*CreateProjec resp, err := network.PostAndDecode[CreateProjectRequest, CreateProjectResponse]( dashboardUrl, path, - false, body, ) if err != nil { diff --git a/internal/pkg/utils/network/request.go b/internal/pkg/utils/network/request.go index 7cbadc1..0f11e71 100644 --- a/internal/pkg/utils/network/request.go +++ b/internal/pkg/utils/network/request.go @@ -48,11 +48,11 @@ func buildRequest(verb string, path string, bodyJson []byte) (*http.Request, err return req, nil } -func performRequest(req *http.Request, useApiKey bool) (*http.Response, error) { +func performRequest(req *http.Request) (*http.Response, error) { // This http client is built using our oauth configurations // and is already configured with our access token ctx := context.Background() - client, err := oauth2.GetHttpClient(ctx, useApiKey) + client, err := oauth2.GetHttpClient(ctx) if err != nil { return nil, err } @@ -88,7 +88,7 @@ func decodeResponse[T any](resp *http.Response, target *T) error { return nil } -func RequestWithBody[B any](baseUrl string, path string, method string, useApiKey bool, body B) (*http.Response, error) { +func RequestWithBody[B any](baseUrl string, path string, method string, body B) (*http.Response, error) { url := baseUrl + path var bodyJson []byte @@ -116,7 +116,7 @@ func RequestWithBody[B any](baseUrl string, path string, method string, useApiKe return nil, pcio.Errorf("error building request: %v", err) } - resp, err := performRequest(req, useApiKey) + resp, err := performRequest(req) if err != nil { log.Error(). Err(err). @@ -127,7 +127,7 @@ func RequestWithBody[B any](baseUrl string, path string, method string, useApiKe return resp, nil } -func RequestWithBodyAndDecode[B any, R any](baseUrl string, path string, method string, useApiKey bool, body B) (*R, error) { +func RequestWithBodyAndDecode[B any, R any](baseUrl string, path string, method string, body B) (*R, error) { url := baseUrl + path var bodyJson []byte @@ -155,7 +155,7 @@ func RequestWithBodyAndDecode[B any, R any](baseUrl string, path string, method return nil, pcio.Errorf("error building request: %v", err) } - resp, err := performRequest(req, useApiKey) + resp, err := performRequest(req) if err != nil { log.Error(). Err(err). @@ -183,7 +183,7 @@ func RequestWithBodyAndDecode[B any, R any](baseUrl string, path string, method return &parsedResponse, nil } -func RequestWithoutBodyAndDecode[T any](baseUrl string, path string, method string, useApiKey bool) (*T, error) { +func RequestWithoutBodyAndDecode[T any](baseUrl string, path string, method string) (*T, error) { url := baseUrl + path requestedService := "assistant engine" @@ -205,7 +205,7 @@ func RequestWithoutBodyAndDecode[T any](baseUrl string, path string, method stri return nil, pcio.Errorf("error building request: %v", err) } - resp, err := performRequest(req, useApiKey) + resp, err := performRequest(req) if err != nil { log.Error(). Err(err). diff --git a/internal/pkg/utils/network/request_delete.go b/internal/pkg/utils/network/request_delete.go index 8ed2127..fdbab69 100644 --- a/internal/pkg/utils/network/request_delete.go +++ b/internal/pkg/utils/network/request_delete.go @@ -4,6 +4,6 @@ import ( "net/http" ) -func DeleteAndDecode[T any](baseUrl string, path string, useApiKey bool) (*T, error) { - return RequestWithoutBodyAndDecode[T](baseUrl, path, http.MethodDelete, useApiKey) +func DeleteAndDecode[T any](baseUrl string, path string) (*T, error) { + return RequestWithoutBodyAndDecode[T](baseUrl, path, http.MethodDelete) } diff --git a/internal/pkg/utils/network/request_get.go b/internal/pkg/utils/network/request_get.go index 79e0569..cc8fc85 100644 --- a/internal/pkg/utils/network/request_get.go +++ b/internal/pkg/utils/network/request_get.go @@ -4,6 +4,6 @@ import ( "net/http" ) -func GetAndDecode[T any](baseUrl string, path string, useApiKey bool) (*T, error) { - return RequestWithoutBodyAndDecode[T](baseUrl, path, http.MethodGet, useApiKey) +func GetAndDecode[T any](baseUrl string, path string) (*T, error) { + return RequestWithoutBodyAndDecode[T](baseUrl, path, http.MethodGet) } diff --git a/internal/pkg/utils/network/request_post.go b/internal/pkg/utils/network/request_post.go index 220aeed..b38975b 100644 --- a/internal/pkg/utils/network/request_post.go +++ b/internal/pkg/utils/network/request_post.go @@ -12,11 +12,11 @@ import ( "github.com/pinecone-io/cli/internal/pkg/utils/pcio" ) -func PostAndDecode[B any, R any](baseUrl string, path string, useApiKey bool, body B) (*R, error) { - return RequestWithBodyAndDecode[B, R](baseUrl, path, http.MethodPost, useApiKey, body) +func PostAndDecode[B any, R any](baseUrl string, path string, body B) (*R, error) { + return RequestWithBodyAndDecode[B, R](baseUrl, path, http.MethodPost, body) } -func PostMultipartFormDataAndDecode[R any](baseUrl string, path string, useApiKey bool, bodyPath string) (*R, error) { +func PostMultipartFormDataAndDecode[R any](baseUrl string, path string, bodyPath string) (*R, error) { url := baseUrl + path var requestBody bytes.Buffer @@ -55,7 +55,7 @@ func PostMultipartFormDataAndDecode[R any](baseUrl string, path string, useApiKe Str("multipart/form-data", bodyPath). Msg("Sending multipart/form-data request") - resp, err := performRequest(req, useApiKey) + resp, err := performRequest(req) if err != nil { log.Error(). Err(err). diff --git a/internal/pkg/utils/oauth2/client.go b/internal/pkg/utils/oauth2/client.go index 3654d29..7e006cf 100644 --- a/internal/pkg/utils/oauth2/client.go +++ b/internal/pkg/utils/oauth2/client.go @@ -18,10 +18,10 @@ func (akt *apiKeyTransport) RoundTrip(req *http.Request) (*http.Response, error) return akt.next.RoundTrip(req) } -func GetHttpClient(ctx context.Context, useApiKey bool) (*http.Client, error) { +func GetHttpClient(ctx context.Context) (*http.Client, error) { token := secrets.OAuth2Token.Get() - if token.AccessToken != "" && !useApiKey { + if token.AccessToken != "" { log.Debug().Msg("Creating http client with OAuth2 token handling") config, err := newOauth2Config() if err != nil { diff --git a/internal/pkg/utils/sdk/client.go b/internal/pkg/utils/sdk/client.go index 3eb56cd..350dc38 100644 --- a/internal/pkg/utils/sdk/client.go +++ b/internal/pkg/utils/sdk/client.go @@ -82,7 +82,7 @@ func NewPineconeClientForUser(projectId string) *pinecone.Client { headers["X-Project-Id"] = projectId ctx := context.Background() - restClient, err := pc_oauth2.GetHttpClient(ctx, false) + restClient, err := pc_oauth2.GetHttpClient(ctx) if err != nil { msg.FailMsg("Failed to create OAuth2 client: %s", err) exit.Error(err)