diff --git a/README.md b/README.md index 698a2b3..90ba769 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ fmt.Println(lazyCat) lc.Delete(res.ID, res.Rev) params := url.Values{"limit": []string{"5"}} -results, err := c.View("myapp", "all", ¶ms) +results, err := c.View("myapp", "all", ¶ms, nil) if err != nil { // Do whatever } diff --git a/couch.go b/couch.go index 382efe0..aeb1e6b 100644 --- a/couch.go +++ b/couch.go @@ -143,17 +143,42 @@ type Row struct { Value interface{} } -func (c *Client) View(design string, name string, options *url.Values) (*MultiDocResponse, error) { - res, _, err := c.execRead("GET", c.DBPath() + "/_design/" + design + "/_view/" + name, nil, options, nil) +type KeysRequest struct { + Keys []string `json:"keys"` +} + +func (c *Client) View(design string, name string, options *url.Values, keys *[]string) (multiDocResponse *MultiDocResponse, err error) { + url := c.UrlString(c.DBPath() + "/_design/" + design + "/_view/" + name, options) + + method := "" + body := new(bytes.Buffer) + if keys != nil { + reqJson, _ := json.Marshal(KeysRequest{Keys: *keys}) + body = bytes.NewBuffer(reqJson) + method = "POST" + } else { + method = "GET" + } + + req, err := c.NewRequest(method, url, body, nil) if err != nil { - return nil, err + return } - multiDocResponse := &MultiDocResponse{} - if err = json.Unmarshal(res, multiDocResponse); err != nil { - return nil, err + httpResp, err := http.DefaultClient.Do(req) + if err != nil { + return + } + + respBody, err := ioutil.ReadAll(httpResp.Body) + if err != nil { + return } - return multiDocResponse, nil + + if err = json.Unmarshal(respBody, &multiDocResponse); err != nil { + return + } + return } func (c *Client) Copy(src string, dest string, destRev *string) (resp *Response, code int, err error) { diff --git a/couch_test.go b/couch_test.go index 71d32c8..18c72bf 100644 --- a/couch_test.go +++ b/couch_test.go @@ -146,7 +146,7 @@ func TestView(t *testing.T) { c.Save(&Dog{Name: "Savannah", Type: "dog"}) params := url.Values{"limit": []string{"5"}} - res, err := c.View("dog", "dog", ¶ms) + res, err := c.View("dog", "dog", ¶ms, nil) if err != nil { t.Error(err) } @@ -160,6 +160,19 @@ func TestView(t *testing.T) { fmt.Println("View") fmt.Println(res) fmt.Println("View") + + dog1 := Dog{ID: "dog1", Type: "dog"} + dog2 := Dog{ID: "dog2", Type: "dog"} + c.BulkSave(dog1, dog2) + + resp, err := c.View("dog", "dog", nil, &[]string{"dog1", "dog2"}) + if err != nil { + t.Error(err) + } + + fmt.Println("ViewWithKeys") + fmt.Println(resp) + fmt.Println("ViewWithKeys") } func TestCopy(t *testing.T) {