Skip to content

Commit

Permalink
Add pagination options to ListFiles request
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
rliddler committed Dec 30, 2024
1 parent 2a0ff5a commit a7be8b8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
34 changes: 32 additions & 2 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"os"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions files_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a7be8b8

Please sign in to comment.