/// <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>()); } }
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 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])); } }
public async Task <JArray> getRecordArray(string type, string queryJson, string optionsJson) { try { var list = await NonSecrets.OpenSearchAsync( d_openWallet, type, queryJson, optionsJson); // get 0 schema's var res = await NonSecrets.FetchNextRecordsAsync( d_openWallet, list, 0); // parse result to see the count of schema's JObject o = JObject.Parse(res); string count = o["totalCount"].ToString(); // return "0" if there are no records for the type and query if (count == "0") { return(null); } // get count schema's res = await NonSecrets.FetchNextRecordsAsync( d_openWallet, list, Int32.Parse(count)); // make response human readable o = JObject.Parse(res); return((JArray)o["records"]); } catch (Exception e) { throw e; } }