Skip to content

Commit a7be8b8

Browse files
committed
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!
1 parent 2a0ff5a commit a7be8b8

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

files.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"net/http"
8+
"net/url"
89
"os"
910
)
1011

@@ -51,7 +52,10 @@ type File struct {
5152

5253
// FilesList is a list of files that belong to the user or organization.
5354
type FilesList struct {
54-
Files []File `json:"data"`
55+
Files []File `json:"data"`
56+
LastID *string `json:"last_id"`
57+
FirstID *string `json:"first_id"`
58+
HasMore bool `json:"has_more"`
5559

5660
httpHeader
5761
}
@@ -138,7 +142,33 @@ func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) {
138142
// ListFiles Lists the currently available files,
139143
// and provides basic information about each file such as the file name and purpose.
140144
func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) {
141-
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/files"))
145+
return c.ListFilesWithOpts(ctx, nil, nil, nil)
146+
}
147+
148+
func (c *Client) ListFilesWithOpts(
149+
ctx context.Context,
150+
limit *int,
151+
order *string,
152+
after *string,
153+
) (files FilesList, err error) {
154+
urlValues := url.Values{}
155+
if limit != nil {
156+
urlValues.Add("limit", fmt.Sprintf("%d", *limit))
157+
}
158+
if order != nil {
159+
urlValues.Add("order", *order)
160+
}
161+
if after != nil {
162+
urlValues.Add("after", *after)
163+
}
164+
165+
encodedValues := ""
166+
if len(urlValues) > 0 {
167+
encodedValues = "?" + urlValues.Encode()
168+
}
169+
170+
urlSuffix := fmt.Sprintf("%s%s", "/files", encodedValues)
171+
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
142172
if err != nil {
143173
return
144174
}

files_api_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ func TestListFile(t *testing.T) {
102102
checks.NoError(t, err, "ListFiles error")
103103
}
104104

105+
func TestListFileWithOpts(t *testing.T) {
106+
limit := 10
107+
order := "desc"
108+
afterID := "deadbeef"
109+
110+
client, server, teardown := setupOpenAITestServer()
111+
defer teardown()
112+
server.RegisterHandler("/v1/files", func(w http.ResponseWriter, _ *http.Request) {
113+
resBytes, _ := json.Marshal(openai.FilesList{})
114+
fmt.Fprintln(w, string(resBytes))
115+
})
116+
_, err := client.ListFilesWithOpts(context.Background(), &limit, &order, &afterID)
117+
checks.NoError(t, err, "ListFiles error")
118+
}
119+
105120
func TestGetFile(t *testing.T) {
106121
client, server, teardown := setupOpenAITestServer()
107122
defer teardown()

0 commit comments

Comments
 (0)