Class KeyValueClient

java.lang.Object
org.kiwiproject.consul.KeyValueClient

public class KeyValueClient extends Object
HTTP Client for /v1/kv/ endpoints.
  • Field Details

  • Method Details

    • getValue

      public Optional<Value> getValue(String key)
      Retrieves a Value for a specific key from the key/value store.

      GET /v1/kv/{key}

      Parameters:
      key - The key to retrieve.
      Returns:
      An Optional containing the value or Optional.empty()
    • getConsulResponseWithValue

      public Optional<ConsulResponse<Value>> getConsulResponseWithValue(String key)
      Retrieves a ConsulResponse with the Value for a specific key from the key/value store
      Parameters:
      key - The key to retrieve
      Returns:
      An Optional containing the ConsulResponse or Optional.empty()
    • getValue

      public Optional<Value> getValue(String key, QueryOptions queryOptions)
      Retrieves a Value for a specific key from the key/value store.

      GET /v1/kv/{key}

      Parameters:
      key - The key to retrieve.
      queryOptions - The query options.
      Returns:
      An Optional containing the value or Optional.empty()
    • getConsulResponseWithValue

      public Optional<ConsulResponse<Value>> getConsulResponseWithValue(String key, QueryOptions queryOptions)
      Returns a ConsulResponse for a specific key from the kv store. Contains the consul response headers along with the configuration value.

      GET /v1/kv/{key}

      Parameters:
      key - The key to retrieve.
      queryOptions - The query options.
      Returns:
      An Optional containing the ConsulResponse or Optional.empty()
    • getValue

      public void getValue(String key, QueryOptions queryOptions, ConsulResponseCallback<Optional<Value>> callback)
      Asynchronously retrieves a Value for a specific key from the key/value store.

      GET /v1/kv/{key}

      Parameters:
      key - The key to retrieve.
      queryOptions - The query options.
      callback - Callback implemented by callee to handle results.
    • getValues

      public List<Value> getValues(String key)
      Retrieves a list of Value objects for a specific key from the key/value store.

      GET /v1/kv/{key}?recurse

      Parameters:
      key - The key to retrieve.
      Returns:
      A list of zero to many Value objects.
    • getConsulResponseWithValues

      public ConsulResponse<List<Value>> getConsulResponseWithValues(String key)
      Retrieves a ConsulResponse with a list of Value objects along with consul response headers for a specific key from the key/value store.

      GET /v1/kv/{key}?recurse

      Parameters:
      key - The key to retrieve.
      Returns:
      A ConsulResponse with a list of zero to many Value objects and consul response headers.
    • getValues

      public List<Value> getValues(String key, QueryOptions queryOptions)
      Retrieves a list of Value objects for a specific key from the key/value store.

      GET /v1/kv/{key}?recurse

      Parameters:
      key - The key to retrieve.
      queryOptions - The query options.
      Returns:
      A list of zero to many Value objects.
    • getConsulResponseWithValues

      public ConsulResponse<List<Value>> getConsulResponseWithValues(String key, QueryOptions queryOptions)
      Retrieves a ConsulResponse with a list of Value objects along with consul response headers for a specific key from the key/value store.

      GET /v1/kv/{key}?recurse

      Parameters:
      key - The key to retrieve.
      queryOptions - The query options to use.
      Returns:
      A ConsulResponse with a list of zero to many Value objects and consul response headers.
    • getValues

      public void getValues(String key, QueryOptions queryOptions, ConsulResponseCallback<List<Value>> callback)
      Asynchronously retrieves a list of Value objects for a specific key from the key/value store.

      GET /v1/kv/{key}?recurse

      Parameters:
      key - The key to retrieve.
      queryOptions - The query options.
      callback - Callback implemented by callee to handle results.
    • getValueAsString

      public Optional<String> getValueAsString(String key)
      Retrieves a string value for a specific key from the key/value store.

      GET /v1/kv/{key}

      Parameters:
      key - The key to retrieve.
      Returns:
      An Optional containing the value as a string or Optional.empty()
    • getValueAsString

      public Optional<String> getValueAsString(String key, Charset charset)
      Retrieves a string value for a specific key from the key/value store.

      GET /v1/kv/{key}

      Parameters:
      key - The key to retrieve.
      charset - The charset of the value
      Returns:
      An Optional containing the value as a string or Optional.empty()
    • getValuesAsString

      public List<String> getValuesAsString(String key)
      Retrieves a list of string values for a specific key from the key/value store.

      GET /v1/kv/{key}?recurse

      Parameters:
      key - The key to retrieve.
      Returns:
      A list of zero to many string values.
    • getValuesAsString

      public List<String> getValuesAsString(String key, Charset charset)
      Retrieves a list of string values for a specific key from the key/value store.

      GET /v1/kv/{key}?recurse

      Parameters:
      key - The key to retrieve.
      charset - The charset of the value
      Returns:
      A list of zero to many string values.
    • putValue

      public boolean putValue(String key)
      Puts a null value into the key/value store.
      Parameters:
      key - The key to use as index.
      Returns:
      true if the value was successfully indexed.
    • putValue

      public boolean putValue(String key, String value)
      Puts a value into the key/value store.
      Parameters:
      key - The key to use as index.
      value - The value to index.
      Returns:
      true if the value was successfully indexed.
    • putValue

      public boolean putValue(String key, String value, Charset charset)
      Puts a value into the key/value store.
      Parameters:
      key - The key to use as index.
      value - The value to index.
      charset - the Charset to use
      Returns:
      true if the value was successfully indexed.
    • putValue

      public boolean putValue(String key, String value, long flags)
      Puts a value into the key/value store.
      Parameters:
      key - The key to use as index.
      value - The value to index.
      flags - The flags for this key.
      Returns:
      true if the value was successfully indexed.
    • putValue

      public boolean putValue(String key, String value, long flags, Charset charset)
      Puts a value into the key/value store.
      Parameters:
      key - The key to use as index.
      value - The value to index.
      flags - The flags for this key.
      charset - the Charset to use
      Returns:
      true if the value was successfully indexed.
    • putValue

      public boolean putValue(String key, String value, long flags, PutOptions putOptions)
      Puts a value into the key/value store.
      Parameters:
      key - The key to use as index.
      value - The value to index.
      flags - The flags for this key.
      putOptions - PUT options (e.g. wait, acquire).
      Returns:
      true if the value was successfully indexed.
    • putValue

      public boolean putValue(String key, String value, long flags, PutOptions putOptions, Charset charset)
      Puts a value into the key/value store.
      Parameters:
      key - The key to use as index.
      value - The value to index.
      flags - The flags for this key.
      putOptions - PUT options (e.g. wait, acquire).
      charset - the Charset to use
      Returns:
      true if the value was successfully indexed.
    • putValue

      public boolean putValue(String key, byte[] value, long flags, PutOptions putOptions)
      Puts a value into the key/value store.
      Parameters:
      key - The key to use as index.
      value - The value to index.
      flags - The flags for this key.
      putOptions - PUT options (e.g. wait, acquire).
      Returns:
      true if the value was successfully indexed.
    • getKeys

      public List<String> getKeys(String key)
      Retrieves a list of matching keys for the given key.

      GET /v1/kv/{key}?keys

      Parameters:
      key - The key to retrieve.
      Returns:
      A list of zero to many keys.
    • getKeys

      public List<String> getKeys(String key, QueryOptions queryOptions)
      Retrieves a list of matching keys for the given key.

      GET /v1/kv/{key}?keys

      Parameters:
      key - The key to retrieve.
      queryOptions - The query options.
      Returns:
      A list of zero to many keys.
    • getKeys

      public List<String> getKeys(String key, String separator)
      Retrieves a list of matching keys for the given key, limiting the prefix of keys returned, only up to the given separator.

      GET /v1/kv/{key}?keys&separator={separator}

      Parameters:
      key - The key to retrieve.
      separator - The separator used to limit the prefix of keys returned.
      Returns:
      A list of zero to many keys.
    • getKeys

      public List<String> getKeys(String key, String separator, QueryOptions queryOptions)
      Retrieves a list of matching keys for the given key.

      GET /v1/kv/{key}?keys&separator={separator}

      Parameters:
      key - The key to retrieve.
      separator - The separator used to limit the prefix of keys returned.
      queryOptions - The query options.
      Returns:
      A list of zero to many keys.
    • deleteKey

      public void deleteKey(String key)
      Deletes a specified key.

      DELETE /v1/kv/{key}

      Parameters:
      key - The key to delete.
    • deleteKeys

      public void deleteKeys(String key)
      Deletes a specified key and any below it.

      DELETE /v1/kv/{key}?recurse

      Parameters:
      key - The key to delete.
    • deleteKey

      public void deleteKey(String key, DeleteOptions deleteOptions)
      Deletes a specified key.

      DELETE /v1/kv/{key}

      Parameters:
      key - The key to delete.
      deleteOptions - DELETE options (e.g., recurse, cas)
    • acquireLock

      public boolean acquireLock(String key, String session)
      Acquire a lock for a given key.

      PUT /v1/kv/{key}?acquire={session}

      Parameters:
      key - The key to acquire the lock.
      session - The session to acquire lock.
      Returns:
      true if the lock is acquired successfully, false otherwise.
    • acquireLock

      public boolean acquireLock(String key, String value, String session)
      Acquire a lock for a given key.

      PUT /v1/kv/{key}?acquire={session}

      Parameters:
      key - The key to acquire the lock.
      session - The session to acquire lock.
      value - key value (usually application-specific info about the lock requester)
      Returns:
      true if the lock is acquired successfully, false otherwise.
    • getSession

      public Optional<String> getSession(String key)
      Retrieves a session string for a specific key from the key/value store.

      GET /v1/kv/{key}

      Parameters:
      key - The key to retrieve.
      Returns:
      An Optional containing the value as a string or Optional.empty()
    • releaseLock

      public boolean releaseLock(String key, String sessionId)
      Releases the lock for a given service and session.

      GET /v1/kv/{key}?release={sessionId}

      Parameters:
      key - identifying the service.
      sessionId - the session ID
      Returns:
      SessionInfo.
    • performTransaction

      public ConsulResponse<TxResponse> performTransaction(Operation... operations)
      Performs a Consul transaction.

      PUT /v1/tx

      Parameters:
      operations - A list of KV operations.
      Returns:
      A ConsulResponse containing results and potential errors.
    • performTransaction

      public ConsulResponse<TxResponse> performTransaction(TransactionOptions transactionOptions, Operation... operations)
      Performs a Consul transaction.

      PUT /v1/tx

      Parameters:
      transactionOptions - transaction options (e.g. dc, consistency).
      operations - A list of KV operations.
      Returns:
      A ConsulResponse containing results and potential errors.
    • getNetworkTimeoutConfig

      public Consul.NetworkTimeoutConfig getNetworkTimeoutConfig()
    • getConfig

      public ClientConfig getConfig()
    • getEventHandler

      public ClientEventHandler getEventHandler()