public async Task <FdaLabelSearchResults> SearchAsync(FdaLabelSearchOptions fdaLabelSearchOptions, PagingSortingOptions pagingOptions = null, string joiner = "AND") { if (pagingOptions == null) { pagingOptions = new PagingSortingOptions(); } var parameterDictionary = new ParameterDictionary { { "search", fdaLabelSearchOptions.ToQuery(joiner) } }; if (pagingOptions.Limit != 0) { parameterDictionary.Add("limit", pagingOptions.Limit.ToString()); } if (pagingOptions.Skip != 0) { parameterDictionary.Add("skip", pagingOptions.Skip.ToString()); } if (!string.IsNullOrEmpty(_apiKey)) { parameterDictionary.Add("api_key", _apiKey); } var json = await GetAsync($"/drug/label.json?{parameterDictionary.ToQueryString()}"); return(FdaLabelSearchResults.FromJson(json)); }
/// <summary> /// /// </summary> /// <param name="fdaLabelSearchOptions"></param> /// <param name="joiner">Supports "AND" or "OR"</param> /// <returns></returns> public static string ToQuery(this FdaLabelSearchOptions fdaLabelSearchOptions, string joiner = "AND") //todo create complex query search { var search = new StringBuilder(); fdaLabelSearchOptions.GetType().GetProperties().ToList().ForEach(x => { var value = x.GetValue(fdaLabelSearchOptions); if (value == null) { return; } var propertyAttribute = (JsonPropertyAttribute)x.GetCustomAttributes().FirstOrDefault(z => (Type)z.TypeId == typeof(JsonPropertyAttribute)); if (propertyAttribute == null) { return; } if (value.GetType() == typeof(List <string>)) { search.Append( string.Join("+", (value as List <string> ?? new List <string>()) .Select(y => $"{propertyAttribute.PropertyName}:\"{y}\"") ) ); } else { search.Append($"{propertyAttribute.PropertyName}:\"{value}\""); } search.Append($"+{joiner}+"); }); return(search.ToString().Trim($"+{joiner}+".ToCharArray())); }