Skip to content

Commit

Permalink
Merge branch 'main' into update_version_to_v0.20.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alallema authored Jul 11, 2022
2 parents 953fb3b + 64f5ee5 commit eeaaa12
Show file tree
Hide file tree
Showing 19 changed files with 3,035 additions and 1,517 deletions.
22 changes: 10 additions & 12 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
get_one_index_1: |-
client.GetIndex("movies")
list_all_indexes_1: |-
client.GetAllIndexes()
client.GetAllIndexes(nil)
create_an_index_1: |-
client.CreateIndex(&meilisearch.IndexConfig{
Uid: "movies",
Expand All @@ -20,10 +20,10 @@ delete_an_index_1: |-
client.Index("movies").Delete()
get_one_document_1: |-
var a interface{}
client.Index("movies").GetDocument("25684", &a)
client.Index("movies").GetDocument("25684", nil, &a)
get_documents_1: |-
var a []interface{}
client.Index("movies").GetDocuments(&meilisearch.DocumentsRequest{
client.Index("movies").GetDocuments(nil, &meilisearch.DocumentsRequest{
Limit: 2,
}, &a)
add_or_replace_documents_1: |-
Expand Down Expand Up @@ -62,15 +62,15 @@ search_post_1: |-
get_task_1: |-
client.GetTask(1);
get_all_tasks_1: |-
client.GetTasks();
client.GetTasks(nil);
get_task_by_index_1: |-
client.Index("movies").GetTask(1)
get_all_tasks_by_index_1: |-
client.Index("movies").GetTasks()
client.Index("movies").GetTasks(nil)
get_one_key_1: |-
client.GetKey("d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4")
get_keys_1: |-
client.GetKeys()
client.GetKeys(nil)
create_a_key_1: |-
client.CreateKey(&meilisearch.Key{
Description: "Add documents: Products API key",
Expand Down Expand Up @@ -306,7 +306,7 @@ search_parameter_guide_highlight_tag_1: |-
})
search_parameter_guide_matches_1: |-
resp, err := client.Index("movies").Search("winter feast", &meilisearch.SearchRequest{
Matches: true,
ShowMatchesPosition: true,
})
settings_guide_synonyms_1: |-
settings := meilisearch.Settings{
Expand Down Expand Up @@ -578,7 +578,7 @@ faceted_search_filter_1: |-
})
faceted_search_facets_distribution_1: |-
resp, err := client.Index("movies").Search("Batman", &meilisearch.SearchRequest{
FacetsDistribution: []string{
Facets: []string{
"genres",
},
})
Expand All @@ -591,8 +591,6 @@ faceted_search_walkthrough_filter_1: |-
})
post_dump_1: |-
resp, err := client.CreateDump()
get_dump_status_1: |-
resp, err := client.GetDumpStatus("dump-uid")
phrase_search_1: |-
resp, err := client.Index("movies").Search("\"african american\" horror", &meilisearch.SearchRequest{})
sorting_guide_update_sortable_attributes_1: |-
Expand Down Expand Up @@ -710,7 +708,7 @@ security_guide_list_keys_1: |-
Host: "http://127.0.0.1:7700",
APIKey: "masterKey",
})
client.GetKeys();
client.GetKeys(nil);
security_guide_delete_key_1: |-
client := meilisearch.NewClient(meilisearch.ClientConfig{
Host: "http://127.0.0.1:7700",
Expand All @@ -722,7 +720,7 @@ authorization_header_1: |-
Host: "http://127.0.0.1:7700",
APIKey: "masterKey",
})
client.GetKeys();
client.GetKeys(nil);
tenant_token_guide_generate_sdk_1: |-
searchRules := map[string]interface{}{
"patient_medical_records": map[string]string{
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func main() {
os.Exit(1)
}

fmt.Println(task.taskID)
fmt.Println(task.taskUID)
}
```

With the `taskID`, you can check the status (`enqueued`, `processing`, `succeeded` or `failed`) of your documents addition using the [task endpoint](https://docs.meilisearch.com/learn/advanced/asynchronous_operations.html#task-status).
With the `taskUID`, you can check the status (`enqueued`, `processing`, `succeeded` or `failed`) of your documents addition using the [task endpoint](https://docs.meilisearch.com/learn/advanced/asynchronous_operations.html#task-status).

#### Basic Search <!-- omit in toc -->

Expand Down Expand Up @@ -219,15 +219,15 @@ searchRes, err := index.Search("wonder",
],
"offset": 0,
"limit": 20,
"nbHits": 1,
"estimatedTotalHits": 1,
"processingTimeMs": 0,
"query": "wonder"
}
```

## 🤖 Compatibility with Meilisearch

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

## 💡 Learn More

Expand Down
94 changes: 64 additions & 30 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"

"github.com/golang-jwt/jwt"
Expand Down Expand Up @@ -35,26 +37,25 @@ 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 *Task, err error)
DeleteIndex(uid string) (resp *Task, err error)
GetAllIndexes(param *IndexesQuery) (resp *IndexesResults, err error)
GetAllRawIndexes(param *IndexesQuery) (resp map[string]interface{}, err error)
CreateIndex(config *IndexConfig) (resp *TaskInfo, err error)
DeleteIndex(uid string) (resp *TaskInfo, err error)
CreateKey(request *Key) (resp *Key, err error)
GetKey(identifier string) (resp *Key, err error)
GetKeys() (resp *ResultKey, err error)
UpdateKey(identifier string, request *Key) (resp *Key, err error)
DeleteKey(identifier string) (resp bool, err error)
GetKeys(param *KeysQuery) (resp *KeysResults, err error)
UpdateKey(keyOrUID string, request *Key) (resp *Key, err error)
DeleteKey(keyOrUID string) (resp bool, err error)
GetAllStats() (resp *Stats, err error)
CreateDump() (resp *Dump, err error)
GetDumpStatus(dumpUID string) (resp *Dump, err error)
CreateDump() (resp *TaskInfo, err error)
Version() (*Version, error)
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)
GenerateTenantToken(searchRules map[string]interface{}, options *TenantTokenOptions) (resp string, err error)
GetTask(taskUID int64) (resp *Task, err error)
GetTasks(param *TasksQuery) (resp *TaskResult, err error)
WaitForTask(taskUID int64, options ...WaitParams) (*Task, error)
GenerateTenantToken(APIKeyUID string, searchRules map[string]interface{}, options *TenantTokenOptions) (resp string, err error)
}

var _ ClientInterface = &Client{}
Expand Down Expand Up @@ -152,27 +153,34 @@ func (c *Client) GetKey(identifier string) (resp *Key, err error) {
return resp, nil
}

func (c *Client) GetKeys() (resp *ResultKey, err error) {
resp = &ResultKey{}
func (c *Client) GetKeys(param *KeysQuery) (resp *KeysResults, err error) {
resp = &KeysResults{}
req := internalRequest{
endpoint: "/keys",
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetKeys",
}
if param != nil && param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param != nil && param.Offset != 0 {
req.withQueryParams["offset"] = strconv.FormatInt(param.Offset, 10)
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (c *Client) UpdateKey(identifier string, request *Key) (resp *Key, err error) {
parsedRequest := convertKeyToParsedKey(*request)
func (c *Client) UpdateKey(keyOrUID string, request *Key) (resp *Key, err error) {
parsedRequest := KeyUpdate{Name: request.Name, Description: request.Description}
resp = &Key{}
req := internalRequest{
endpoint: "/keys/" + identifier,
endpoint: "/keys/" + keyOrUID,
method: http.MethodPatch,
contentType: contentTypeJSON,
withRequest: &parsedRequest,
Expand All @@ -186,9 +194,9 @@ func (c *Client) UpdateKey(identifier string, request *Key) (resp *Key, err erro
return resp, nil
}

func (c *Client) DeleteKey(identifier string) (resp bool, err error) {
func (c *Client) DeleteKey(keyOrUID string) (resp bool, err error) {
req := internalRequest{
endpoint: "/keys/" + identifier,
endpoint: "/keys/" + keyOrUID,
method: http.MethodDelete,
withRequest: nil,
withResponse: nil,
Expand Down Expand Up @@ -224,8 +232,8 @@ func (c *Client) IsHealthy() bool {
return true
}

func (c *Client) CreateDump() (resp *Dump, err error) {
resp = &Dump{}
func (c *Client) CreateDump() (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/dumps",
method: http.MethodPost,
Expand Down Expand Up @@ -257,10 +265,10 @@ func (c *Client) GetDumpStatus(dumpUID string) (resp *Dump, err error) {
return resp, nil
}

func (c *Client) GetTask(taskID int64) (resp *Task, err error) {
func (c *Client) GetTask(taskUID int64) (resp *Task, err error) {
resp = &Task{}
req := internalRequest{
endpoint: "/tasks/" + strconv.FormatInt(taskID, 10),
endpoint: "/tasks/" + strconv.FormatInt(taskUID, 10),
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
Expand All @@ -273,16 +281,34 @@ func (c *Client) GetTask(taskID int64) (resp *Task, err error) {
return resp, nil
}

func (c *Client) GetTasks() (resp *ResultTask, err error) {
resp = &ResultTask{}
func (c *Client) GetTasks(param *TasksQuery) (resp *TaskResult, err error) {
resp = &TaskResult{}
req := internalRequest{
endpoint: "/tasks",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
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, ",")
}
if len(param.IndexUID) != 0 {
req.withQueryParams["indexUid"] = strings.Join(param.IndexUID, ",")
}
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
Expand All @@ -295,7 +321,7 @@ func (c *Client) GetTasks() (resp *ResultTask, err error) {
// 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) {
func (c *Client) WaitForTask(taskUID int64, options ...WaitParams) (*Task, error) {
if options == nil {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFunc()
Expand All @@ -310,7 +336,7 @@ func (c *Client) WaitForTask(task *Task, options ...WaitParams) (*Task, error) {
if err := options[0].Context.Err(); err != nil {
return nil, err
}
getTask, err := c.GetTask(task.UID)
getTask, err := c.GetTask(taskUID)
if err != nil {
return nil, err
}
Expand All @@ -327,14 +353,17 @@ func (c *Client) WaitForTask(task *Task, options ...WaitParams) (*Task, error) {
// accessible indexes for the signing API Key.
// ExpiresAt options is a time.Time when the key will expire. Note that if an ExpiresAt value is included it should be in UTC time.
// ApiKey options is the API key parent of the token. If you leave it empty the client API Key will be used.
func (c *Client) GenerateTenantToken(SearchRules map[string]interface{}, Options *TenantTokenOptions) (resp string, err error) {
func (c *Client) GenerateTenantToken(APIKeyUID string, SearchRules map[string]interface{}, Options *TenantTokenOptions) (resp string, err error) {
// Validate the arguments
if SearchRules == nil {
return "", fmt.Errorf("GenerateTenantToken: The search rules added in the token generation must be of type array or object")
}
if (Options == nil || Options.APIKey == "") && c.config.APIKey == "" {
return "", fmt.Errorf("GenerateTenantToken: The API key used for the token generation must exist and be a valid Meilisearch key")
}
if APIKeyUID == "" || !IsValidUUID(APIKeyUID) {
return "", fmt.Errorf("GenerateTenantToken: The uid used for the token generation must exist and comply to uuid4 format")
}
if Options != nil && !Options.ExpiresAt.IsZero() && Options.ExpiresAt.Before(time.Now()) {
return "", fmt.Errorf("GenerateTenantToken: When the expiresAt field in the token generation has a value, it must be a date set in the future")
}
Expand All @@ -356,7 +385,7 @@ func (c *Client) GenerateTenantToken(SearchRules map[string]interface{}, Options
ExpiresAt: Options.ExpiresAt.Unix(),
}
}
claims.APIKeyPrefix = secret[:8]
claims.APIKeyUID = APIKeyUID
claims.SearchRules = SearchRules

// Create a new token object, specifying signing method and the claims
Expand All @@ -383,3 +412,8 @@ func convertKeyToParsedKey(key Key) (resp KeyParsed) {
}
return resp
}

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)
}
Loading

0 comments on commit eeaaa12

Please sign in to comment.