Example #1
0
        public async Task <Optional <List <Question> > > GetSince(long unixEpoch, int page, string site, string[] tags)
        {
            var throttle = _store.Read <StackexchangeThrottle>("StackexchangeThrottle");

            if (Unthrottled(throttle))
            {
                if (UnixEpoch.Now >= throttle.QuotaResetTime)
                {
                    throttle.QuotaResetTime = StackexchangeThrottle.UNKNOWN;
                }
                string url     = CreateUrl(unixEpoch, page, site, tags);
                var    results = await _web.Request(url);

                var wrapper = JsonConvert.DeserializeObject <StackexchangeWrapper>(Encoding.UTF8.GetString(Gzip.Decompress(results.Bytes)));
                if (results.StatusCode == HttpStatusCode.OK)
                {
                    UpdateThrottle(throttle, wrapper);
                    _store.Write("StackexchangeThrottle", throttle);
                    return(new Optional <List <Question> >(wrapper.Items.ToList()));
                }
                else
                {
                    throttle.BackoffUntil = ForceWait2Hours();
                    _store.Write("StackexchangeThrottle", throttle);
                }
            }
            return(new Optional <List <Question> >());
        }
        public Optional <string> IfICanNotMakeRequestsWhy()
        {
            var throttle = _store.Read <StackexchangeThrottle>("StackexchangeThrottle");
            var time     = UnixEpoch.Now;

            if (time < throttle.BackoffUntil)
            {
                return(new Optional <string>("The server said to backoff for " + (throttle.BackoffUntil - time).ToString() + " more seconds."));
            }
            else if (throttle.QuotaRemaining == 0 && UnixEpoch.Now < throttle.QuotaResetTime)
            {
                return(new Optional <string>("You ran out of api requests for today. It will reset at "
                                             + DateTimeOffset.FromUnixTimeSeconds(throttle.QuotaResetTime) + "."));
            }
            return(new Optional <string>());
        }