Skip to content

Commit de4aec8

Browse files
committed
TypedAPI: Integration of the first working version in the client
1 parent 5fa1401 commit de4aec8

File tree

2,332 files changed

+256062
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,332 files changed

+256062
-25
lines changed

.doc/typedapi.asciidoc

Whitespace-only changes.

elasticsearch.go

+79-23
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/base64"
2222
"errors"
2323
"fmt"
24+
"github.com/elastic/go-elasticsearch/v8/typedapi"
2425
"net/http"
2526
"net/url"
2627
"os"
@@ -108,10 +109,9 @@ type Config struct {
108109
ConnectionPoolFunc func([]*elastictransport.Connection, elastictransport.Selector) elastictransport.ConnectionPool
109110
}
110111

111-
// Client represents the Elasticsearch client.
112+
// BaseClient represents the Elasticsearch client.
112113
//
113-
type Client struct {
114-
*esapi.API // Embeds the API methods
114+
type BaseClient struct {
115115
Transport elastictransport.Interface
116116
metaHeader string
117117
compatibilityHeader bool
@@ -121,6 +121,18 @@ type Client struct {
121121
productCheckSuccess bool
122122
}
123123

124+
// Client represents the Functional Options API.
125+
type Client struct {
126+
BaseClient
127+
*esapi.API
128+
}
129+
130+
// TypedClient represents the Typed API.
131+
type TypedClient struct {
132+
BaseClient
133+
*typedapi.API
134+
}
135+
124136
// NewDefaultClient creates a new client with default options.
125137
//
126138
// It will use http://localhost:9200 as the default address.
@@ -145,6 +157,65 @@ func NewDefaultClient() (*Client, error) {
145157
// It's an error to set both cfg.Addresses and cfg.CloudID.
146158
//
147159
func NewClient(cfg Config) (*Client, error) {
160+
tp, err := newTransport(cfg)
161+
if err != nil {
162+
return nil, err
163+
}
164+
165+
compatHeaderEnv := os.Getenv(esCompatHeader)
166+
compatibilityHeader, _ := strconv.ParseBool(compatHeaderEnv)
167+
168+
client := &Client{
169+
BaseClient: BaseClient{
170+
Transport: tp,
171+
disableMetaHeader: cfg.DisableMetaHeader,
172+
metaHeader: initMetaHeader(tp),
173+
compatibilityHeader: cfg.EnableCompatibilityMode || compatibilityHeader,
174+
},
175+
}
176+
client.API = esapi.New(client)
177+
178+
if cfg.DiscoverNodesOnStart {
179+
go client.DiscoverNodes()
180+
}
181+
182+
return client, nil
183+
}
184+
185+
// NewTypedClient create a new client with the configuration from cfg.
186+
//
187+
// This version uses the same configuration as NewClient.
188+
//
189+
// It will return the client with the TypedAPI.
190+
func NewTypedClient(cfg Config) (*TypedClient, error) {
191+
tp, err := newTransport(cfg)
192+
if err != nil {
193+
return nil, err
194+
}
195+
196+
compatHeaderEnv := os.Getenv(esCompatHeader)
197+
compatibilityHeader, _ := strconv.ParseBool(compatHeaderEnv)
198+
199+
metaHeader := strings.Join([]string{initMetaHeader(tp), "hl=1"}, ",")
200+
201+
client := &TypedClient{
202+
BaseClient: BaseClient{
203+
Transport: tp,
204+
disableMetaHeader: cfg.DisableMetaHeader,
205+
metaHeader: metaHeader,
206+
compatibilityHeader: cfg.EnableCompatibilityMode || compatibilityHeader,
207+
},
208+
}
209+
client.API = typedapi.New(client)
210+
211+
if cfg.DiscoverNodesOnStart {
212+
go client.DiscoverNodes()
213+
}
214+
215+
return client, nil
216+
}
217+
218+
func newTransport(cfg Config) (*elastictransport.Client, error) {
148219
var addrs []string
149220

150221
if len(cfg.Addresses) == 0 && cfg.CloudID == "" {
@@ -219,27 +290,12 @@ func NewClient(cfg Config) (*Client, error) {
219290
return nil, fmt.Errorf("error creating transport: %s", err)
220291
}
221292

222-
compatHeaderEnv := os.Getenv(esCompatHeader)
223-
compatibilityHeader, _ := strconv.ParseBool(compatHeaderEnv)
224-
225-
client := &Client{
226-
Transport: tp,
227-
disableMetaHeader: cfg.DisableMetaHeader,
228-
metaHeader: initMetaHeader(tp),
229-
compatibilityHeader: cfg.EnableCompatibilityMode || compatibilityHeader,
230-
}
231-
client.API = esapi.New(client)
232-
233-
if cfg.DiscoverNodesOnStart {
234-
go client.DiscoverNodes()
235-
}
236-
237-
return client, nil
293+
return tp, nil
238294
}
239295

240296
// Perform delegates to Transport to execute a request and return a response.
241297
//
242-
func (c *Client) Perform(req *http.Request) (*http.Response, error) {
298+
func (c *BaseClient) Perform(req *http.Request) (*http.Response, error) {
243299
// Compatibility Header
244300
if c.compatibilityHeader {
245301
if req.Body != nil {
@@ -276,7 +332,7 @@ func (c *Client) Perform(req *http.Request) (*http.Response, error) {
276332

277333
// doProductCheck calls f if there as not been a prior successful call to doProductCheck,
278334
// returning nil otherwise.
279-
func (c *Client) doProductCheck(f func() error) error {
335+
func (c *BaseClient) doProductCheck(f func() error) error {
280336
c.productCheckMu.RLock()
281337
productCheckSuccess := c.productCheckSuccess
282338
c.productCheckMu.RUnlock()
@@ -312,7 +368,7 @@ func genuineCheckHeader(header http.Header) error {
312368

313369
// Metrics returns the client metrics.
314370
//
315-
func (c *Client) Metrics() (elastictransport.Metrics, error) {
371+
func (c *BaseClient) Metrics() (elastictransport.Metrics, error) {
316372
if mt, ok := c.Transport.(elastictransport.Measurable); ok {
317373
return mt.Metrics()
318374
}
@@ -321,7 +377,7 @@ func (c *Client) Metrics() (elastictransport.Metrics, error) {
321377

322378
// DiscoverNodes reloads the client connections by fetching information from the cluster.
323379
//
324-
func (c *Client) DiscoverNodes() error {
380+
func (c *BaseClient) DiscoverNodes() error {
325381
if dt, ok := c.Transport.(elastictransport.Discoverable); ok {
326382
return dt.DiscoverNodes()
327383
}

0 commit comments

Comments
 (0)