Compare commits

...

2 Commits

Author SHA1 Message Date
Paul Bonser
d7fb602e0f Allow username:password to be included with host 2017-03-02 19:02:12 -06:00
Paul Bonser
429a615005 Allow inclusion of https in host 2017-03-02 18:58:52 -06:00
3 changed files with 22 additions and 9 deletions

15
goes.go
View File

@ -34,7 +34,15 @@ 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 NewClient(host string, port string) *Client { 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. // 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) { func (c *Client) replaceHost(req *http.Request) {
req.URL.Scheme = "http" req.URL.User = c.Host.User
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 // DoRaw Does the provided requeset and returns the raw bytes and the status code of the response

View File

@ -41,7 +41,12 @@ func (s *GoesTestSuite) SetUpTest(c *C) {
func (s *GoesTestSuite) TestNewClient(c *C) { func (s *GoesTestSuite) TestNewClient(c *C) {
conn := NewClient(ESHost, ESPort) 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) 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) { func (s *GoesTestSuite) TestWithHTTPClient(c *C) {
@ -54,7 +59,8 @@ func (s *GoesTestSuite) TestWithHTTPClient(c *C) {
} }
conn := NewClient(ESHost, ESPort).WithHTTPClient(cl) 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).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

@ -7,15 +7,13 @@ package goes
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"net/url"
) )
// Client represents a connection to elasticsearch // Client represents a connection to elasticsearch
type Client struct { type Client struct {
// The host to connect to // The host to connect to
Host string Host *url.URL
// The port to use
Port string
// 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