diff --git a/client/keys.go b/client/keys.go index 9be36f2d4..8c383c0fa 100644 --- a/client/keys.go +++ b/client/keys.go @@ -56,14 +56,12 @@ func NewKeysAPIWithPrefix(c Client, p string) KeysAPI { } type KeysAPI interface { + Get(ctx context.Context, key string, opts *GetOptions) (*Response, error) Set(ctx context.Context, key, value string, opts *SetOptions) (*Response, error) - Create(ctx context.Context, key, value string) (*Response, error) - Update(ctx context.Context, key, value string) (*Response, error) - Delete(ctx context.Context, key string, opts *DeleteOptions) (*Response, error) - Get(ctx context.Context, key string) (*Response, error) - RGet(ctx context.Context, key string) (*Response, error) + Create(ctx context.Context, key, value string) (*Response, error) + Update(ctx context.Context, key, value string) (*Response, error) Watcher(key string, opts *WatcherOptions) Watcher } @@ -113,6 +111,12 @@ type SetOptions struct { TTL time.Duration } +type GetOptions struct { + // Recursive defines whether or not all children of the Node + // should be returned. + Recursive bool +} + type DeleteOptions struct { // PrevValue specifies what the current value of the Node must // be in order for the Delete operation to succeed. @@ -224,29 +228,17 @@ func (k *httpKeysAPI) Delete(ctx context.Context, key string, opts *DeleteOption return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body) } -func (k *httpKeysAPI) Get(ctx context.Context, key string) (*Response, error) { - get := &getAction{ - Prefix: k.prefix, - Key: key, - Recursive: false, +func (k *httpKeysAPI) Get(ctx context.Context, key string, opts *GetOptions) (*Response, error) { + act := &getAction{ + Prefix: k.prefix, + Key: key, } - resp, body, err := k.client.Do(ctx, get) - if err != nil { - return nil, err + if opts != nil { + act.Recursive = opts.Recursive } - return unmarshalHTTPResponse(resp.StatusCode, resp.Header, body) -} - -func (k *httpKeysAPI) RGet(ctx context.Context, key string) (*Response, error) { - get := &getAction{ - Prefix: k.prefix, - Key: key, - Recursive: true, - } - - resp, body, err := k.client.Do(ctx, get) + resp, body, err := k.client.Do(ctx, act) if err != nil { return nil, err } diff --git a/discovery/discovery.go b/discovery/discovery.go index 2f06901f7..e6c819f4d 100644 --- a/discovery/discovery.go +++ b/discovery/discovery.go @@ -199,7 +199,7 @@ func (d *discovery) checkCluster() ([]*client.Node, int, uint64, error) { configKey := path.Join("/", d.cluster, "_config") ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout) // find cluster size - resp, err := d.c.Get(ctx, path.Join(configKey, "size")) + resp, err := d.c.Get(ctx, path.Join(configKey, "size"), nil) cancel() if err != nil { if err == client.ErrKeyNoExist { @@ -216,7 +216,7 @@ func (d *discovery) checkCluster() ([]*client.Node, int, uint64, error) { } ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout) - resp, err = d.c.Get(ctx, d.cluster) + resp, err = d.c.Get(ctx, d.cluster, nil) cancel() if err != nil { if err == client.ErrTimeout { diff --git a/discovery/discovery_test.go b/discovery/discovery_test.go index d8bb0080c..0d1b84e1b 100644 --- a/discovery/discovery_test.go +++ b/discovery/discovery_test.go @@ -422,7 +422,7 @@ func (c *clientWithResp) Create(ctx context.Context, key string, value string) ( return r, nil } -func (c *clientWithResp) Get(ctx context.Context, key string) (*client.Response, error) { +func (c *clientWithResp) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) { if len(c.rs) == 0 { return &client.Response{}, client.ErrKeyNoExist } @@ -445,7 +445,7 @@ func (c *clientWithErr) Create(ctx context.Context, key string, value string) (* return &client.Response{}, c.err } -func (c *clientWithErr) Get(ctx context.Context, key string) (*client.Response, error) { +func (c *clientWithErr) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) { return &client.Response{}, c.err } @@ -490,12 +490,12 @@ func (c *clientWithRetry) Create(ctx context.Context, key string, value string) return c.clientWithResp.Create(ctx, key, value) } -func (c *clientWithRetry) Get(ctx context.Context, key string) (*client.Response, error) { +func (c *clientWithRetry) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) { if c.failCount < c.failTimes { c.failCount++ return nil, client.ErrTimeout } - return c.clientWithResp.Get(ctx, key) + return c.clientWithResp.Get(ctx, key, opts) } // watcherWithRetry will timeout all requests up to failTimes