Split Run into two parts.

This will make writing request methods which return different response types simpler.
This commit is contained in:
Paul Bonser 2015-07-14 11:29:04 -05:00
parent 3f6bbaec1f
commit f760e32967
2 changed files with 69 additions and 44 deletions

79
goes.go
View File

@ -329,8 +329,47 @@ func (c *Connection) Delete(d Document, extraArgs url.Values) (*Response, error)
} }
// Run executes an elasticsearch Request. It converts data to Json, sends the // Run executes an elasticsearch Request. It converts data to Json, sends the
// request and return the Response obtained // request and returns the Response obtained
func (req *Request) Run() (*Response, error) { func (req *Request) Run() (*Response, error) {
body, statusCode, err := req.run()
esResp := &Response{Status: statusCode}
if err != nil {
return esResp, err
}
if req.method != "HEAD" {
err = json.Unmarshal(body, &esResp)
if err != nil {
return esResp, err
}
json.Unmarshal(body, &esResp.Raw)
}
err = json.Unmarshal(body, &esResp)
if err != nil {
return &Response{Status: statusCode}, err
}
if req.api == "_bulk" && esResp.Errors {
for _, item := range esResp.Items {
for _, i := range item {
if i.Error != "" {
return esResp, &SearchError{i.Error, i.Status}
}
}
}
return esResp, &SearchError{Msg: "Unknown error while bulk indexing"}
}
if esResp.Error != "" {
return esResp, &SearchError{esResp.Error, esResp.Status}
}
return esResp, nil
}
func (req *Request) run() ([]byte, uint64, error) {
postData := []byte{} postData := []byte{}
// XXX : refactor this // XXX : refactor this
@ -341,7 +380,7 @@ func (req *Request) Run() (*Response, error) {
} else { } else {
b, err := json.Marshal(req.Query) b, err := json.Marshal(req.Query)
if err != nil { if err != nil {
return &Response{}, err return nil, 0, err
} }
postData = b postData = b
} }
@ -350,7 +389,7 @@ func (req *Request) Run() (*Response, error) {
newReq, err := http.NewRequest(req.method, req.Url(), reader) newReq, err := http.NewRequest(req.method, req.Url(), reader)
if err != nil { if err != nil {
return &Response{}, err return nil, 0, err
} }
if req.method == "POST" || req.method == "PUT" { if req.method == "POST" || req.method == "PUT" {
@ -359,47 +398,21 @@ func (req *Request) Run() (*Response, error) {
resp, err := req.Conn.Client.Do(newReq) resp, err := req.Conn.Client.Do(newReq)
if err != nil { if err != nil {
return &Response{}, err return nil, 0, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return &Response{}, err return nil, uint64(resp.StatusCode), err
} }
if resp.StatusCode > 201 && resp.StatusCode < 400 { if resp.StatusCode > 201 && resp.StatusCode < 400 {
return &Response{}, errors.New(string(body)) return nil, uint64(resp.StatusCode), errors.New(string(body))
} }
esResp := new(Response) return body, uint64(resp.StatusCode), nil
if req.method == "HEAD" {
esResp.Status = uint64(resp.StatusCode)
} else {
err = json.Unmarshal(body, &esResp)
if err != nil {
return &Response{}, err
}
json.Unmarshal(body, &esResp.Raw)
}
if req.api == "_bulk" && esResp.Errors {
for _, item := range esResp.Items {
for _, i := range item {
if i.Error != "" {
return &Response{}, &SearchError{i.Error, i.Status}
}
}
}
return &Response{}, &SearchError{Msg: "Unknown error while bulk indexing"}
}
if esResp.Error != "" {
return &Response{}, &SearchError{esResp.Error, esResp.Status}
}
return esResp, nil
} }
// Url builds a Request for a URL // Url builds a Request for a URL

View File

@ -156,7 +156,8 @@ func (s *GoesTestSuite) TestDeleteIndexInexistantIndex(c *C) {
resp, err := conn.DeleteIndex("foobar") resp, err := conn.DeleteIndex("foobar")
c.Assert(err.Error(), Equals, "[404] IndexMissingException[[foobar] missing]") c.Assert(err.Error(), Equals, "[404] IndexMissingException[[foobar] missing]")
c.Assert(resp, DeepEquals, &Response{}) resp.Raw = nil // Don't make us have to duplicate this.
c.Assert(resp, DeepEquals, &Response{Status: 404, Error: "IndexMissingException[[foobar] missing]"})
} }
func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) { func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
@ -171,8 +172,10 @@ func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
resp, err := conn.DeleteIndex(indexName) resp, err := conn.DeleteIndex(indexName)
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := &Response{} expectedResponse := &Response{
expectedResponse.Acknowledged = true Acknowledged: true,
Status: 200,
}
resp.Raw = nil resp.Raw = nil
c.Assert(resp, DeepEquals, expectedResponse) c.Assert(resp, DeepEquals, expectedResponse)
} }
@ -379,6 +382,7 @@ func (s *GoesTestSuite) TestIndexWithFieldsInStruct(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := &Response{ expectedResponse := &Response{
Status: 201,
Index: indexName, Index: indexName,
Id: docId, Id: docId,
Type: docType, Type: docType,
@ -444,6 +448,7 @@ func (s *GoesTestSuite) TestIndexIdDefined(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := &Response{ expectedResponse := &Response{
Status: 201,
Index: indexName, Index: indexName,
Id: docId, Id: docId,
Type: docType, Type: docType,
@ -513,6 +518,7 @@ func (s *GoesTestSuite) TestDelete(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := &Response{ expectedResponse := &Response{
Status: 200,
Found: true, Found: true,
Index: indexName, Index: indexName,
Type: docType, Type: docType,
@ -527,6 +533,7 @@ func (s *GoesTestSuite) TestDelete(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse = &Response{ expectedResponse = &Response{
Status: 404,
Found: false, Found: false,
Index: indexName, Index: indexName,
Type: docType, Type: docType,
@ -588,6 +595,7 @@ func (s *GoesTestSuite) TestDeleteByQuery(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := &Response{ expectedResponse := &Response{
Status: 200,
Found: false, Found: false,
Index: "", Index: "",
Type: "", Type: "",
@ -633,6 +641,7 @@ func (s *GoesTestSuite) TestGet(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := &Response{ expectedResponse := &Response{
Status: 200,
Index: indexName, Index: indexName,
Type: docType, Type: docType,
Id: docId, Id: docId,
@ -650,6 +659,7 @@ func (s *GoesTestSuite) TestGet(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse = &Response{ expectedResponse = &Response{
Status: 200,
Index: indexName, Index: indexName,
Type: docType, Type: docType,
Id: docId, Id: docId,
@ -1147,6 +1157,7 @@ func (s *GoesTestSuite) TestUpdate(c *C) {
time.Sleep(200 * time.Millisecond) time.Sleep(200 * time.Millisecond)
expectedResponse := &Response{ expectedResponse := &Response{
Status: 201,
Index: indexName, Index: indexName,
Id: docId, Id: docId,
Type: docType, Type: docType,
@ -1317,6 +1328,7 @@ func (s *GoesTestSuite) TestAddAlias(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
expectedResponse := &Response{ expectedResponse := &Response{
Status: 200,
Index: indexName, Index: indexName,
Type: docType, Type: docType,
Id: docId, Id: docId,