{"id":2672,"date":"2019-10-15T08:58:16","date_gmt":"2019-10-15T07:58:16","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2672"},"modified":"2022-07-05T10:07:49","modified_gmt":"2022-07-05T09:07:49","slug":"querybuilder","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/querybuilder\/","title":{"rendered":"UrlQueryBuilder"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System.Web;\n\npublic class HttpHelper\n{\n\tpublic static string UrlDecode(string input) => HttpUtility.UrlDecode(input);\n\tpublic static string UrlEncode(string input) => HttpUtility.UrlEncode(input);\n}\npublic class UrlQueryBuilder\n{\n\tprivate IDictionary&lt;string, string> qry = new Dictionary&lt;string, string>();\n\tpublic UrlQueryBuilder Set(string key, object value)\n\t{\n\t\tif (value is DateTime)\n\t\t\tvalue = ((DateTime)value).ToString(\"yyyy-MM-dd HH:mm:ss\");\n\t\tqry[key] = value.ToString();\n\t\treturn this;\n\t}\n\tpublic string Build()\n\t{\n\t\tif (!qry.Any()) return string.Empty;\n\t\treturn string.Join(\"&amp;\", qry.Select(x => string.Join(\"=\", x.Key, HttpHelper.UrlEncode(x.Value))));\n\t}\n}\n\nprivate RestApiClient GetRestApiClient(string apiKey)\n{\n\tstring userName = apiKey;\n\tstring password = \"\";\n\tstring webserviceApiUrl = \"https:\/\/test.com\/api\/\";\n\tRestApiClient client = new RestApiClient(webserviceApiUrl, new NetworkCredential(userName, password));\n\treturn client;\n}\n\nprivate async Task CheckRateLimit(HttpResponseMessage response)\n{\n\tif (response.Headers.Contains(\"X-RateLimit-Remaining\"))\n\t{\n\t\tvar rateLimit = int.Parse(response.Headers.GetValues(\"X-RateLimit-Limit\").FirstOrDefault());\n\t\tvar rateLimitRemaining = int.Parse(response.Headers.GetValues(\"X-RateLimit-Remaining\").FirstOrDefault());\n\t\tif (rateLimitRemaining &lt; 10)\n\t\t\tawait Task.Delay(5000); \/\/ wait some time to prevent hitting the limit\n\t}\n}\n\nprivate async Task&lt;T> GetSingle&lt;T>(string apiKey, string path)\n{\n\tusing (var client = GetRestApiClient(apiKey))\n\t{\n\t\tvar response = await client.Get&lt;T>($\"{path}\");\n\t\tawait CheckRateLimit(response.Response);\n\t\treturn response.Result;\n\t}\n}\n\nprivate async Task&lt;IEnumerable&lt;T>> GetPaged&lt;T>(string apiKey, string path, QueryBuilder query = null)\n{\n\tif (query == null)\n\t\tquery = new QueryBuilder();\n\tusing (var client = GetRestApiClient(apiKey))\n\t{\n\t\tvar allItems = new List&lt;T>();\n\t\tvar page = 0;\n\t\tvar pageSize = 100;\n\t\tvar hasMore = true;\n\t\twhile (hasMore)\n\t\t{\n\t\t\tvar offset = page * pageSize;\n\t\t\tif (offset > 0)\n\t\t\t\tquery.Set(\"offset\", offset);\n\t\t\tvar response = await client.Get&lt;IList&lt;T>>($\"{path}?{query.Build()}\");\n\t\t\tawait CheckRateLimit(response.Response);\n\t\t\thasMore = response.Result.Count == pageSize;\n\t\t\tallItems.AddRange(response.Result);\n\t\t\tpage++;\n\t\t}\n\t\treturn allItems;\n\t}\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2672","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2672"}],"version-history":[{"count":5,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2672\/revisions"}],"predecessor-version":[{"id":6363,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2672\/revisions\/6363"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}