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

Redesign update API to task API #262

Merged
merged 4 commits into from
Feb 1, 2022
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
77 changes: 75 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 @@ -21,15 +23,20 @@ type ClientConfig struct {
Timeout time.Duration
}

type waitParams struct {
Context context.Context
Interval time.Duration
}

// ClientInterface is interface for all Meilisearch client
type ClientInterface interface {
Index(uid string) *Index
GetIndex(indexID string) (resp *Index, err error)
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 +45,9 @@ 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(task *Task, options ...waitParams) (*Task, error)
}

var _ ClientInterface = &Client{}
Expand Down Expand Up @@ -170,3 +180,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
}

// WaitForTask waits for a task to be processed.
// The function will check by regular interval provided in parameter interval
// the TaskStatus.
// If no ctx and interval are provided WaitForTask will check each 50ms the
// status of a task.
func (c *Client) WaitForTask(task *Task, options ...waitParams) (*Task, error) {
if options == nil {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFunc()
options = []waitParams{
{
Context: ctx,
Interval: time.Millisecond * 50,
},
}
}
for {
if err := options[0].Context.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(options[0].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