public async Task StoreAsync <T>(string key, T value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key MUST have a value");
            }

            string serializedValue   = JsonConvert.SerializeObject(value);
            string generatedStoreKey = GenerateStoredKey(key, typeof(T));

            var item = new DataStoreItem()
            {
                PartitionKey = PARTITION_NAME,
                RowKey       = generatedStoreKey,
                Value        = serializedValue
            };

            TableOperation insertOperation = TableOperation.InsertOrMerge(item);
            TableResult    result          = await _table.ExecuteAsync(insertOperation);

            if (result.HttpStatusCode < (int)HttpStatusCode.OK || result.HttpStatusCode > (int)HttpStatusCode.MultipleChoices)
            {
                throw new Exception($"[{result.HttpStatusCode}] Failed to insert record at {TABLE_NAME} {item.RowKey}");
            }

            await Task.CompletedTask;
        }
Example #2
0
        public async Task <string> GetSecret()
        {
            var retrieveOperation = TableOperation.Retrieve <DataStoreItem>(PARTITION_NAME, "0");
            var retrievedResult   = await cloudTable.ExecuteAsync(retrieveOperation);

            DataStoreItem item = (DataStoreItem)retrievedResult.Result;

            return(item.Value);
        }
        public async Task DeleteAsync <T>(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key MUST have a value");
            }

            DataStoreItem item = await GetAsync <DataStoreItem>(key);

            var deleteOperation = TableOperation.Delete(item);
            await _table.ExecuteAsync(deleteOperation);
        }
        public async Task <T> GetAsync <T>(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key MUST have a value");
            }

            string generatedStoreKey = GenerateStoredKey(key, typeof(T));

            TableOperation retrieveOperation = TableOperation.Retrieve <DataStoreItem>(PARTITION_NAME, generatedStoreKey);
            TableResult    retrievedResult   = await _table.ExecuteAsync(retrieveOperation);

            DataStoreItem item = (DataStoreItem)retrievedResult.Result;

            T value = item == null ? default(T) : JsonConvert.DeserializeObject <T>(item.Value);

            return(value);
        }