Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: batch delete files #6

Merged
merged 1 commit into from
Apr 22, 2024
Merged
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
20 changes: 12 additions & 8 deletions api_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,24 @@
return deleteResponse, nil
}

type FilesDeleteAllResponse struct {
type FilesBatchDeleteRequest struct {
FileIDList []string `json:"file_ids"`
}
type FilesBatchDeleteResponse struct {
RespList []*FilesDeleteResponse `json:"resp_list"`
}

func (f *files) DeleteAll() (*FilesDeleteAllResponse, error) {
listResp, err := f.Lists()
if err != nil {
return nil, err
func (f *files) BatchDelete(req *FilesBatchDeleteRequest) (*FilesBatchDeleteResponse, error) {
if req == nil || len(req.FileIDList) == 0 {
return nil, fmt.Errorf("batch delete request must contain at least one file id")

Check warning on line 211 in api_files.go

View check run for this annotation

Codecov / codecov/patch

api_files.go#L211

Added line #L211 was not covered by tests
}

deleteAllResp := new(FilesDeleteAllResponse)
deleteAllResp := &FilesBatchDeleteResponse{
RespList: make([]*FilesDeleteResponse, 0),
}

for _, data := range listResp.Data {
deleteResp, err := f.Delete(data.ID)
for _, fileID := range req.FileIDList {
deleteResp, err := f.Delete(fileID)
if err != nil {
return nil, err
}
Expand Down
32 changes: 17 additions & 15 deletions api_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,48 +132,50 @@ func TestFilesDelete(t *testing.T) {
t.Logf("%+v", test.MarshalJsonToStringX(resp))
}

/*
❗ This test may lead to unexpected results
*/
func TestFilesDeleteAll(t *testing.T) {

func TestFilesBatchDelete(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}

fileIdList := make([]string, 0)

for i := 0; i < 10; i++ {
fp, _ := test.GenerateTestFile(test.GenerateTestContent())
_, _ = cli.Files().Upload(&moonshot.FilesUploadRequest{
uploadResp, err := cli.Files().Upload(&moonshot.FilesUploadRequest{
Name: filepath.Base(fp),
Path: fp,
Purpose: moonshot.FilePurposeExtract,
})
if err != nil {
t.Logf("upload file err: %v", err)
continue
}
fileIdList = append(fileIdList, uploadResp.ID)
}

listResp, err := cli.Files().Lists()
if err != nil {
t.Fatal(err)
}
t.Logf("file id to delete: %v", fileIdList)

deleteAllResp, err := cli.Files().DeleteAll()
deleteAllResp, err := cli.Files().BatchDelete(&moonshot.FilesBatchDeleteRequest{
FileIDList: fileIdList,
})
if err != nil {
t.Fatal(err)
}

require.EqualValues(t, func(in []*moonshot.FilesListResponseData) (ls []string) {
require.EqualValues(t, func(in []string) (ls []string) {
for _, v := range in {
ls = append(ls, v.ID)
ls = append(ls, v)
}
return
}(listResp.Data), func(in []*moonshot.FilesDeleteResponse) (ls []string) {
}(fileIdList), func(in []*moonshot.FilesDeleteResponse) (ls []string) {
for _, resp := range in {
ls = append(ls, resp.ID)
}
return
}(deleteAllResp.RespList), "must delete all files")

t.Logf("Deleted Files ID List: %v", func(ls []*moonshot.FilesDeleteResponse) (l []string) {
t.Logf("deleted Files ID List: %v", func(ls []*moonshot.FilesDeleteResponse) (l []string) {
for _, v := range ls {
l = append(l, v.ID)
}
Expand Down
Loading