From 3f0875ed66536671e8e8b14962eb4b4d89dc5262 Mon Sep 17 00:00:00 2001 From: Paul Bonser Date: Tue, 27 Sep 2016 14:30:17 -0500 Subject: [PATCH] make all request fields exported so custom requests can be done --- goes.go | 110 +++++++++++++++++++++++++-------------------------- goes_test.go | 16 ++++---- structs.go | 8 ++-- 3 files changed, 67 insertions(+), 67 deletions(-) diff --git a/goes.go b/goes.go index aa09e48..61af34f 100644 --- a/goes.go +++ b/goes.go @@ -49,7 +49,7 @@ func (c *Client) CreateIndex(name string, mapping interface{}) (*Response, error Conn: c, Query: mapping, IndexList: []string{name}, - method: "PUT", + Method: "PUT", } return r.Run() @@ -60,7 +60,7 @@ func (c *Client) DeleteIndex(name string) (*Response, error) { r := Request{ Conn: c, IndexList: []string{name}, - method: "DELETE", + Method: "DELETE", } return r.Run() @@ -71,8 +71,8 @@ func (c *Client) RefreshIndex(name string) (*Response, error) { r := Request{ Conn: c, IndexList: []string{name}, - method: "POST", - api: "_refresh", + Method: "POST", + API: "_refresh", } return r.Run() @@ -85,8 +85,8 @@ func (c *Client) UpdateIndexSettings(name string, settings interface{}) (*Respon Conn: c, Query: settings, IndexList: []string{name}, - method: "PUT", - api: "_settings", + Method: "PUT", + API: "_settings", } return r.Run() @@ -99,8 +99,8 @@ func (c *Client) Optimize(indexList []string, extraArgs url.Values) (*Response, Conn: c, IndexList: indexList, ExtraArgs: extraArgs, - method: "POST", - api: "_optimize", + Method: "POST", + API: "_optimize", } return r.Run() @@ -112,8 +112,8 @@ func (c *Client) Stats(indexList []string, extraArgs url.Values) (*Response, err Conn: c, IndexList: indexList, ExtraArgs: extraArgs, - method: "GET", - api: "_stats", + Method: "GET", + API: "_stats", } return r.Run() @@ -125,8 +125,8 @@ func (c *Client) IndexStatus(indexList []string) (*Response, error) { r := Request{ Conn: c, IndexList: indexList, - method: "GET", - api: "_status", + Method: "GET", + API: "_status", } return r.Run() @@ -204,9 +204,9 @@ func (c *Client) BulkSend(documents []Document) (*Response, error) { r := Request{ Conn: c, - method: "POST", - api: "_bulk", - bulkData: bytes.Join(bulkData, []byte("\n")), + Method: "POST", + API: "_bulk", + BulkData: bytes.Join(bulkData, []byte("\n")), } return r.Run() @@ -219,8 +219,8 @@ func (c *Client) Search(query interface{}, indexList []string, typeList []string Query: query, IndexList: indexList, TypeList: typeList, - method: "POST", - api: "_search", + Method: "POST", + API: "_search", ExtraArgs: extraArgs, } @@ -234,8 +234,8 @@ func (c *Client) Count(query interface{}, indexList []string, typeList []string, Query: query, IndexList: indexList, TypeList: typeList, - method: "POST", - api: "_count", + Method: "POST", + API: "_count", ExtraArgs: extraArgs, } @@ -251,8 +251,8 @@ func (c *Client) Query(query interface{}, indexList []string, typeList []string, Query: query, IndexList: indexList, TypeList: typeList, - method: httpMethod, - api: "_query", + Method: httpMethod, + API: "_query", ExtraArgs: extraArgs, } @@ -271,8 +271,8 @@ func (c *Client) Scan(query interface{}, indexList []string, typeList []string, Query: query, IndexList: indexList, TypeList: typeList, - method: "POST", - api: "_search", + Method: "POST", + API: "_search", ExtraArgs: v, } @@ -286,8 +286,8 @@ func (c *Client) Scroll(scrollID string, timeout string) (*Response, error) { r := Request{ Conn: c, - method: "POST", - api: "_search/scroll", + Method: "POST", + API: "_search/scroll", ExtraArgs: v, Body: []byte(scrollID), } @@ -300,8 +300,8 @@ func (c *Client) Get(index string, documentType string, id string, extraArgs url r := Request{ Conn: c, IndexList: []string{index}, - method: "GET", - api: documentType + "/" + id, + Method: "GET", + API: documentType + "/" + id, ExtraArgs: extraArgs, } @@ -318,12 +318,12 @@ func (c *Client) Index(d Document, extraArgs url.Values) (*Response, error) { IndexList: []string{d.Index.(string)}, TypeList: []string{d.Type}, ExtraArgs: extraArgs, - method: "POST", + Method: "POST", } if d.ID != nil { - r.method = "PUT" - r.id = d.ID.(string) + r.Method = "PUT" + r.ID = d.ID.(string) } return r.Run() @@ -338,8 +338,8 @@ func (c *Client) Delete(d Document, extraArgs url.Values) (*Response, error) { IndexList: []string{d.Index.(string)}, TypeList: []string{d.Type}, ExtraArgs: extraArgs, - method: "DELETE", - id: d.ID.(string), + Method: "DELETE", + ID: d.ID.(string), } return r.Run() @@ -355,7 +355,7 @@ func (req *Request) Run() (*Response, error) { return esResp, err } - if req.method != "HEAD" { + if req.Method != "HEAD" { err = json.Unmarshal(body, &esResp) if err != nil { return esResp, err @@ -366,7 +366,7 @@ func (req *Request) Run() (*Response, error) { } } - if req.api == "_bulk" && esResp.Errors { + if req.API == "_bulk" && esResp.Errors { for _, item := range esResp.Items { for _, i := range item { if i.Error != "" { @@ -390,8 +390,8 @@ func (req *Request) run() ([]byte, uint64, error) { // XXX : refactor this if len(req.Body) > 0 { postData = req.Body - } else if req.api == "_bulk" { - postData = req.bulkData + } else if req.API == "_bulk" { + postData = req.BulkData } else { b, err := json.Marshal(req.Query) if err != nil { @@ -402,12 +402,12 @@ func (req *Request) run() ([]byte, uint64, error) { reader := bytes.NewReader(postData) - newReq, err := http.NewRequest(req.method, req.URL(), reader) + newReq, err := http.NewRequest(req.Method, req.URL(), reader) if err != nil { return nil, 0, err } - if req.method == "POST" || req.method == "PUT" { + if req.Method == "POST" || req.Method == "PUT" { newReq.Header.Set("Content-Type", "application/json") } @@ -439,11 +439,11 @@ func (req *Request) URL() string { } // XXX : for indexing documents using the normal (non bulk) API - if len(req.id) > 0 { - path += "/" + req.id + if len(req.ID) > 0 { + path += "/" + req.ID } - path += "/" + req.api + path += "/" + req.API u := url.URL{ Scheme: "http", @@ -492,8 +492,8 @@ func (c *Client) PutMapping(typeName string, mapping interface{}, indexes []stri Conn: c, Query: mapping, IndexList: indexes, - method: "PUT", - api: "_mappings/" + typeName, + Method: "PUT", + API: "_mappings/" + typeName, } return r.Run() @@ -505,8 +505,8 @@ func (c *Client) GetMapping(types []string, indexes []string) (*Response, error) r := Request{ Conn: c, IndexList: indexes, - method: "GET", - api: "_mapping/" + strings.Join(types, ","), + Method: "GET", + API: "_mapping/" + strings.Join(types, ","), } return r.Run() @@ -518,7 +518,7 @@ func (c *Client) IndicesExist(indexes []string) (bool, error) { r := Request{ Conn: c, IndexList: indexes, - method: "HEAD", + Method: "HEAD", } resp, err := r.Run() @@ -534,12 +534,12 @@ func (c *Client) Update(d Document, query interface{}, extraArgs url.Values) (*R IndexList: []string{d.Index.(string)}, TypeList: []string{d.Type}, ExtraArgs: extraArgs, - method: "POST", - api: "_update", + Method: "POST", + API: "_update", } if d.ID != nil { - r.id = d.ID.(string) + r.ID = d.ID.(string) } return r.Run() @@ -551,8 +551,8 @@ func (c *Client) DeleteMapping(typeName string, indexes []string) (*Response, er r := Request{ Conn: c, IndexList: indexes, - method: "DELETE", - api: "_mappings/" + typeName, + Method: "DELETE", + API: "_mappings/" + typeName, } return r.Run() @@ -575,8 +575,8 @@ func (c *Client) modifyAlias(action string, alias string, indexes []string) (*Re r := Request{ Conn: c, Query: command, - method: "POST", - api: "_aliases", + Method: "POST", + API: "_aliases", } return r.Run() @@ -597,8 +597,8 @@ func (c *Client) AliasExists(alias string) (bool, error) { r := Request{ Conn: c, - method: "HEAD", - api: "_alias/" + alias, + Method: "HEAD", + API: "_alias/" + alias, } resp, err := r.Run() diff --git a/goes_test.go b/goes_test.go index 25b77af..ddd7731 100644 --- a/goes_test.go +++ b/goes_test.go @@ -67,8 +67,8 @@ func (s *GoesTestSuite) TestUrl(c *C) { Query: "q", IndexList: []string{"i"}, TypeList: []string{}, - method: "GET", - api: "_search", + Method: "GET", + API: "_search", } c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/i/_search") @@ -83,8 +83,8 @@ func (s *GoesTestSuite) TestUrl(c *C) { r.ExtraArgs.Set("version", "1") c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/a,b/c,d/_search?version=1") - r.id = "1234" - r.api = "" + r.ID = "1234" + r.API = "" c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/a,b/c,d/1234/?version=1") } @@ -97,8 +97,8 @@ func (s *GoesTestSuite) TestEsDown(c *C) { Conn: conn, Query: query, IndexList: []string{"i"}, - method: "GET", - api: "_search", + Method: "GET", + API: "_search", } _, err := r.Run() @@ -114,8 +114,8 @@ func (s *GoesTestSuite) TestRunMissingIndex(c *C) { Conn: conn, Query: query, IndexList: []string{"i"}, - method: "GET", - api: "_search", + Method: "GET", + API: "_search", } _, err := r.Run() diff --git a/structs.go b/structs.go index 00fa131..6aca112 100644 --- a/structs.go +++ b/structs.go @@ -37,13 +37,13 @@ type Request struct { TypeList []string // HTTP Method to user (GET, POST ...) - method string + Method string // Which api keyword (_search, _bulk, etc) to use - api string + API string // Bulk data - bulkData []byte + BulkData []byte // Request body Body []byte @@ -52,7 +52,7 @@ type Request struct { ExtraArgs url.Values // Used for the id field when indexing a document - id string + ID string } // Response holds an elasticsearch response