diff --git a/files.go b/files.go index edc9f2a2..fb7a5056 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 aa4fda45..d8f8dce4 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()