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

Add typo tolerance settings #294

Merged
merged 2 commits into from May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions index_settings.go
Expand Up @@ -445,3 +445,52 @@ func (i Index) ResetSortableAttributes() (resp *Task, err error) {
}
return resp, nil
}

func (i Index) GetTypoTolerance() (resp *TypoTolerance, err error) {
resp = &TypoTolerance{}
req := internalRequest{
endpoint: "/indexes/" + i.UID + "/settings/typo-tolerance",
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTypoTolerance",
}
if err := i.client.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (i Index) UpdateTypoTolerance(request *TypoTolerance) (resp *Task, err error) {
resp = &Task{}
req := internalRequest{
endpoint: "/indexes/" + i.UID + "/settings/typo-tolerance",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: &request,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you enlighten me on this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object doesn't really exist in go, so to send multiple arguments it's easier to use struct. For this purpose, we create this struct with all data we need to process a request:

type internalRequest struct {
	endpoint    string
	method      string
	contentType string

	withRequest     interface{}
	withResponse    interface{}
	withQueryParams map[string]string

	acceptedStatusCodes []int

	functionName string
}

We send the struct to the eexecuteRequestfunction with all the data needed, and ainterface{}inwithRequest` who will be fill with the response from the API.

withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "UpdateTypoTolerance",
}
if err := i.client.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (i Index) ResetTypoTolerance() (resp *Task, err error) {
resp = &Task{}
req := internalRequest{
endpoint: "/indexes/" + i.UID + "/settings/typo-tolerance",
method: http.MethodDelete,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "ResetTypoTolerance",
}
if err := i.client.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}