This commit is contained in:
Alexey 2016-10-05 06:30:55 +00:00 committed by GitHub
commit ace66c8925
3 changed files with 28 additions and 4 deletions

25
goes.go
View File

@ -32,7 +32,7 @@ func (err *SearchError) Error() string {
// This function is pretty useless for now but might be useful in a near future // This function is pretty useless for now but might be useful in a near future
// if wee need more features like connection pooling or load balancing. // if wee need more features like connection pooling or load balancing.
func NewConnection(host string, port string) *Connection { func NewConnection(host string, port string) *Connection {
return &Connection{host, port, http.DefaultClient} return &Connection{host, port, http.DefaultClient, false}
} }
func (c *Connection) WithClient(cl *http.Client) *Connection { func (c *Connection) WithClient(cl *http.Client) *Connection {
@ -342,6 +342,27 @@ func (c *Connection) Delete(d Document, extraArgs url.Values) (*Response, error)
return r.Run() return r.Run()
} }
// Enable use number behaviour
func (c *Connection) UseNumberEnable() {
c.UseNumber = true
}
func (req *Request) unmarshalBody(body []byte, esResp *Response) error {
if !req.Conn.UseNumber {
err := json.Unmarshal(body, esResp)
if err != nil {
return err
}
} else {
dec := json.NewDecoder(bytes.NewReader(body))
dec.UseNumber()
if err := dec.Decode(esResp); err != nil {
return err
}
}
return nil
}
// 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 returns the Response obtained // request and returns the Response obtained
func (req *Request) Run() (*Response, error) { func (req *Request) Run() (*Response, error) {
@ -353,7 +374,7 @@ func (req *Request) Run() (*Response, error) {
} }
if req.method != "HEAD" { if req.method != "HEAD" {
err = json.Unmarshal(body, &esResp) err = req.unmarshalBody(body, esResp)
if err != nil { if err != nil {
return esResp, err return esResp, err
} }

View File

@ -41,7 +41,7 @@ func (s *GoesTestSuite) SetUpTest(c *C) {
func (s *GoesTestSuite) TestNewConnection(c *C) { func (s *GoesTestSuite) TestNewConnection(c *C) {
conn := NewConnection(ES_HOST, ES_PORT) conn := NewConnection(ES_HOST, ES_PORT)
c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, http.DefaultClient}) c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, http.DefaultClient, false})
} }
func (s *GoesTestSuite) TestWithClient(c *C) { func (s *GoesTestSuite) TestWithClient(c *C) {
@ -54,7 +54,7 @@ func (s *GoesTestSuite) TestWithClient(c *C) {
} }
conn := NewConnection(ES_HOST, ES_PORT).WithClient(cl) conn := NewConnection(ES_HOST, ES_PORT).WithClient(cl)
c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, cl}) c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, cl, false})
c.Assert(conn.Client.Transport.(*http.Transport).DisableCompression, Equals, true) c.Assert(conn.Client.Transport.(*http.Transport).DisableCompression, Equals, true)
c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second) c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second)
} }

View File

@ -20,6 +20,9 @@ type Connection struct {
// Client is the http client used to make requests, allowing settings things // Client is the http client used to make requests, allowing settings things
// such as timeouts etc // such as timeouts etc
Client *http.Client Client *http.Client
// Specify to use numbers when unmarshal
UseNumber bool
} }
// Represents a Request to elasticsearch // Represents a Request to elasticsearch