Skip to content

Commit

Permalink
update authorization flows allowing assistants to share the same auth…
Browse files Browse the repository at this point in the history
… paths as indexes - login vs. api key should both work for both if provided, get rid of the boolean controlling auth
  • Loading branch information
austin-denoble committed Jun 16, 2024
1 parent 3898d7f commit 584300a
Show file tree
Hide file tree
Showing 22 changed files with 26 additions and 38 deletions.
6 changes: 2 additions & 4 deletions internal/pkg/assistants/assistant_chat_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/assistants/assistant_file_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/assistants/assistant_file_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/assistants/assistant_file_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/assistants/assistant_files_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/assistants/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/assistants/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/assistants/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/assistants/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func ListAssistants() (*ListAssistantsResponse, error) {
resp, err := network.GetAndDecode[ListAssistantsResponse](
assistantControlUrl,
URL_LIST_ASSISTANTS,
false,
)
if err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/dashboard/keys_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func DeleteApiKey(projId string, key Key) (*DeleteApiKeyResponse, error) {
dashboardUrl,
path,
http.MethodDelete,
false,
body,
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/dashboard/keys_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion internal/pkg/dashboard/keys_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/dashboard/organizations_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/dashboard/organizations_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/dashboard/project_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion internal/pkg/dashboard/projects_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions internal/pkg/utils/network/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand All @@ -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
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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"
Expand All @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/utils/network/request_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions internal/pkg/utils/network/request_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions internal/pkg/utils/network/request_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/utils/oauth2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/utils/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 584300a

Please sign in to comment.