Skip to content

Commit

Permalink
Merge #382
Browse files Browse the repository at this point in the history
382: Changes related to the next Meilisearch release (v0.30.0) r=alallema a=meili-bot

Related to this issue: meilisearch/integration-guides#221

This PR:
- gathers the changes related to the next Meilisearch release (v0.30.0) so that this package is ready when the official release is out.
- should pass the tests against the [latest pre-release of Meilisearch](https://github.com/meilisearch/meilisearch/releases).
- might eventually contain test failures until the Meilisearch v0.30.0 is out.

⚠️ This PR should NOT be merged until the next release of Meilisearch (v0.30.0) is out.

_This PR is auto-generated for the [pre-release week](https://github.com/meilisearch/integration-guides/blob/main/resources/pre-release-week.md) purpose._

Done:
- #390
- #392
- #395
- #396
- #397

Co-authored-by: meili-bot <[email protected]>
Co-authored-by: Amélie <[email protected]>
Co-authored-by: alallema <[email protected]>
  • Loading branch information
4 people authored Dec 14, 2022
2 parents fc4a073 + 0345f62 commit 9d80462
Show file tree
Hide file tree
Showing 10 changed files with 2,319 additions and 719 deletions.
2 changes: 1 addition & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ get_all_tasks_filtering_1: |-
client.Index("movies").GetTasks(nil);
// OR
client.GetTasks(&TasksQuery{
IndexUID: []string{"movies"},
IndexUIDs: []string{"movies"},
});
get_all_tasks_filtering_2: |-
client.GetTasks(&meilisearch.TasksQuery{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ searchRes, err := index.Search("wonder",

## 🤖 Compatibility with Meilisearch

This package only guarantees the compatibility with the [version v0.29.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.29.0).
This package only guarantees the compatibility with the [version v0.30.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.30.0).

## 💡 Learn more

Expand Down
147 changes: 131 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type ClientInterface interface {
IsHealthy() bool
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)
SwapIndexes(param []SwapIndexesParams) (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 @@ -277,21 +280,87 @@ func (c *Client) GetTasks(param *TasksQuery) (resp *TaskResult, err error) {
functionName: "GetTasks",
}
if param != nil {
if param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param.From != 0 {
req.withQueryParams["from"] = strconv.FormatInt(param.From, 10)
}
if len(param.Status) != 0 {
req.withQueryParams["status"] = strings.Join(param.Status, ",")
}
if len(param.Type) != 0 {
req.withQueryParams["type"] = strings.Join(param.Type, ",")
encodeTasksQuery(param, &req)
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (c *Client) CancelTasks(param *CancelTasksQuery) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/tasks/cancel",
method: http.MethodPost,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "CancelTasks",
}
if param != nil {
paramToSend := &TasksQuery{
UIDS: param.UIDS,
IndexUIDS: param.IndexUIDS,
Statuses: param.Statuses,
Types: param.Types,
BeforeEnqueuedAt: param.BeforeEnqueuedAt,
AfterEnqueuedAt: param.AfterEnqueuedAt,
BeforeStartedAt: param.BeforeStartedAt,
AfterStartedAt: param.AfterStartedAt,
}
if len(param.IndexUID) != 0 {
req.withQueryParams["indexUid"] = strings.Join(param.IndexUID, ",")
encodeTasksQuery(paramToSend, &req)
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
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
}

func (c *Client) SwapIndexes(param []SwapIndexesParams) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/swap-indexes",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: param,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "SwapIndexes",
}
if err := c.executeRequest(req); err != nil {
return nil, err
Expand Down Expand Up @@ -390,9 +459,7 @@ func convertKeyToParsedKey(key Key) (resp KeyParsed) {
// Convert time.Time to *string to feat the exact ISO-8601
// format of Meilisearch
if !key.ExpiresAt.IsZero() {
const Format = "2006-01-02T15:04:05"
timeParsedToString := key.ExpiresAt.Format(Format)
resp.ExpiresAt = &timeParsedToString
resp.ExpiresAt = formatDate(key.ExpiresAt, true)
}
return resp
}
Expand All @@ -401,3 +468,51 @@ func IsValidUUID(uuid string) bool {
r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
return r.MatchString(uuid)
}

func encodeTasksQuery(param *TasksQuery, req *internalRequest) {
if param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param.From != 0 {
req.withQueryParams["from"] = strconv.FormatInt(param.From, 10)
}
if len(param.Statuses) != 0 {
req.withQueryParams["statuses"] = strings.Join(param.Statuses, ",")
}
if len(param.Types) != 0 {
req.withQueryParams["types"] = strings.Join(param.Types, ",")
}
if len(param.IndexUIDS) != 0 {
req.withQueryParams["indexUids"] = strings.Join(param.IndexUIDS, ",")
}
if len(param.UIDS) != 0 {
req.withQueryParams["uids"] = strings.Trim(strings.Join(strings.Fields(fmt.Sprint(param.UIDS)), ","), "[]")
}
if len(param.CanceledBy) != 0 {
req.withQueryParams["canceledBy"] = strings.Trim(strings.Join(strings.Fields(fmt.Sprint(param.CanceledBy)), ","), "[]")
}
if !param.BeforeEnqueuedAt.IsZero() {
req.withQueryParams["beforeEnqueuedAt"] = *formatDate(param.BeforeEnqueuedAt, false)
}
if !param.AfterEnqueuedAt.IsZero() {
req.withQueryParams["afterEnqueuedAt"] = *formatDate(param.AfterEnqueuedAt, false)
}
if !param.BeforeStartedAt.IsZero() {
req.withQueryParams["beforeStartedAt"] = *formatDate(param.BeforeStartedAt, false)
}
if !param.AfterStartedAt.IsZero() {
req.withQueryParams["afterStartedAt"] = *formatDate(param.AfterStartedAt, false)
}
if !param.BeforeFinishedAt.IsZero() {
req.withQueryParams["beforeFinishedAt"] = *formatDate(param.BeforeFinishedAt, false)
}
if !param.AfterFinishedAt.IsZero() {
req.withQueryParams["afterFinishedAt"] = *formatDate(param.AfterFinishedAt, false)
}
}

func formatDate(date time.Time, key bool) *string {
const format = "2006-01-02T15:04:05Z"
timeParsedToString := date.Format(format)
return &timeParsedToString
}
Loading

0 comments on commit 9d80462

Please sign in to comment.