Split Run into two parts.
This will make writing request methods which return different response types simpler.
This commit is contained in:
		
							parent
							
								
									3f6bbaec1f
								
							
						
					
					
						commit
						f760e32967
					
				
							
								
								
									
										79
									
								
								goes.go
									
									
									
									
									
								
							
							
						
						
									
										79
									
								
								goes.go
									
									
									
									
									
								
							@ -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
 | 
			
		||||
// request and return the Response obtained
 | 
			
		||||
// request and returns the Response obtained
 | 
			
		||||
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{}
 | 
			
		||||
 | 
			
		||||
	// XXX : refactor this
 | 
			
		||||
@ -341,7 +380,7 @@ func (req *Request) Run() (*Response, error) {
 | 
			
		||||
	} else {
 | 
			
		||||
		b, err := json.Marshal(req.Query)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return &Response{}, err
 | 
			
		||||
			return nil, 0, err
 | 
			
		||||
		}
 | 
			
		||||
		postData = b
 | 
			
		||||
	}
 | 
			
		||||
@ -350,7 +389,7 @@ func (req *Request) Run() (*Response, error) {
 | 
			
		||||
 | 
			
		||||
	newReq, err := http.NewRequest(req.method, req.Url(), reader)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return &Response{}, err
 | 
			
		||||
		return nil, 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if req.method == "POST" || req.method == "PUT" {
 | 
			
		||||
@ -359,47 +398,21 @@ func (req *Request) Run() (*Response, error) {
 | 
			
		||||
 | 
			
		||||
	resp, err := req.Conn.Client.Do(newReq)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return &Response{}, err
 | 
			
		||||
		return nil, 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	body, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return &Response{}, err
 | 
			
		||||
		return nil, uint64(resp.StatusCode), err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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)
 | 
			
		||||
	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
 | 
			
		||||
	return body, uint64(resp.StatusCode), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Url builds a Request for a URL
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										34
									
								
								goes_test.go
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								goes_test.go
									
									
									
									
									
								
							@ -156,7 +156,8 @@ func (s *GoesTestSuite) TestDeleteIndexInexistantIndex(c *C) {
 | 
			
		||||
	resp, err := conn.DeleteIndex("foobar")
 | 
			
		||||
 | 
			
		||||
	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) {
 | 
			
		||||
@ -171,8 +172,10 @@ func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
 | 
			
		||||
	resp, err := conn.DeleteIndex(indexName)
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse := &Response{}
 | 
			
		||||
	expectedResponse.Acknowledged = true
 | 
			
		||||
	expectedResponse := &Response{
 | 
			
		||||
		Acknowledged: true,
 | 
			
		||||
		Status:       200,
 | 
			
		||||
	}
 | 
			
		||||
	resp.Raw = nil
 | 
			
		||||
	c.Assert(resp, DeepEquals, expectedResponse)
 | 
			
		||||
}
 | 
			
		||||
@ -379,6 +382,7 @@ func (s *GoesTestSuite) TestIndexWithFieldsInStruct(c *C) {
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse := &Response{
 | 
			
		||||
		Status:  201,
 | 
			
		||||
		Index:   indexName,
 | 
			
		||||
		Id:      docId,
 | 
			
		||||
		Type:    docType,
 | 
			
		||||
@ -444,6 +448,7 @@ func (s *GoesTestSuite) TestIndexIdDefined(c *C) {
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse := &Response{
 | 
			
		||||
		Status:  201,
 | 
			
		||||
		Index:   indexName,
 | 
			
		||||
		Id:      docId,
 | 
			
		||||
		Type:    docType,
 | 
			
		||||
@ -513,10 +518,11 @@ func (s *GoesTestSuite) TestDelete(c *C) {
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse := &Response{
 | 
			
		||||
		Found: true,
 | 
			
		||||
		Index: indexName,
 | 
			
		||||
		Type:  docType,
 | 
			
		||||
		Id:    docId,
 | 
			
		||||
		Status: 200,
 | 
			
		||||
		Found:  true,
 | 
			
		||||
		Index:  indexName,
 | 
			
		||||
		Type:   docType,
 | 
			
		||||
		Id:     docId,
 | 
			
		||||
		// XXX : even after a DELETE the version number seems to be incremented
 | 
			
		||||
		Version: 2,
 | 
			
		||||
	}
 | 
			
		||||
@ -527,10 +533,11 @@ func (s *GoesTestSuite) TestDelete(c *C) {
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse = &Response{
 | 
			
		||||
		Found: false,
 | 
			
		||||
		Index: indexName,
 | 
			
		||||
		Type:  docType,
 | 
			
		||||
		Id:    docId,
 | 
			
		||||
		Status: 404,
 | 
			
		||||
		Found:  false,
 | 
			
		||||
		Index:  indexName,
 | 
			
		||||
		Type:   docType,
 | 
			
		||||
		Id:     docId,
 | 
			
		||||
		// XXX : even after a DELETE the version number seems to be incremented
 | 
			
		||||
		Version: 3,
 | 
			
		||||
	}
 | 
			
		||||
@ -588,6 +595,7 @@ func (s *GoesTestSuite) TestDeleteByQuery(c *C) {
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse := &Response{
 | 
			
		||||
		Status:  200,
 | 
			
		||||
		Found:   false,
 | 
			
		||||
		Index:   "",
 | 
			
		||||
		Type:    "",
 | 
			
		||||
@ -633,6 +641,7 @@ func (s *GoesTestSuite) TestGet(c *C) {
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse := &Response{
 | 
			
		||||
		Status:  200,
 | 
			
		||||
		Index:   indexName,
 | 
			
		||||
		Type:    docType,
 | 
			
		||||
		Id:      docId,
 | 
			
		||||
@ -650,6 +659,7 @@ func (s *GoesTestSuite) TestGet(c *C) {
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse = &Response{
 | 
			
		||||
		Status:  200,
 | 
			
		||||
		Index:   indexName,
 | 
			
		||||
		Type:    docType,
 | 
			
		||||
		Id:      docId,
 | 
			
		||||
@ -1147,6 +1157,7 @@ func (s *GoesTestSuite) TestUpdate(c *C) {
 | 
			
		||||
	time.Sleep(200 * time.Millisecond)
 | 
			
		||||
 | 
			
		||||
	expectedResponse := &Response{
 | 
			
		||||
		Status:  201,
 | 
			
		||||
		Index:   indexName,
 | 
			
		||||
		Id:      docId,
 | 
			
		||||
		Type:    docType,
 | 
			
		||||
@ -1317,6 +1328,7 @@ func (s *GoesTestSuite) TestAddAlias(c *C) {
 | 
			
		||||
	c.Assert(err, IsNil)
 | 
			
		||||
 | 
			
		||||
	expectedResponse := &Response{
 | 
			
		||||
		Status:  200,
 | 
			
		||||
		Index:   indexName,
 | 
			
		||||
		Type:    docType,
 | 
			
		||||
		Id:      docId,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user