From 429a61500562b2308c388382e2dba81eeb3734ce Mon Sep 17 00:00:00 2001 From: Paul Bonser Date: Thu, 2 Mar 2017 18:58:52 -0600 Subject: [PATCH 1/2] Allow inclusion of https in host --- goes.go | 15 ++++++++++++--- goes_test.go | 5 +++-- structs.go | 6 ++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/goes.go b/goes.go index 5ff3d1c..99f7668 100644 --- a/goes.go +++ b/goes.go @@ -34,7 +34,15 @@ func (err *SearchError) Error() string { // 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. func NewClient(host string, port string) *Client { - return &Client{host, port, http.DefaultClient, ""} + if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") { + host = "http://" + host + } + host = host + ":" + port + u, err := url.Parse(host) + if err != nil { + panic(err) + } + return &Client{u, http.DefaultClient, ""} } // WithHTTPClient sets the http.Client to be used with the connection. Returns the original client. @@ -574,8 +582,9 @@ func (c *Client) AliasExists(alias string) (bool, error) { } func (c *Client) replaceHost(req *http.Request) { - req.URL.Scheme = "http" - req.URL.Host = fmt.Sprintf("%s:%s", c.Host, c.Port) + + req.URL.Scheme = c.Host.Scheme + req.URL.Host = c.Host.Host } // DoRaw Does the provided requeset and returns the raw bytes and the status code of the response diff --git a/goes_test.go b/goes_test.go index 0050a36..a843c5d 100644 --- a/goes_test.go +++ b/goes_test.go @@ -41,7 +41,7 @@ func (s *GoesTestSuite) SetUpTest(c *C) { func (s *GoesTestSuite) TestNewClient(c *C) { conn := NewClient(ESHost, ESPort) - c.Assert(conn, DeepEquals, &Client{ESHost, ESPort, http.DefaultClient, ""}) + c.Assert(conn, DeepEquals, &Client{&url.URL{Scheme: "http", Host: ESHost + ":" + ESPort}, http.DefaultClient, ""}) } func (s *GoesTestSuite) TestWithHTTPClient(c *C) { @@ -54,7 +54,8 @@ func (s *GoesTestSuite) TestWithHTTPClient(c *C) { } conn := NewClient(ESHost, ESPort).WithHTTPClient(cl) - c.Assert(conn, DeepEquals, &Client{ESHost, ESPort, cl, ""}) + c.Assert(conn.Host, DeepEquals, &url.URL{Scheme: "http", Host: ESHost + ":" + ESPort}) + c.Assert(conn.Client, DeepEquals, cl) c.Assert(conn.Client.Transport.(*http.Transport).DisableCompression, Equals, true) c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second) } diff --git a/structs.go b/structs.go index 75cdc5d..68bca63 100644 --- a/structs.go +++ b/structs.go @@ -7,15 +7,13 @@ package goes import ( "encoding/json" "net/http" + "net/url" ) // Client represents a connection to elasticsearch type Client struct { // The host to connect to - Host string - - // The port to use - Port string + Host *url.URL // Client is the http client used to make requests, allowing settings things // such as timeouts etc From d7fb602e0f22f943b1cda708b96557bc88c646d6 Mon Sep 17 00:00:00 2001 From: Paul Bonser Date: Thu, 2 Mar 2017 19:02:12 -0600 Subject: [PATCH 2/2] Allow username:password to be included with host --- goes.go | 2 +- goes_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/goes.go b/goes.go index 99f7668..0bb5035 100644 --- a/goes.go +++ b/goes.go @@ -582,7 +582,7 @@ func (c *Client) AliasExists(alias string) (bool, error) { } func (c *Client) replaceHost(req *http.Request) { - + req.URL.User = c.Host.User req.URL.Scheme = c.Host.Scheme req.URL.Host = c.Host.Host } diff --git a/goes_test.go b/goes_test.go index a843c5d..da3f46d 100644 --- a/goes_test.go +++ b/goes_test.go @@ -44,6 +44,11 @@ func (s *GoesTestSuite) TestNewClient(c *C) { c.Assert(conn, DeepEquals, &Client{&url.URL{Scheme: "http", Host: ESHost + ":" + ESPort}, http.DefaultClient, ""}) } +func (s *GoesTestSuite) TestNewClientWithAuth(c *C) { + conn := NewClient("foo:bar@"+ESHost, ESPort) + c.Assert(conn, DeepEquals, &Client{&url.URL{Scheme: "http", User: url.UserPassword("foo", "bar"), Host: ESHost + ":" + ESPort}, http.DefaultClient, ""}) +} + func (s *GoesTestSuite) TestWithHTTPClient(c *C) { tr := &http.Transport{ DisableCompression: true,