Q42.RijksmuseumApi.Models.CollectionSearchRequest.ToString C# (CSharp) Method

ToString() public method

Generates querystring
public ToString ( ) : string
return string
    public override string ToString()
    {
      StringBuilder sb = new StringBuilder();

      if (!string.IsNullOrEmpty(SearchQuery))
        sb.AppendFormat("q={0}&", SearchQuery);

      if (!string.IsNullOrEmpty(Maker))
        sb.AppendFormat("maker={0}&", Maker);

      if (!string.IsNullOrEmpty(Type))
        sb.AppendFormat("type={0}&", Type);

      if (!string.IsNullOrEmpty(Material))
        sb.AppendFormat("material={0}&", Material);

      if (!string.IsNullOrEmpty(Technique))
        sb.AppendFormat("technique={0}&", Technique);

      if (DatingPeriod.HasValue)
        sb.AppendFormat("f.dating.period={0}&", DatingPeriod.Value);

      if (!string.IsNullOrEmpty(HexColor))
        sb.AppendFormat("f.normalized32Colors.hex={0}&", HexColor);

      if (ImageOnly.HasValue)
      {
        if(ImageOnly.Value)
          sb.Append("imgonly=True&");
        else
          sb.Append("imgonly=False&");

      }

      if (TopPiecesOnly.HasValue)
      {
        if (TopPiecesOnly.Value)
          sb.Append("toppieces=True&");
        else
          sb.Append("toppieces=False&");

      }

      //Remove last &
      string result = sb.ToString().TrimEnd(new List<char>() { '&' }.ToArray());

      return result;

    }

Usage Example

Exemplo n.º 1
0
    /// <summary>
    /// https://www.rijksmuseum.nl/api/nl/collection?key=fakekey&format=json
    /// </summary>
    /// <returns></returns>
    public async Task<CollectionSearchResponse> GetCollection(CollectionSearchRequest search, string sort = "relevance", int page = 0, int pageSize = 10)
    {
      if (search == null)
        throw new ArgumentNullException(nameof(search));

      //Create URL
      Uri uri = new Uri(string.Format("{0}{1}/collection?s={2}&p={3}&ps={4}&{5}&{6}", _apiBase, _language, sort, page, pageSize, _queryStringApiKeyFormat, search.ToString()));

      //Do HTTP Request
      HttpClient client = new HttpClient();
      string stringResult = await client.GetStringAsync(uri).ConfigureAwait(false);

      //Parse JSON
      var result = JsonConvert.DeserializeObject<CollectionSearchResponse>(stringResult);

      return result;
    }
CollectionSearchRequest