4444function _M .new (opts )
4545 local timeout = opts .timeout
4646 local ttl = opts .ttl
47- local prefix = opts .prefix or " /v2/keys"
47+ local api_prefix = opts .api_prefix or " "
48+ local key_prefix = opts .key_prefix or " "
4849 local http_host = opts .http_host
4950 local user = opts .user
5051 local password = opts .password
@@ -54,15 +55,19 @@ function _M.new(opts)
5455 end
5556
5657 if not typeof .string (http_host ) and not typeof .table (http_host ) then
57- return nil , ' opts.host must be string or string array'
58+ return nil , ' opts.http_host must be string or string array'
5859 end
5960
6061 if not typeof .int (ttl ) then
6162 return nil , ' opts.ttl must be integer'
6263 end
6364
64- if not typeof .string (prefix ) then
65- return nil , ' opts.prefix must be string'
65+ if not typeof .string (api_prefix ) then
66+ return nil , ' opts.api_prefix must be string'
67+ end
68+
69+ if not typeof .string (key_prefix ) then
70+ return nil , ' opts.key_prefix must be string'
6671 end
6772
6873 if user and not typeof .string (user ) then
@@ -83,9 +88,9 @@ function _M.new(opts)
8388
8489 for _ , host in ipairs (http_hosts ) do
8590 table.insert (endpoints , {
86- full_prefix = host .. utils .normalize (prefix ),
91+ full_prefix = host .. utils .normalize (api_prefix ),
8792 http_host = host ,
88- prefix = prefix ,
93+ api_prefix = api_prefix ,
8994 version = host .. ' /version' ,
9095 stats_leader = host .. ' /v2/stats/leader' ,
9196 stats_self = host .. ' /v2/stats/self' ,
@@ -98,6 +103,7 @@ function _M.new(opts)
98103 init_count = 0 ,
99104 timeout = timeout ,
100105 ttl = ttl ,
106+ key_prefix = key_prefix ,
101107 is_cluster = # endpoints > 1 ,
102108 user = user ,
103109 password = password ,
@@ -237,8 +243,9 @@ local function set(self, key, val, attr)
237243
238244 local res
239245 res , err = _request (self , attr .in_order and ' POST' or ' PUT' ,
240- choose_endpoint (self ).full_prefix .. key ,
246+ choose_endpoint (self ).full_prefix .. " /keys " .. key ,
241247 opts , self .timeout )
248+
242249 if err then
243250 return nil , err
244251 end
@@ -306,7 +313,7 @@ local function get(self, key, attr)
306313 end
307314
308315 local res , err = _request (self , " GET" ,
309- choose_endpoint (self ).full_prefix .. utils .normalize (key ),
316+ choose_endpoint (self ).full_prefix .. " /keys " .. utils .normalize (key ),
310317 opts , attr and attr .timeout or self .timeout )
311318 if err then
312319 return res , err
@@ -367,7 +374,7 @@ local function delete(self, key, attr)
367374
368375 -- todo: check arguments
369376 return _request (self , " DELETE" ,
370- choose_endpoint (self ).full_prefix .. utils .normalize (key ),
377+ choose_endpoint (self ).full_prefix .. " /keys " .. utils .normalize (key ),
371378 opts , self .timeout )
372379end
373380
@@ -378,6 +385,8 @@ function _M.get(self, key)
378385 return nil , ' key must be string'
379386 end
380387
388+ key = utils .get_real_key (self .key_prefix , key )
389+
381390 return get (self , key )
382391end
383392
@@ -388,6 +397,8 @@ function _M.wait(self, key, modified_index, timeout)
388397 attr .wait_index = modified_index
389398 attr .timeout = timeout
390399
400+ key = utils .get_real_key (self .key_prefix , key )
401+
391402 return get (self , key , attr )
392403end
393404
@@ -396,6 +407,8 @@ function _M.readdir(self, key, recursive)
396407 attr .dir = true
397408 attr .recursive = recursive
398409
410+ key = utils .get_real_key (self .key_prefix , key )
411+
399412 return get (self , key , attr )
400413end
401414
@@ -408,6 +421,8 @@ function _M.waitdir(self, key, modified_index, timeout)
408421 attr .wait_index = modified_index
409422 attr .timeout = timeout
410423
424+ key = utils .get_real_key (self .key_prefix , key )
425+
411426 return get (self , key , attr )
412427end
413428
@@ -442,6 +457,8 @@ function _M.set(self, key, val, ttl)
442457 clear_tab (attr )
443458 attr .ttl = ttl
444459
460+ key = utils .get_real_key (self .key_prefix , key )
461+
445462 return set (self , key , val , attr )
446463end
447464
@@ -451,6 +468,8 @@ function _M.setnx(self, key, val, ttl)
451468 attr .ttl = ttl
452469 attr .prev_exist = false
453470
471+ key = utils .get_real_key (self .key_prefix , key )
472+
454473 return set (self , key , val , attr )
455474end
456475
@@ -461,6 +480,8 @@ function _M.setx(self, key, val, ttl, modified_index)
461480 attr .prev_exist = true
462481 attr .prev_index = modified_index
463482
483+ key = utils .get_real_key (self .key_prefix , key )
484+
464485 return set (self , key , val , attr )
465486end
466487
@@ -470,6 +491,8 @@ function _M.mkdir(self, key, ttl)
470491 attr .ttl = ttl
471492 attr .dir = true
472493
494+ key = utils .get_real_key (self .key_prefix , key )
495+
473496 return set (self , key , nil , attr )
474497end
475498
@@ -480,6 +503,8 @@ function _M.mkdirnx(self, key, ttl)
480503 attr .dir = true
481504 attr .prev_exist = false
482505
506+ key = utils .get_real_key (self .key_prefix , key )
507+
483508 return set (self , key , nil , attr )
484509end
485510
@@ -489,6 +514,8 @@ function _M.push(self, key, val, ttl)
489514 attr .ttl = ttl
490515 attr .in_order = true
491516
517+ key = utils .get_real_key (self .key_prefix , key )
518+
492519 return set (self , key , val , attr )
493520end
494521
@@ -502,6 +529,8 @@ function _M.delete(self, key, prev_val, modified_index)
502529 attr .prev_value = prev_val
503530 attr .prev_index = modified_index
504531
532+ key = utils .get_real_key (self .key_prefix , key )
533+
505534 return delete (self , key , attr )
506535end
507536
@@ -510,6 +539,8 @@ function _M.rmdir(self, key, recursive)
510539 attr .dir = true
511540 attr .recursive = recursive
512541
542+ key = utils .get_real_key (self .key_prefix , key )
543+
513544 return delete (self , key , attr )
514545end
515546
0 commit comments