Skip to content

Commit

Permalink
Merge pull request #31 from derekahn/fix-DocumentsList
Browse files Browse the repository at this point in the history
Fix 🐛 Documents.List()
  • Loading branch information
eskombro authored May 18, 2020
2 parents 4702e2b + 22ce0d4 commit 9375223
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 15 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)

Expand Down Expand Up @@ -162,7 +164,19 @@ func (c Client) sendRequest(req *internalRequest, internalError *Error) (*http.R

internalError.RequestToString = string(rawJSONRequest)

request, err = http.NewRequest(req.method, c.config.Host+req.endpoint, bytes.NewBuffer(rawJSONRequest))
URL := c.config.Host + req.endpoint

// Build query params for Batch GET 'Documents.List'
if req.method == "GET" {
r, ok := req.withRequest.(*ListDocumentsRequest)
if ok {
URL = fmt.Sprintf("%s?limit=%d&offset=%d&attributesToRetrieve=%s",
URL, r.Limit, r.Offset, strings.Join(r.AttributesToRetrieve, ","),
)
}
}

request, err = http.NewRequest(req.method, URL, bytes.NewBuffer(rawJSONRequest))
} else {
request, err = http.NewRequest(req.method, c.config.Host+req.endpoint, nil)
}
Expand Down
17 changes: 7 additions & 10 deletions client_documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,17 @@ func TestClientDocuments_List(t *testing.T) {
Host: "http://localhost:7700",
})

_, err := client.Indexes().Create(CreateIndexRequest{
if _, err := client.Indexes().Create(CreateIndexRequest{
UID: indexUID,
})

if err != nil {
}); err != nil {
t.Fatal(err)
}

updateIDRes, err := client.
Documents(indexUID).
AddOrUpdate([]interface{}{
docTest{ID: "123", Name: "nestle"},
docTest{ID: "456", Name: "nestle"},
docTest{ID: "456", Name: "hershey"},
})

if err != nil {
Expand All @@ -166,17 +164,16 @@ func TestClientDocuments_List(t *testing.T) {

var list []docTest
err = client.Documents(indexUID).List(ListDocumentsRequest{
Offset: 0,
Limit: 100,
Offset: 1,
Limit: 1,
}, &list)

if err != nil {
t.Fatal(err)
}

// tests are running in parallel so there can be more than 1 docs
if len(list) < 1 {
t.Fatal("number of doc should be at least 1")
if len(list) == 0 || list[0].ID != "456" {
t.Fatal("expected to return the document[1]")
}
}

Expand Down

0 comments on commit 9375223

Please sign in to comment.