From a7be8b82d7680a542a06f696735dd7d6a5da189c Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 30 Dec 2024 13:16:45 +0000 Subject: [PATCH] Add pagination options to ListFiles request List files follows the same pattern as the other endpoints and accepts a limit, start id, and order. It returns has_more if there are more entries along with the start and end id. This doesn't seem to be in the current documentation here: https://platform.openai.com/docs/api-reference/files/list However this is the behaviour and it matches other endpoints. Without this change this client doesn't let you paginate through your files, which we would love to do! --- files.go | 34 ++++++++++++++++++++++++++++++++-- files_api_test.go | 15 +++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/files.go b/files.go index edc9f2a20..fb7a5056d 100644 --- a/files.go +++ b/files.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "net/http" + "net/url" "os" ) @@ -51,7 +52,10 @@ type File struct { // FilesList is a list of files that belong to the user or organization. type FilesList struct { - Files []File `json:"data"` + Files []File `json:"data"` + LastID *string `json:"last_id"` + FirstID *string `json:"first_id"` + HasMore bool `json:"has_more"` httpHeader } @@ -138,7 +142,33 @@ func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) { // ListFiles Lists the currently available files, // and provides basic information about each file such as the file name and purpose. func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) { - req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/files")) + return c.ListFilesWithOpts(ctx, nil, nil, nil) +} + +func (c *Client) ListFilesWithOpts( + ctx context.Context, + limit *int, + order *string, + after *string, +) (files FilesList, err error) { + urlValues := url.Values{} + if limit != nil { + urlValues.Add("limit", fmt.Sprintf("%d", *limit)) + } + if order != nil { + urlValues.Add("order", *order) + } + if after != nil { + urlValues.Add("after", *after) + } + + encodedValues := "" + if len(urlValues) > 0 { + encodedValues = "?" + urlValues.Encode() + } + + urlSuffix := fmt.Sprintf("%s%s", "/files", encodedValues) + req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix)) if err != nil { return } diff --git a/files_api_test.go b/files_api_test.go index aa4fda458..d8f8dce42 100644 --- a/files_api_test.go +++ b/files_api_test.go @@ -102,6 +102,21 @@ func TestListFile(t *testing.T) { checks.NoError(t, err, "ListFiles error") } +func TestListFileWithOpts(t *testing.T) { + limit := 10 + order := "desc" + afterID := "deadbeef" + + client, server, teardown := setupOpenAITestServer() + defer teardown() + server.RegisterHandler("/v1/files", func(w http.ResponseWriter, _ *http.Request) { + resBytes, _ := json.Marshal(openai.FilesList{}) + fmt.Fprintln(w, string(resBytes)) + }) + _, err := client.ListFilesWithOpts(context.Background(), &limit, &order, &afterID) + checks.NoError(t, err, "ListFiles error") +} + func TestGetFile(t *testing.T) { client, server, teardown := setupOpenAITestServer() defer teardown()