コード例 #1
0
        protected ApiRateLimit GetRateLimit(HttpResponseMessage response)
        {
            int  allowed   = int.Parse(response.Headers.GetValues("X-RateLimit-Limit").First());
            int  remaining = int.Parse(response.Headers.GetValues("X-RateLimit-Remaining").First());
            long reset     = long.Parse(response.Headers.GetValues("X-RateLimit-Reset").First());

            var rateLimit = new ApiRateLimit(allowed, remaining, reset);

            return(rateLimit);
        }
コード例 #2
0
        public static ApiRateLimit operator -(ApiRateLimit a, ApiRateLimit b)
        {
            var output = new ApiRateLimit();

            output.Daily   = a.Daily - b.Daily;
            output.Weekly  = a.Weekly - b.Weekly;
            output.Monthly = a.Monthly - b.Monthly;
            output.Name    = b.Name;
            return(output);
        }
コード例 #3
0
        public async Task <ApiSearchResponse <T> > SearchAsync <T>(string command, int pageNumber, IDictionary <string, string> parameters)
        {
            pageNumber = pageNumber < 1 ? 1 : pageNumber;
            pageNumber = pageNumber > 1000 ? 1000 : pageNumber;

            if (!parameters.Keys.Contains("page", StringComparer.OrdinalIgnoreCase))
            {
                parameters.Add("page", pageNumber.ToString());
            }

            using (HttpClient client = CreateClient())
            {
                string cmd = CreateCommand(command, parameters);

                HttpResponseMessage response = await client.GetAsync(cmd).ConfigureAwait(false);

                string json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                // rate limit will not exist if there is an error.
                if (!response.IsSuccessStatusCode)
                {
                    var error = new ApiSearchResponse <T>
                    {
                        // This will throw up if the error is page number = 0; the resultant json will be: {"errors":["page must be greater than 0"]}
                        // in other words, the json will not include a status_code. Asked the api devs and this is a known issue they are working on.
                        // What to do? Nothing really, the page guard at the top of the method will keep the page number > 0.
                        Error       = JsonConvert.DeserializeObject <ApiError>(json),
                        CommandText = response.RequestMessage.RequestUri.ToString(),
                        Json        = json,
                    };

                    return(error);
                }

                ApiRateLimit rateLimit = GetRateLimit(response);

                var result = JsonConvert.DeserializeObject <ApiSearchResponse <T> >(json, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });

                result.RateLimit   = rateLimit;
                result.CommandText = response.RequestMessage.RequestUri.ToString();
                result.Json        = json;

                return(result);
            }
        }