Move bulk of method into separate private method with minor clean-ups

This commit is contained in:
Dean Herbert 2018-12-14 15:48:34 +09:00
parent a1543905ae
commit 9072af9792
1 changed files with 37 additions and 41 deletions

View File

@ -196,52 +196,13 @@ public void Login(string username, string password)
/// <returns>true if we should remove this request from the queue.</returns>
private bool handleRequest(APIRequest req)
{
bool handleWebException(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":
case "Forbidden":
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;
}
return true;
}
try
{
Logger.Log($@"Performing request {req}", LoggingTarget.Network);
req.Failure += ex =>
{
switch (ex)
{
case WebException we:
handleWebException(we);
break;
}
if (ex is WebException we)
handleWebException(we);
};
req.Perform(this);
@ -296,6 +257,41 @@ private set
}
}
private bool handleWebException(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":
case "Forbidden":
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;
}
return true;
}
public bool IsLoggedIn => LocalUser.Value.Id > 1;
public void Queue(APIRequest request)