Compare commits
2 Commits
allow_http
...
master
Author | SHA1 | Date | |
---|---|---|---|
89c18a567f | |||
|
fbfb1d80a8 |
24
goes.go
24
goes.go
@ -34,15 +34,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 NewClient(host string, port string) *Client {
|
func NewClient(host string, port string) *Client {
|
||||||
if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") {
|
return &Client{host, port, http.DefaultClient, "", "", ""}
|
||||||
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.
|
||||||
@ -582,9 +574,8 @@ func (c *Client) AliasExists(alias string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) replaceHost(req *http.Request) {
|
func (c *Client) replaceHost(req *http.Request) {
|
||||||
req.URL.User = c.Host.User
|
req.URL.Scheme = "http"
|
||||||
req.URL.Scheme = c.Host.Scheme
|
req.URL.Host = fmt.Sprintf("%s:%s", c.Host, c.Port)
|
||||||
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
|
||||||
@ -594,6 +585,11 @@ func (c *Client) DoRaw(r Requester) ([]byte, uint64, error) {
|
|||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
c.replaceHost(req)
|
c.replaceHost(req)
|
||||||
|
|
||||||
|
if c.AuthUsername != "" {
|
||||||
|
req.SetBasicAuth(c.AuthUsername, c.AuthPassword)
|
||||||
|
}
|
||||||
|
|
||||||
return c.doRequest(req)
|
return c.doRequest(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -605,6 +601,10 @@ func (c *Client) Do(r Requester) (*Response, error) {
|
|||||||
}
|
}
|
||||||
c.replaceHost(req)
|
c.replaceHost(req)
|
||||||
|
|
||||||
|
if c.AuthUsername != "" {
|
||||||
|
req.SetBasicAuth(c.AuthUsername, c.AuthPassword)
|
||||||
|
}
|
||||||
|
|
||||||
body, statusCode, err := c.doRequest(req)
|
body, statusCode, err := c.doRequest(req)
|
||||||
esResp := &Response{Status: statusCode}
|
esResp := &Response{Status: statusCode}
|
||||||
|
|
||||||
|
13
goes_test.go
13
goes_test.go
@ -5,14 +5,13 @@
|
|||||||
package goes
|
package goes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
. "gopkg.in/check.v1"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/go-check/check"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -41,12 +40,7 @@ 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{&url.URL{Scheme: "http", Host: ESHost + ":" + ESPort}, http.DefaultClient, ""})
|
c.Assert(conn, DeepEquals, &Client{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) {
|
||||||
@ -59,8 +53,7 @@ func (s *GoesTestSuite) TestWithHTTPClient(c *C) {
|
|||||||
}
|
}
|
||||||
conn := NewClient(ESHost, ESPort).WithHTTPClient(cl)
|
conn := NewClient(ESHost, ESPort).WithHTTPClient(cl)
|
||||||
|
|
||||||
c.Assert(conn.Host, DeepEquals, &url.URL{Scheme: "http", Host: ESHost + ":" + ESPort})
|
c.Assert(conn, DeepEquals, &Client{ESHost, ESPort, cl, "", "", ""})
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,12 @@ type Request struct {
|
|||||||
|
|
||||||
// Used for the id field when indexing a document
|
// Used for the id field when indexing a document
|
||||||
ID string
|
ID string
|
||||||
|
|
||||||
|
// Auth username
|
||||||
|
AuthUsername string
|
||||||
|
|
||||||
|
// Auth password
|
||||||
|
AuthPassword string
|
||||||
}
|
}
|
||||||
|
|
||||||
// URL builds a URL for a Request
|
// URL builds a URL for a Request
|
||||||
|
12
structs.go
12
structs.go
@ -7,13 +7,15 @@ 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 *url.URL
|
Host string
|
||||||
|
|
||||||
|
// 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
|
||||||
@ -21,6 +23,12 @@ type Client struct {
|
|||||||
|
|
||||||
// Detected version of ES
|
// Detected version of ES
|
||||||
version string
|
version string
|
||||||
|
|
||||||
|
// user name for http basic auth
|
||||||
|
AuthUsername string `json:"username"`
|
||||||
|
|
||||||
|
// pass word for http basic auth
|
||||||
|
AuthPassword string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response holds an elasticsearch response
|
// Response holds an elasticsearch response
|
||||||
|
Loading…
Reference in New Issue
Block a user