/// <inheritdoc /> public virtual async Task <List <T> > SearchAsync <T>(Wallet wallet, SearchRecordQuery query, SearchRecordOptions options, int count) where T : WalletRecord, new() { using (var search = await NonSecrets.OpenSearchAsync(wallet, new T().GetTypeName(), (query ?? new SearchRecordQuery()).ToJson(), (options ?? new SearchRecordOptions()).ToJson())) { var result = JsonConvert.DeserializeObject <SearchRecordResult>(await search.NextAsync(wallet, count)); // TODO: Add support for pagination return(result.Records? .Select(x => { var record = JsonConvert.DeserializeObject <T>(x.Value); record.Tags.Clear(); foreach (var tag in x.Tags) { record.Tags.Add(tag.Key, tag.Value); } return record; }) .ToList() ?? new List <T>()); } }
public async Task TestWalletSearchWorksForQuery() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tags); await NonSecrets.AddRecordAsync(wallet, type, id2, value2, tags2); await NonSecrets.AddRecordAsync(wallet, type, id3, value2, tags3); var query = "{\"tagName1\":\"str2\"}"; using (var search = await NonSecrets.OpenSearchAsync(wallet, type, query, optionsEmpty)) { var searchRecordsJson = await search.NextAsync(wallet, 3); var searchRecords = JObject.Parse(searchRecordsJson); var records = (JArray)searchRecords["records"]; Assert.AreEqual(1, records.Count); var expected = JObject.FromObject(new { id = id2, type = (object)null, value = value2, tags = (object)null }); Assert.IsTrue(JValue.DeepEquals(expected, records[0])); } }
/// <inheritdoc /> public virtual async Task <List <T> > SearchAsync <T>(Wallet wallet, ISearchQuery query, SearchOptions options, int count, int skip) where T : RecordBase, new() { using (var search = await NonSecrets.OpenSearchAsync(wallet, new T().TypeName, (query ?? SearchQuery.Empty).ToJson(), (options ?? new SearchOptions()).ToJson())) { if (skip > 0) { await search.NextAsync(wallet, skip); } var result = JsonConvert.DeserializeObject <SearchResult>(await search.NextAsync(wallet, count), _jsonSettings); return(result.Records? .Select(x => { var record = JsonConvert.DeserializeObject <T>(x.Value, _jsonSettings); foreach (var tag in x.Tags) { record.Tags[tag.Key] = tag.Value; } return record; }) .ToList() ?? new List <T>()); } }
/// <inheritdoc /> public virtual async Task <bool> DeleteAsync <T>(Wallet wallet, string id) where T : RecordBase, new() { try { var record = await GetAsync <T>(wallet, id); var typeName = new T().TypeName; await NonSecrets.DeleteRecordTagsAsync( wallet: wallet, type : typeName, id : id, tagsJson : record.Tags.Select(x => x.Key).ToArray().ToJson()); await NonSecrets.DeleteRecordAsync( wallet : wallet, type : typeName, id : id); return(true); } catch (Exception e) { Debug.WriteLine($"Couldn't delete record: {e}"); return(false); } }
/// <inheritdoc /> public virtual async Task <T> GetAsync <T>(Wallet wallet, string id) where T : WalletRecord, new() { try { var recordJson = await NonSecrets.GetRecordAsync(wallet, new T().GetTypeName(), id, new SearchRecordOptions().ToJson()); if (recordJson == null) { return(null); } var item = JsonConvert.DeserializeObject <SearchRecordItem>(recordJson); var record = JsonConvert.DeserializeObject <T>(item.Value); record.Tags.Clear(); foreach (var tag in item.Tags) { record.Tags.Add(tag.Key, tag.Value); } return(record); } catch (WalletItemNotFoundException) { return(null); } }
/// <inheritdoc /> public virtual async Task <T> GetAsync <T>(Wallet wallet, string id) where T : RecordBase, new() { try { var recordJson = await NonSecrets.GetRecordAsync(wallet, new T().TypeName, id, new SearchOptions().ToJson()); if (recordJson == null) { return(null); } var item = JsonConvert.DeserializeObject <SearchItem>(recordJson, _jsonSettings); var record = JsonConvert.DeserializeObject <T>(item.Value, _jsonSettings); foreach (var tag in item.Tags) { record.Tags[tag.Key] = tag.Value; } return(record); } catch (WalletItemNotFoundException) { return(null); } }
public static async Task Execute() { Console.Write("Executing non-secrets sample... "); var myWalletConfig = "{\"id\":\"my_wallet\"}"; var myWalletCredentials = "{\"key\":\"my_wallet_key\"}"; try { // Create and Open First Wallet await WalletUtils.CreateWalletAsync(myWalletConfig, myWalletCredentials); using (var myWallet = await Wallet.OpenWalletAsync(myWalletConfig, myWalletCredentials)) { var id = "myRecordId"; var value = "myRecordValue"; var type = "record_type"; var tagsJson = JsonConvert.SerializeObject(new { tagName = "tagValue", tagName2 = "tagValue2" }); var queryJson = JsonConvert.SerializeObject(new { tagName = "tagValue" }); // Add a new record to the wallet await NonSecrets.AddRecordAsync(myWallet, type, id, value, tagsJson); // Retrieve the record by type and id var recordJson = await NonSecrets.GetRecordAsync(myWallet, type, id, "{}"); var record = JObject.Parse(recordJson); Debug.Assert(record["id"].ToObject <string>() == id); Debug.Assert(record["value"].ToObject <string>() == value); // Open wallet search inside using statement to properly dispose and close the search handle using (var walletSearch = await NonSecrets.OpenSearchAsync(myWallet, type, queryJson, "{}")) { // Invoke fetch next records var searchJson = await walletSearch.NextAsync(myWallet, 5); var search = JObject.Parse(searchJson); // There should be one record returned Debug.Assert(search["records"].ToObject <JObject[]>().Length == 1); } // Close wallets await myWallet.CloseAsync(); } Console.WriteLine("OK", Color.Green); } catch (Exception e) { Console.WriteLine($"Error: {e.Message}", Color.Red); } finally { // Delete wallets await WalletUtils.DeleteWalletAsync(myWalletConfig, myWalletCredentials); } }
public async Task TestAddRecordWorksForDuplicate() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tagsEmpty); var ex = await Assert.ThrowsExceptionAsync <WalletItemAlreadyExistsException>(() => NonSecrets.AddRecordAsync(wallet, type, id, value, tagsEmpty) ); }
/// <inheritdoc /> public virtual Task AddAsync <T>(Wallet wallet, T record) where T : WalletRecord, new() { return(NonSecrets.AddRecordAsync(wallet, record.GetTypeName(), record.GetId(), record.ToJson(), record.Tags.ToJson())); }
public async Task TestUpdateRecordValueWorks() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tagsEmpty); await CheckRecordFieldAsync(wallet, type, id, "value", value); await NonSecrets.UpdateRecordValueAsync(wallet, type, id, value2); await CheckRecordFieldAsync(wallet, type, id, "value", value2); }
public async Task TestUpdateRecordTagsWorks() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tagsEmpty); await CheckRecordFieldAsync(wallet, type, id, "tags", tagsEmpty); await NonSecrets.UpdateRecordTagsAsync(wallet, type, id, tags); await CheckRecordFieldAsync(wallet, type, id, "tags", tags); }
public async Task TestDeleteRecordWorksForTwice() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tags); await NonSecrets.DeleteRecordAsync(wallet, type, id); var ex = await Assert.ThrowsExceptionAsync <WalletItemNotFoundException>(() => NonSecrets.DeleteRecordAsync(wallet, type, id) ); }
public async Task TestBuildAttribRequestWorksForRawData() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tagsEmpty); await CheckRecordFieldAsync(wallet, type, id, "tags", tagsEmpty); await NonSecrets.AddRecordTagsAsync(wallet, type, id, tags); await CheckRecordFieldAsync(wallet, type, id, "tags", tags); }
public async Task TestDeleteRecordTagsWorksForDeleteAll() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tags); await CheckRecordFieldAsync(wallet, type, id, "tags", tags); await NonSecrets.DeleteRecordTagsAsync(wallet, type, id, "[\"tagName1\", \"tagName2\", \"tagName3\"]"); await CheckRecordFieldAsync(wallet, type, id, "tags", tagsEmpty); }
public async Task deleteRecord(string type, string id) { try { await NonSecrets.DeleteRecordAsync(d_openWallet, type, id); } catch (Exception e) { Console.WriteLine($"Error: {e.Message}"); } }
/// <inheritdoc /> public virtual Task AddAsync <T>(Wallet wallet, T record) where T : RecordBase, new() { record.CreatedAtUtc = DateTime.UtcNow; return(NonSecrets.AddRecordAsync(wallet, record.TypeName, record.Id, record.ToJson(_jsonSettings), record.Tags.ToJson())); }
public async Task TestDeleteRecordTagsWorks() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tags); await CheckRecordFieldAsync(wallet, type, id, "tags", tags); await NonSecrets.DeleteRecordTagsAsync(wallet, type, id, "[\"tagName1\"]"); var expectedTags = "{\"tagName2\": \"5\", \"tagName3\": \"12\"}"; await CheckRecordFieldAsync(wallet, type, id, "tags", expectedTags); }
/// <inheritdoc /> public virtual async Task UpdateAsync <T>(Wallet wallet, T record) where T : WalletRecord, new() { await NonSecrets.UpdateRecordValueAsync(wallet, record.GetTypeName(), record.GetId(), record.ToJson()); await NonSecrets.UpdateRecordTagsAsync(wallet, record.GetTypeName(), record.GetId(), record.Tags.ToJson()); }
/// <inheritdoc /> public virtual Task AddAsync <T>(Wallet wallet, T record) where T : RecordBase, new() { Debug.WriteLine($"Adding record of type {record.TypeName} with Id {record.Id}"); SetCreatedAtUtc(record); return(NonSecrets.AddRecordAsync(wallet, record.TypeName, record.Id, Base64Encode(record.ToJson(_jsonSettings)), GetTags(record).ToJson())); }
/// <inheritdoc /> public virtual Task AddAsync <T>(Wallet wallet, T record) where T : RecordBase, new() { Debug.WriteLine($"Adding record of type {record.TypeName} with Id {record.Id}"); record.CreatedAtUtc = DateTime.UtcNow; return(NonSecrets.AddRecordAsync(wallet, record.TypeName, record.Id, record.ToJson(_jsonSettings), record.Tags.ToJson())); }
public async Task updateRecordTag(string type, string id, string tagJson) { try { await NonSecrets.UpdateRecordTagsAsync(d_openWallet, type, id, tagJson); } catch (Exception e) { Console.WriteLine($"Error: {e.Message}"); } }
/// <inheritdoc /> public virtual async Task UpdateAsync(Wallet wallet, RecordBase record) { record.UpdatedAtUtc = DateTime.UtcNow; await NonSecrets.UpdateRecordValueAsync(wallet, record.TypeName, record.Id, record.ToJson(_jsonSettings)); await NonSecrets.UpdateRecordTagsAsync(wallet, record.TypeName, record.Id, record.Tags.ToJson(_jsonSettings)); }
/// <inheritdoc /> public virtual async Task UpdateAsync(Wallet wallet, RecordBase record) { SetUpdatedAtUtc(record); await NonSecrets.UpdateRecordValueAsync(wallet, record.TypeName, record.Id, Base64Encode(record.ToJson(_jsonSettings))); await NonSecrets.UpdateRecordTagsAsync(wallet, record.TypeName, record.Id, GetTags(record).ToJson(_jsonSettings)); }
/// <inheritdoc /> public virtual async Task <bool> DeleteAsync <T>(Wallet wallet, string id) where T : RecordBase, new() { try { await NonSecrets.DeleteRecordAsync(wallet, new T().TypeName, id); return(true); } catch (WalletItemNotFoundException) { return(false); } }
public async Task <string> addRecord(string type, string id, string value, string tagsJson) { try { await NonSecrets.AddRecordAsync( d_openWallet, type, id, value, tagsJson); return("succes!"); } catch (Exception e) { return($"Error: {e.Message}"); } }
public async Task TestGetRecordWorksForDefaultOptions() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tags); var recordJson = await NonSecrets.GetRecordAsync(wallet, type, id, optionsEmpty); var actual = JObject.Parse(recordJson); var expected = JObject.FromObject(new { id = id, type = (object)null, value = value, tags = (object)null }); Assert.IsTrue(JValue.DeepEquals(expected, actual)); }
public async Task TestAddRecordTagsWorksForTwice() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tagsEmpty); await CheckRecordFieldAsync(wallet, type, id, "tags", tagsEmpty); var tags1 = "{\"tagName1\": \"str1\"}"; await NonSecrets.AddRecordTagsAsync(wallet, type, id, tags1); await CheckRecordFieldAsync(wallet, type, id, "tags", tags1); var tags2 = "{\"tagName2\": \"str2\"}"; await NonSecrets.AddRecordTagsAsync(wallet, type, id, tags2); var expectedTags = "{\"tagName1\":\"str1\",\"tagName2\":\"str2\"}"; await CheckRecordFieldAsync(wallet, type, id, "tags", expectedTags); }
public async Task TestGetRecordWorksForFullData() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tags); var optionsJson = JsonConvert.SerializeObject( new { retrieveType = true, retrieveValue = true, retrieveTags = true } ); var recordJson = await NonSecrets.GetRecordAsync(wallet, type, id, optionsJson); var record = JObject.Parse(recordJson); Assert.AreEqual(id, record["id"]); Assert.AreEqual(type, record["type"]); Assert.AreEqual(value, record["value"]); Assert.IsTrue(JValue.DeepEquals(JObject.Parse(tags), JObject.Parse(record["tags"].ToString()))); }
public async Task TestWalletSearchWorksForOptions() { await NonSecrets.AddRecordAsync(wallet, type, id, value, tags); var options = JsonConvert.SerializeObject( new { retrieveRecords = true, retrieveTotalCount = false, retrieveType = false, retrieveValue = false, retrieveTags = false } ); using (var search = await NonSecrets.OpenSearchAsync(wallet, type, queryEmpty, options)) { var searchRecordsJson = await search.NextAsync(wallet, 1); var searchRecords = JObject.Parse(searchRecordsJson); var records = (JArray)searchRecords["records"]; Assert.AreEqual(1, records.Count); var expected = JObject.FromObject( new { id = id, type = (object)null, value = (object)null, tags = (object)null } ); Assert.IsTrue(JValue.DeepEquals(expected, records[0])); } }
protected async Task CheckRecordFieldAsync(Wallet wallet, string type, string id, string field, string expectedValue) { string optionsFull = "{\"retrieveType\":true, \"retrieveValue\":true, \"retrieveTags\":true}"; string recordJson = await NonSecrets.GetRecordAsync(wallet, type, id, optionsFull); var record = JObject.Parse(recordJson); switch (field) { case "value": Assert.AreEqual(expectedValue, record["value"].ToString()); break; case "tags": var expected = JObject.Parse(expectedValue); Assert.IsTrue(JValue.DeepEquals(expected, JObject.Parse(record["tags"].ToString()))); break; default: Assert.IsTrue(false); break; } }