예제 #1
0
        internal Limitation(HttpResponseMessage response)
            : base()
        {
            this.IsLimited = false;

            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            if (response.Headers.Contains("x-ratelimit-limit"))
            {
                this.Maximum = Convert.ToInt32(response.Headers.GetValues("x-ratelimit-limit").FirstOrDefault());
            }

            if (response.Headers.Contains("x-ratelimit-remaining"))
            {
                this.Remaining = Convert.ToInt32(response.Headers.GetValues("x-ratelimit-remaining").FirstOrDefault());

                if (this.Remaining <= 0)
                {
                    this.IsLimited  = true;
                    this.limitation = this.GetLimitationBody(response);
                }
            }

            if (response.Headers.Contains("x-ratelimit-reset"))
            {
                this.Expiration = DateTimeOffset.FromUnixTimeSeconds(Convert.ToInt64(response.Headers.GetValues("x-ratelimit-reset").FirstOrDefault()));
            }
        }
예제 #2
0
        private LimitationBody GetLimitationBody(HttpResponseMessage response)
        {
            LimitationBody result = null;

            try
            {
                Task.Run(async() =>
                {
                    JsonSerializerSettings settings = new JsonSerializerSettings()
                    {
                        MissingMemberHandling = MissingMemberHandling.Ignore,
                    };
                    result = JsonConvert.DeserializeObject <LimitationBody>(await response.Content.ReadAsStringAsync(), settings);
                })
                .Wait();
            }
            catch (Exception exception)
            {
                System.Diagnostics.Debug.WriteLine(exception);
            }

            return(result);
        }