client: add a Search method

Change-Id: Iec4c22bf6e4bbe40645ec178b5c2d23392ad270d
This commit is contained in:
Brad Fitzpatrick 2013-12-27 16:22:06 -08:00
parent 3e0b83e717
commit 7d4c86d0de
2 changed files with 25 additions and 0 deletions

View File

@ -433,6 +433,29 @@ func (c *Client) Describe(req *search.DescribeRequest) (*search.DescribeResponse
return res, nil
}
func (c *Client) Search(req *search.SearchQuery) (*search.SearchResult, error) {
sr, err := c.SearchRoot()
if err != nil {
return nil, err
}
url := sr + req.URLSuffix()
body, err := json.MarshalIndent(req, "", "\t")
if err != nil {
return nil, err
}
hreq := c.newRequest("POST", url, bytes.NewReader(body))
hres, err := c.doReqGated(hreq)
if err != nil {
return nil, err
}
defer hres.Body.Close()
res := new(search.SearchResult)
if err := json.NewDecoder(hres.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
// SearchExistingFileSchema does a search query looking for an
// existing file with entire contents of wholeRef, then does a HEAD
// request to verify the file still exists on the server. If so,

View File

@ -75,6 +75,8 @@ type SearchQuery struct {
Describe *DescribeRequest `json:"describe,omitempty"`
}
func (q *SearchQuery) URLSuffix() string { return "camli/search/query" }
func (q *SearchQuery) fromHTTP(req *http.Request) error {
dec := json.NewDecoder(io.LimitReader(req.Body, 1<<20))
if err := dec.Decode(q); err != nil {