/** * Retrieves a string value for a specific key from the key/value store. * <p> * GET /v1/keyValueStore/{key} * * @param key The key to retrieve. * @return An {@link Optional} containing the value as a string or * {@link java.util.Optional#empty()} */ public Optional<String> getValueAsString(String key) { Optional<KeyValue> value = getValue(key); return value.isPresent() ? Optional.of(RequestUtils.decodeBase64(value.get().getValue())) : Optional.<String>empty(); }
private Optional<KeyValue> getKeyValueOptional(HTTP.Response httpResponse) { final List<KeyValue> keyValues = fromJsonArray(httpResponse.body(), KeyValue.class); keyValues.forEach(keyValue -> keyValue.setValue(decodeBase64(keyValue.getValue()))); return keyValues != null && keyValues.size() > 0 ? Optional.of(keyValues.get(0)) : Optional.<KeyValue>empty(); }
@Test public void putAndReceiveValue() throws UnknownHostException { Consul client = Consul.consul(); KeyValueStoreEndpoint keyValueStore = client.keyValueStore(); String key = UUID.randomUUID().toString(); String value = UUID.randomUUID().toString(); assertTrue(keyValueStore.putValue(key, value)); KeyValue received = keyValueStore.getValue(key).get(); assertEquals(value, decodeBase64(received.getValue())); assertEquals(0L, received.getFlags()); }
@Test public void putAndReceiveWithFlags() throws UnknownHostException { Consul client = Consul.consul(); KeyValueStoreEndpoint keyValueStore = client.keyValueStore(); String key = UUID.randomUUID().toString(); String value = UUID.randomUUID().toString(); long flags = Long.MAX_VALUE; assertTrue(keyValueStore.putValue(key, value, flags)); KeyValue received = keyValueStore.getValue(key).get(); assertEquals(value, decodeBase64(received.getValue())); assertEquals(flags, received.getFlags()); }
/** * Retrieves a list of string values for a specific key from the key/value * store. * <p> * GET /v1/keyValueStore/{key}?recurse * * @param key The key to retrieve. * @return A list of zero to many string values. */ public List<String> getValuesAsString(String key) { @SuppressWarnings("UnnecessaryLocalVariable") List<String> result = getValues(key).stream().map(keyValue -> RequestUtils.decodeBase64(keyValue.getValue())).collect(Collectors.toList()); return result; }
/** * Retrieves a string value for a specific key from the key/value store. * <p> * GET /v1/keyValueStore/{key} * * @param key The key to retrieve. * @return An {@link Optional} containing the value as a string or * {@link java.util.Optional#empty()} */ public Optional<String> getValueAsString(String key) { Optional<KeyValue> value = getValue(key); return value.isPresent() ? Optional.of(RequestUtils.decodeBase64(value.get().getValue())) : Optional.<String>empty(); }
/** * Retrieves a list of string values for a specific key from the key/value * store. * <p> * GET /v1/keyValueStore/{key}?recurse * * @param key The key to retrieve. * @return A list of zero to many string values. */ public List<String> getValuesAsString(String key) { @SuppressWarnings("UnnecessaryLocalVariable") List<String> result = getValues(key).stream().map(keyValue -> RequestUtils.decodeBase64(keyValue.getValue())).collect(Collectors.toList()); return result; }