Browse Source

Merge pull request #8 from opsmatic/customclient

Added HTTP client override support
tags/v1.0.0
Jérôme Renard 10 years ago
parent
commit
18e694b712
4 changed files with 46 additions and 7 deletions
  1. +17
    -2
      example_test.go
  2. +7
    -4
      goes.go
  3. +17
    -1
      goes_test.go
  4. +5
    -0
      structs.go

+ 17
- 2
example_test.go View File

@@ -6,8 +6,11 @@ package goes_test


import ( import (
"fmt" "fmt"
"github.com/belogik/goes"
"net/http"
"net/url" "net/url"
"time"

"github.com/belogik/goes"
) )


func ExampleConnection_CreateIndex() { func ExampleConnection_CreateIndex() {
@@ -86,7 +89,7 @@ func ExampleConnection_Search() {
}, },
}, },
} }
extraArgs := make(url.Values, 1) extraArgs := make(url.Values, 1)


searchResults, err := conn.Search(query, []string{"someindex"}, []string{""}, extraArgs) searchResults, err := conn.Search(query, []string{"someindex"}, []string{""}, extraArgs)
@@ -143,3 +146,15 @@ func ExampleConnection_Delete() {


fmt.Printf("%s", response) fmt.Printf("%s", response)
} }

func ExampleConnectionOverrideHttpClient() {
tr := &http.Transport{
ResponseHeaderTimeout: 1 * time.Second,
}
cl := &http.Client{
Transport: tr,
}
conn := goes.NewConnection("localhost", "9200").WithClient(cl)

fmt.Printf("%v\n", conn.Client)
}

+ 7
- 4
goes.go View File

@@ -31,7 +31,12 @@ 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}
return &Connection{host, port, http.DefaultClient}
}

func (c *Connection) WithClient(cl *http.Client) *Connection {
c.Client = cl
return c
} }


// CreateIndex creates a new index represented by a name and a mapping // CreateIndex creates a new index represented by a name and a mapping
@@ -285,8 +290,6 @@ func (req *Request) Run() (Response, error) {


reader := bytes.NewReader(postData) reader := bytes.NewReader(postData)


client := http.DefaultClient

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 Response{}, err
@@ -296,7 +299,7 @@ func (req *Request) Run() (Response, error) {
newReq.Header.Set("Content-Type", "application/x-www-form-urlencoded") newReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} }


resp, err := client.Do(newReq)
resp, err := req.Conn.Client.Do(newReq)
if err != nil { if err != nil {
return Response{}, err return Response{}, err
} }


+ 17
- 1
goes_test.go View File

@@ -6,6 +6,7 @@ package goes


import ( import (
. "launchpad.net/gocheck" . "launchpad.net/gocheck"
"net/http"
"net/url" "net/url"
"os" "os"
"testing" "testing"
@@ -38,7 +39,22 @@ 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})
c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, http.DefaultClient})
}

func (s *GoesTestSuite) TestWithClient(c *C) {
tr := &http.Transport{
DisableCompression: true,
ResponseHeaderTimeout: 1 * time.Second,
}
cl := &http.Client{
Transport: tr,
}
conn := NewConnection(ES_HOST, ES_PORT).WithClient(cl)

c.Assert(conn, DeepEquals, &Connection{ES_HOST, ES_PORT, cl})
c.Assert(conn.Client.Transport.(*http.Transport).DisableCompression, Equals, true)
c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second)
} }


func (s *GoesTestSuite) TestUrl(c *C) { func (s *GoesTestSuite) TestUrl(c *C) {


+ 5
- 0
structs.go View File

@@ -5,6 +5,7 @@
package goes package goes


import ( import (
"net/http"
"net/url" "net/url"
) )


@@ -15,6 +16,10 @@ type Connection struct {


// The port to use // The port to use
Port string Port string

// Client is the http client used to make requests, allowing settings things
// such as timeouts etc
Client *http.Client
} }


// Represents a Request to elasticsearch // Represents a Request to elasticsearch


Loading…
Cancel
Save