Skip to content
Snippets Groups Projects
Commit 8d987074 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs
Browse files

New: Functions with Waiting

parent 7904c080
No related branches found
No related tags found
2 merge requests!13Product/119 refine vue web part,!12Topic/132 config properties
...@@ -39,6 +39,24 @@ namespace Coscine.Configuration ...@@ -39,6 +39,24 @@ namespace Coscine.Configuration
} }
} }
public bool PutAndWait(string key, byte[] value)
{
var putPair = new KVPair(key)
{
Value = value
};
using (var client = new ConsulClient(Configuration))
{
var putAttempt = client.KV.Put(putPair);
putAttempt.Wait();
return putAttempt.Result.Response;
}
}
public async Task<byte[]> GetAsync(string key) public async Task<byte[]> GetAsync(string key)
{ {
using (var client = new ConsulClient(Configuration)) using (var client = new ConsulClient(Configuration))
...@@ -57,6 +75,26 @@ namespace Coscine.Configuration ...@@ -57,6 +75,26 @@ namespace Coscine.Configuration
} }
} }
public byte[] GetAndWait(string key)
{
using (var client = new ConsulClient(Configuration))
{
var getPair = client.KV.Get(key);
getPair.Wait();
if (getPair.Result.Response?.Value != null)
{
return getPair.Result.Response.Value;
}
else
{
return null;
}
}
}
public async Task<string> GetStringAsync(string key) public async Task<string> GetStringAsync(string key)
{ {
var value = await GetAsync(key); var value = await GetAsync(key);
...@@ -70,6 +108,19 @@ namespace Coscine.Configuration ...@@ -70,6 +108,19 @@ namespace Coscine.Configuration
} }
} }
public string GetStringAndWait(string key)
{
var value = GetAndWait(key);
if (value != null)
{
return Encoding.UTF8.GetString(value, 0, value.Length);
}
else
{
return null;
}
}
public async Task<string[]> KeysAsync(string prefix) public async Task<string[]> KeysAsync(string prefix)
{ {
using (var client = new ConsulClient(Configuration)) using (var client = new ConsulClient(Configuration))
...@@ -79,6 +130,16 @@ namespace Coscine.Configuration ...@@ -79,6 +130,16 @@ namespace Coscine.Configuration
} }
} }
public string[] KeysAndWait(string prefix)
{
using (var client = new ConsulClient(Configuration))
{
var keys = client.KV.Keys(prefix);
keys.Wait();
return keys.Result.Response;
}
}
public async Task<bool> DeleteAsync(string key) public async Task<bool> DeleteAsync(string key)
{ {
using (var client = new ConsulClient(Configuration)) using (var client = new ConsulClient(Configuration))
...@@ -88,6 +149,16 @@ namespace Coscine.Configuration ...@@ -88,6 +149,16 @@ namespace Coscine.Configuration
} }
} }
public bool DeleteAndWait(string key)
{
using (var client = new ConsulClient(Configuration))
{
var deleteRequest = client.KV.Delete(key);
deleteRequest.Wait();
return deleteRequest.Result.Response;
}
}
public bool Put(string key, string value) public bool Put(string key, string value)
{ {
return PutAsync(key, value).GetAwaiter().GetResult(); return PutAsync(key, value).GetAwaiter().GetResult();
......
...@@ -34,6 +34,11 @@ namespace Coscine.Configuration ...@@ -34,6 +34,11 @@ namespace Coscine.Configuration
return true; return true;
} }
public bool DeleteAndWait(string key)
{
return Delete(key);
}
public Task<bool> DeleteAsync(string key) public Task<bool> DeleteAsync(string key)
{ {
return Task.Factory.StartNew(() => return Task.Factory.StartNew(() =>
...@@ -55,6 +60,11 @@ namespace Coscine.Configuration ...@@ -55,6 +60,11 @@ namespace Coscine.Configuration
} }
} }
public byte[] GetAndWait(string key)
{
return Get(key);
}
public Task<byte[]> GetAsync(string key) public Task<byte[]> GetAsync(string key)
{ {
return Task.Factory.StartNew(() => return Task.Factory.StartNew(() =>
...@@ -75,6 +85,11 @@ namespace Coscine.Configuration ...@@ -75,6 +85,11 @@ namespace Coscine.Configuration
} }
} }
public string GetStringAndWait(string key)
{
return GetString(key);
}
public Task<string> GetStringAsync(string key) public Task<string> GetStringAsync(string key)
{ {
return Task.Factory.StartNew(() => return Task.Factory.StartNew(() =>
...@@ -97,6 +112,11 @@ namespace Coscine.Configuration ...@@ -97,6 +112,11 @@ namespace Coscine.Configuration
return filteredKeys.ToArray(); return filteredKeys.ToArray();
} }
public string[] KeysAndWait(string prefix)
{
return Keys(prefix);
}
public Task<string[]> KeysAsync(string prefix) public Task<string[]> KeysAsync(string prefix)
{ {
return Task.Factory.StartNew(() => return Task.Factory.StartNew(() =>
...@@ -123,6 +143,11 @@ namespace Coscine.Configuration ...@@ -123,6 +143,11 @@ namespace Coscine.Configuration
return Put(key, new System.Text.ASCIIEncoding().GetString(value)); return Put(key, new System.Text.ASCIIEncoding().GetString(value));
} }
public bool PutAndWait(string key, byte[] value)
{
return Put(key, value);
}
public Task<bool> PutAsync(string key, string value) public Task<bool> PutAsync(string key, string value)
{ {
return Task.Factory.StartNew(() => return Task.Factory.StartNew(() =>
......
...@@ -8,14 +8,24 @@ namespace Coscine.Configuration ...@@ -8,14 +8,24 @@ namespace Coscine.Configuration
Task<bool> PutAsync(string key, byte[] value); Task<bool> PutAsync(string key, byte[] value);
bool PutAndWait(string key, byte[] value);
Task<byte[]> GetAsync(string key); Task<byte[]> GetAsync(string key);
byte[] GetAndWait(string key);
Task<string> GetStringAsync(string key); Task<string> GetStringAsync(string key);
string GetStringAndWait(string key);
Task<string[]> KeysAsync(string prefix); Task<string[]> KeysAsync(string prefix);
string[] KeysAndWait(string prefix);
Task<bool> DeleteAsync(string key); Task<bool> DeleteAsync(string key);
bool DeleteAndWait(string key);
bool Put(string key, string value); bool Put(string key, string value);
bool Put(string key, byte[] value); bool Put(string key, byte[] value);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment