func (db *Database) GetView(docName string, viewName string, query string) ([]byte, error) { log.Debugf("couch : GetView query %s in viewName %s of desDoc %s", query, viewName, docName) type ViewResponse struct { Error string `json:"error"` Reason string `json:"reason"` } var body string var errs []error var prefix string var superAgent *gorequest.SuperAgent if query == "" { prefix = docName + "/_view/" + viewName log.Debug("Getting view name " + prefix) _, body, errs = db.Req.Get(prefix).End() } else { values, err := url.ParseQuery(query) if err != nil { return nil, errors.New("Unable to parse query string: " + query) } encodedKey := values.Encode() prefix = docName + "/_view/" + viewName superAgent = db.Req.Get(prefix).Query(encodedKey) _, body, errs = superAgent.End() log.Debug("Url" + superAgent.Url + encodedKey) } if len(errs) > 0 { return nil, errors.New("Database : Error making request " + fmt.Sprint("%v", errs)) } viewResp := &ViewResponse{} err := json.Unmarshal([]byte(body), viewResp) if err != nil { log.Error(body) return nil, err } if viewResp.Error != "" { err = errors.New(viewResp.Error + " " + viewResp.Reason + "\n req " + superAgent.Url) return nil, err } log.Debug("Returning body", body) return []byte(body), nil }
// Execute a parse request func do(req *gorequest.SuperAgent, data interface{}) error { res, body, errs := req.End() if errs != nil { return fmt.Errorf("%v", errs) } if res.StatusCode < 200 || res.StatusCode >= 300 { // parse as error model reserr := new(Error) err := json.NewDecoder(strings.NewReader(body)).Decode(reserr) if err != nil { return err } return reserr } if data == nil { return nil } return json.NewDecoder(strings.NewReader(body)).Decode(data) }