Skip to content

Commit

Permalink
Merge pull request #396 from meilisearch/add_delete_api_for_v0.30.0
Browse files Browse the repository at this point in the history
Add delete task api for v0.30.0
  • Loading branch information
alallema authored Dec 14, 2022
2 parents d629950 + c41800e commit d6bce8a
Show file tree
Hide file tree
Showing 4 changed files with 528 additions and 42 deletions.
34 changes: 34 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ClientInterface interface {
GetTask(taskUID int64) (resp *Task, err error)
GetTasks(param *TasksQuery) (resp *TaskResult, err error)
CancelTasks(param *CancelTasksQuery) (resp *TaskInfo, err error)
DeleteTasks(param *DeleteTasksQuery) (resp *TaskInfo, err error)
WaitForTask(taskUID int64, options ...WaitParams) (*Task, error)
GenerateTenantToken(APIKeyUID string, searchRules map[string]interface{}, options *TenantTokenOptions) (resp string, err error)
}
Expand Down Expand Up @@ -316,6 +317,39 @@ func (c *Client) CancelTasks(param *CancelTasksQuery) (resp *TaskInfo, err error
return resp, nil
}

func (c *Client) DeleteTasks(param *DeleteTasksQuery) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/tasks",
method: http.MethodDelete,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "DeleteTasks",
}
if param != nil {
paramToSend := &TasksQuery{
UIDS: param.UIDS,
IndexUIDS: param.IndexUIDS,
Statuses: param.Statuses,
Types: param.Types,
CanceledBy: param.CanceledBy,
BeforeEnqueuedAt: param.BeforeEnqueuedAt,
AfterEnqueuedAt: param.AfterEnqueuedAt,
BeforeStartedAt: param.BeforeStartedAt,
AfterStartedAt: param.AfterStartedAt,
BeforeFinishedAt: param.BeforeFinishedAt,
AfterFinishedAt: param.AfterFinishedAt,
}
encodeTasksQuery(paramToSend, &req)
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

// WaitForTask waits for a task to be processed
//
// The function will check by regular interval provided in parameter interval
Expand Down
129 changes: 129 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,135 @@ func TestClient_CancelTasks(t *testing.T) {
}
}

func TestClient_DeleteTasks(t *testing.T) {
type args struct {
UID string
client *Client
query *DeleteTasksQuery
}
tests := []struct {
name string
args args
want string
}{
{
name: "TestBasicDeleteTasks",
args: args{
UID: "indexUID",
client: defaultClient,
query: &DeleteTasksQuery{
Statuses: []string{"enqueued"},
},
},
want: "?statuses=enqueued",
},
{
name: "TestDeleteTasksWithUidFilter",
args: args{
UID: "indexUID",
client: defaultClient,
query: &DeleteTasksQuery{
UIDS: []int64{1},
},
},
want: "?uids=1",
},
{
name: "TestDeleteTasksWithMultipleUidsFilter",
args: args{
UID: "indexUID",
client: defaultClient,
query: &DeleteTasksQuery{
UIDS: []int64{0, 1},
},
},
want: "?uids=0%2C1",
},
{
name: "TestDeleteTasksWithIndexUidFilter",
args: args{
UID: "indexUID",
client: defaultClient,
query: &DeleteTasksQuery{
IndexUIDS: []string{"0"},
},
},
want: "?indexUids=0",
},
{
name: "TestDeleteTasksWithMultipleIndexUidsFilter",
args: args{
UID: "indexUID",
client: defaultClient,
query: &DeleteTasksQuery{
IndexUIDS: []string{"0", "1"},
},
},
want: "?indexUids=0%2C1",
},
{
name: "TestDeleteTasksWithDateFilter",
args: args{
UID: "indexUID",
client: defaultClient,
query: &DeleteTasksQuery{
BeforeEnqueuedAt: time.Now(),
},
},
want: strings.NewReplacer(":", "%3A").Replace("?beforeEnqueuedAt=" + time.Now().Format("2006-01-02T15:04:05Z")),
},
{
name: "TestDeleteTasksWithCanceledByFilter",
args: args{
UID: "indexUID",
client: defaultClient,
query: &DeleteTasksQuery{
CanceledBy: []int64{1},
},
},
want: "?canceledBy=1",
},
{
name: "TestDeleteTasksWithParameters",
args: args{
UID: "indexUID",
client: defaultClient,
query: &DeleteTasksQuery{
Statuses: []string{"enqueued"},
IndexUIDS: []string{"indexUID"},
UIDS: []int64{1},
AfterEnqueuedAt: time.Now(),
},
},
want: "?afterEnqueuedAt=" + strings.NewReplacer(":", "%3A").Replace(time.Now().Format("2006-01-02T15:04:05Z")) + "&indexUids=indexUID&statuses=enqueued&uids=1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := tt.args.client
t.Cleanup(cleanup(c))

gotResp, err := c.DeleteTasks(tt.args.query)
require.NoError(t, err)

_, err = c.WaitForTask(gotResp.TaskUID)
require.NoError(t, err)

gotTask, err := c.GetTask(gotResp.TaskUID)
require.NoError(t, err)

require.NotNil(t, gotResp.Status)
require.NotNil(t, gotResp.Type)
require.NotNil(t, gotResp.TaskUID)
require.NotNil(t, gotResp.EnqueuedAt)
require.Equal(t, "", gotResp.IndexUID)
require.Equal(t, "taskDeletion", gotResp.Type)
require.NotNil(t, gotTask.Details.OriginalFilter)
require.Equal(t, tt.want, gotTask.Details.OriginalFilter)
})
}
}

func TestClient_DefaultWaitForTask(t *testing.T) {
type args struct {
UID string
Expand Down
15 changes: 15 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ type CancelTasksQuery struct {
AfterStartedAt time.Time
}

// DeleteTasksQuery is a list of filter available to send as query parameters
type DeleteTasksQuery struct {
UIDS []int64
IndexUIDS []string
Statuses []string
Types []string
CanceledBy []int64
BeforeEnqueuedAt time.Time
AfterEnqueuedAt time.Time
BeforeStartedAt time.Time
AfterStartedAt time.Time
BeforeFinishedAt time.Time
AfterFinishedAt time.Time
}

type Details struct {
ReceivedDocuments int64 `json:"receivedDocuments,omitempty"`
IndexedDocuments int64 `json:"indexedDocuments,omitempty"`
Expand Down
Loading

0 comments on commit d6bce8a

Please sign in to comment.