From 807d7f92b058b5f7e4559ccd194adb6cdd53af19 Mon Sep 17 00:00:00 2001 From: alimy Date: Tue, 21 Jun 2022 23:55:50 +0800 Subject: [PATCH] optimize #118 just pretty code for zinc client logic --- pkg/zinc/zinc.go | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/pkg/zinc/zinc.go b/pkg/zinc/zinc.go index 4ea31051..63dfeeb1 100644 --- a/pkg/zinc/zinc.go +++ b/pkg/zinc/zinc.go @@ -90,9 +90,7 @@ func (c *ZincClient) CreateIndex(name string, p *ZincIndexProperty) bool { Properties: p, }, } - client := resty.New() // 创建一个restry客户端 - client.DisableWarn = true - resp, err := client.R().SetBody(data).SetBasicAuth(c.ZincUser, c.ZincPassword).Put(c.ZincHost + "/api/index") + resp, err := c.request().SetBody(data).Put(c.ZincHost + "/api/index") if err != nil || resp.StatusCode() != http.StatusOK { return false @@ -103,9 +101,7 @@ func (c *ZincClient) CreateIndex(name string, p *ZincIndexProperty) bool { // 检查索引是否存在 func (c *ZincClient) ExistIndex(name string) bool { - client := resty.New() - client.DisableWarn = true - resp, err := client.R().SetBasicAuth(c.ZincUser, c.ZincPassword).Get(c.ZincHost + "/api/index") + resp, err := c.request().Get(c.ZincHost + "/api/index") if err != nil || resp.StatusCode() != http.StatusOK { return false @@ -126,9 +122,7 @@ func (c *ZincClient) ExistIndex(name string) bool { // 新增/更新文档 func (c *ZincClient) PutDoc(name string, id int64, doc interface{}) (bool, error) { - client := resty.New() - client.DisableWarn = true - resp, err := client.R().SetBody(doc).SetBasicAuth(c.ZincUser, c.ZincPassword).Put(fmt.Sprintf("%s/api/%s/_doc/%d", c.ZincHost, name, id)) + resp, err := c.request().SetBody(doc).Put(fmt.Sprintf("%s/api/%s/_doc/%d", c.ZincHost, name, id)) if err != nil { return false, err @@ -151,9 +145,7 @@ func (c *ZincClient) BulkPushDoc(docs []map[string]interface{}) (bool, error) { } } - client := resty.New() - client.DisableWarn = true - resp, err := client.R().SetBody(dataStr).SetBasicAuth(c.ZincUser, c.ZincPassword).Post(fmt.Sprintf("%s/api/_bulk", c.ZincHost)) + resp, err := c.request().SetBody(dataStr).Post(fmt.Sprintf("%s/api/_bulk", c.ZincHost)) if err != nil { return false, err } @@ -166,9 +158,7 @@ func (c *ZincClient) BulkPushDoc(docs []map[string]interface{}) (bool, error) { } func (c *ZincClient) EsQuery(indexName string, q interface{}) (*QueryResultT, error) { - client := resty.New() - client.DisableWarn = true - resp, err := client.R().SetBody(q).SetBasicAuth(c.ZincUser, c.ZincPassword).Post(fmt.Sprintf("%s/es/%s/_search", c.ZincHost, indexName)) + resp, err := c.request().SetBody(q).Post(fmt.Sprintf("%s/es/%s/_search", c.ZincHost, indexName)) if err != nil { return nil, err } @@ -187,9 +177,7 @@ func (c *ZincClient) EsQuery(indexName string, q interface{}) (*QueryResultT, er } func (c *ZincClient) ApiQuery(indexName string, q interface{}) (*QueryResultT, error) { - client := resty.New() - client.DisableWarn = true - resp, err := client.R().SetBody(q).SetBasicAuth(c.ZincUser, c.ZincPassword).Post(fmt.Sprintf("%s/api/%s/_search", c.ZincHost, indexName)) + resp, err := c.request().SetBody(q).Post(fmt.Sprintf("%s/api/%s/_search", c.ZincHost, indexName)) if err != nil { return nil, err } @@ -208,9 +196,7 @@ func (c *ZincClient) ApiQuery(indexName string, q interface{}) (*QueryResultT, e } func (c *ZincClient) DelDoc(indexName, id string) error { - client := resty.New() - client.DisableWarn = true - resp, err := client.R().SetBasicAuth(c.ZincUser, c.ZincPassword).Delete(fmt.Sprintf("%s/api/%s/_doc/%s", c.ZincHost, indexName, id)) + resp, err := c.request().Delete(fmt.Sprintf("%s/api/%s/_doc/%s", c.ZincHost, indexName, id)) if err != nil { return err } @@ -221,3 +207,11 @@ func (c *ZincClient) DelDoc(indexName, id string) error { return nil } + +func (c *ZincClient) request() *resty.Request { + client := resty.New() + client.DisableWarn = true + client.SetBasicAuth(c.ZincUser, c.ZincPassword) + + return client.R() +}