Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading