/// <summary> /// Handle a single API request. /// Ensures all exceptions are caught and dealt with correctly. /// </summary> /// <param name="req">The request.</param> /// <returns>true if the request succeeded.</returns> private bool handleRequest(APIRequest req) { try { 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) { handleWebException(we); return(false); } catch (Exception e) { return(false); } }
/// <summary> /// Handle a single API request. /// Ensures all exceptions are caught and dealt with correctly. /// </summary> /// <param name="req">The request.</param> /// <returns>true if the request succeeded.</returns> private bool handleRequest(APIRequest req) { try { 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.Value = APIState.Online; } failureCount = 0; return(true); } catch (WebException we) { handleWebException(we); return(false); } catch (Exception ex) { Logger.Error(ex, "Error occurred while handling an API request."); return(false); } }
public virtual void Queue(APIRequest request) { if (HandleRequest?.Invoke(request) != true) { // this will fail due to not receiving an APIAccess, and trigger a failure on the request. // this is intended - any request in testing that needs non-failures should use HandleRequest. request.Perform(this); } }
/// <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); 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); } }
public void Perform(APIRequest request) { try { request.Perform(this); } catch (Exception e) { // todo: fix exception handling request.Fail(e); } }
/// <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); State = APIState.Online; failureCount = 0; return(true); } catch (WebException we) { HttpStatusCode statusCode = (we.Response as HttpWebResponse)?.StatusCode ?? HttpStatusCode.RequestTimeout; switch (statusCode) { case HttpStatusCode.Unauthorized: State = APIState.Offline; 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; 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); } }
/// <summary> /// Handle a single API request. /// </summary> /// <param name="req">The request.</param> /// <returns>true if the request succeeded.</returns> private bool handleRequest(APIRequest req, out bool removeFromQueue) { removeFromQueue = true; try { Logger.Log($@"Performing request {req}", LoggingTarget.Network); req.Failure += ex => { if (ex is WebException we) { handleWebException(we); } }; 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) { removeFromQueue = handleWebException(we); if (removeFromQueue) { req.Fail(we); } return(false); } catch (Exception e) { if (e is TimeoutException) { log.Add(@"API level timeout exception was hit"); } req.Fail(e); return(false); } }
/// <summary> /// Handle a single API request. /// Ensures all exceptions are caught and dealt with correctly. /// </summary> /// <param name="req">The request.</param> /// <returns>true if the request succeeded.</returns> private bool handleRequest(APIRequest req) { try { 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.Value = APIState.Online; } failureCount = 0; return(true); } catch (HttpRequestException re) { log.Add($"{nameof(HttpRequestException)} while performing request {req}: {re.Message}"); handleFailure(); return(false); } catch (SocketException se) { log.Add($"{nameof(SocketException)} while performing request {req}: {se.Message}"); handleFailure(); return(false); } catch (WebException we) { log.Add($"{nameof(WebException)} while performing request {req}: {we.Message}"); handleWebException(we); return(false); } catch (Exception ex) { Logger.Error(ex, "Error occurred while handling an API request."); return(false); } }