Skip to content

Commit

Permalink
Redesign update API to task API
Browse files Browse the repository at this point in the history
  • Loading branch information
alallema committed Jan 27, 2022
1 parent 081174d commit fcdcc9b
Show file tree
Hide file tree
Showing 15 changed files with 1,770 additions and 1,264 deletions.
73 changes: 71 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package meilisearch

import (
"context"
"net/http"
"strconv"
"time"

"github.com/valyala/fasthttp"
Expand All @@ -28,8 +30,8 @@ type ClientInterface interface {
GetRawIndex(uid string) (resp map[string]interface{}, err error)
GetAllIndexes() (resp []*Index, err error)
GetAllRawIndexes() (resp []map[string]interface{}, err error)
CreateIndex(config *IndexConfig) (resp *Index, err error)
DeleteIndex(uid string) (bool, error)
CreateIndex(config *IndexConfig) (resp *Task, err error)
DeleteIndex(uid string) (resp *Task, err error)
GetKeys() (resp *Keys, err error)
GetAllStats() (resp *Stats, err error)
CreateDump() (resp *Dump, err error)
Expand All @@ -38,6 +40,10 @@ type ClientInterface interface {
GetVersion() (resp *Version, err error)
Health() (*Health, error)
IsHealthy() bool
GetTask(taskID int64) (resp *Task, err error)
GetTasks() (resp *ResultTask, err error)
WaitForTask(ctx context.Context, interval time.Duration, taskID *Task) (*Task, error)
DefaultWaitForTask(taskID *Task) (*Task, error)
}

var _ ClientInterface = &Client{}
Expand Down Expand Up @@ -170,3 +176,66 @@ func (c *Client) GetDumpStatus(dumpUID string) (resp *Dump, err error) {
}
return resp, nil
}

func (c *Client) GetTask(taskID int64) (resp *Task, err error) {
resp = &Task{}
req := internalRequest{
endpoint: "/tasks/" + strconv.FormatInt(taskID, 10),
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTask",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (c *Client) GetTasks() (resp *ResultTask, err error) {
resp = &ResultTask{}
req := internalRequest{
endpoint: "/tasks",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTasks",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

// // DefaultWaitForTask checks each 50ms the status of a task.
// // This is a default implementation of WaitForTask.
func (c *Client) DefaultWaitForTask(task *Task) (*Task, error) {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFunc()
return c.WaitForTask(ctx, time.Millisecond*50, task)
}

// WaitForTask waits for a task to be processed.
// The function will check by regular interval provided in parameter interval
// the TaskStatus. If it is not TaskStatusEnqueued or the ctx cancelled
// we return the TaskStatus.
func (c *Client) WaitForTask(
ctx context.Context,
interval time.Duration,
task *Task) (*Task, error) {
for {
if err := ctx.Err(); err != nil {
return nil, err
}
getTask, err := c.GetTask(task.UID)
if err != nil {
return nil, err
}
if getTask.Status != TaskStatusEnqueued && getTask.Status != TaskStatusProcessing {
return getTask, nil
}
time.Sleep(interval)
}
}
22 changes: 12 additions & 10 deletions client_index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package meilisearch

import "net/http"
import (
"net/http"
)

func (c *Client) Index(uid string) *Index {
return newIndex(c, uid)
Expand All @@ -26,19 +28,19 @@ func (c *Client) GetRawIndex(uid string) (resp map[string]interface{}, err error
return resp, nil
}

func (c *Client) CreateIndex(config *IndexConfig) (resp *Index, err error) {
func (c *Client) CreateIndex(config *IndexConfig) (resp *Task, err error) {
request := &CreateIndexRequest{
UID: config.Uid,
PrimaryKey: config.PrimaryKey,
}
resp = newIndex(c, config.Uid)
resp = &Task{}
req := internalRequest{
endpoint: "/indexes",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusCreated},
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "CreateIndex",
}
if err := c.executeRequest(req); err != nil {
Expand Down Expand Up @@ -79,18 +81,18 @@ func (c *Client) GetAllRawIndexes() (resp []map[string]interface{}, err error) {
return resp, nil
}

func (c *Client) DeleteIndex(uid string) (ok bool, err error) {
func (c *Client) DeleteIndex(uid string) (resp *Task, err error) {
resp = &Task{}
req := internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodDelete,
withRequest: nil,
withResponse: nil,
acceptedStatusCodes: []int{http.StatusNoContent},
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "DeleteIndex",
}
// err is not nil if status code is not 204 StatusNoContent
if err := c.executeRequest(req); err != nil {
return false, err
return nil, err
}
return true, nil
return resp, nil
}
Loading

0 comments on commit fcdcc9b

Please sign in to comment.