Example #1
0
 public void Queue(APIRequest request)
 {
     lock (queue) queue.Enqueue(request);
 }
Example #2
0
 public Task PerformAsync(APIRequest request) =>
 Task.Factory.StartNew(() => Perform(request), TaskCreationOptions.LongRunning);
Example #3
0
 public void Queue(APIRequest request)
 {
     queue.Enqueue(request);
 }
 public Task PerformAsync(APIRequest request)
 {
     HandleRequest?.Invoke(request);
     return(Task.CompletedTask);
 }
 public void Perform(APIRequest request) => HandleRequest?.Invoke(request);
 public virtual void Queue(APIRequest request)
 {
     HandleRequest?.Invoke(request);
 }
Example #7
0
        /// <summary>
        /// Handle a single API request.
        /// </summary>
        /// <param name="req">The request.</param>
        /// <returns>true if we should remove this request from the queue.</returns>
        private bool handleRequest(APIRequest req)
        {
            try
            {
                Logger.Log($@"Performing request {req}", LoggingTarget.Network);
                req.Perform(this);

                //we could still be in initialisation, at which point we don't want to say we're Online yet.
                if (IsLoggedIn)
                {
                    State = APIState.Online;
                }

                failureCount = 0;
                return(true);
            }
            catch (WebException we)
            {
                HttpStatusCode statusCode = (we.Response as HttpWebResponse)?.StatusCode
                                            ?? (we.Status == WebExceptionStatus.UnknownError ? HttpStatusCode.NotAcceptable : HttpStatusCode.RequestTimeout);

                // special cases for un-typed but useful message responses.
                switch (we.Message)
                {
                case "Unauthorized":
                    statusCode = HttpStatusCode.Unauthorized;
                    break;
                }

                switch (statusCode)
                {
                case HttpStatusCode.Unauthorized:
                    Logout(false);
                    return(true);

                case HttpStatusCode.RequestTimeout:
                    failureCount++;
                    log.Add($@"API failure count is now {failureCount}");

                    if (failureCount < 3)
                    {
                        //we might try again at an api level.
                        return(false);
                    }

                    State = APIState.Failing;
                    flushQueue();
                    return(true);
                }

                req.Fail(we);
                return(true);
            }
            catch (Exception e)
            {
                if (e is TimeoutException)
                {
                    log.Add(@"API level timeout exception was hit");
                }

                req.Fail(e);
                return(true);
            }
        }