Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…alendarSync into release

HandleAPIlimits() when authenticating.
Allow x-thread retrieval of Console text.
Update console when unable to get Google calendar timezone/reminder settings.
Check for quota limits error - rateLimitExceeded error message changed.
Resolves #1053
  • Loading branch information
phw198 committed Oct 25, 2020
2 parents 8429cd9 + 1dc494e commit b70c8e3
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 63 deletions.
15 changes: 13 additions & 2 deletions src/OutlookGoogleCalendarSync/Console/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ public class Console {
private String content = "";
public String DocumentText {
get {
return (this.wb == null ? null : this.wb.DocumentText);
String documentText = "";
if (this.wb == null)
return null;
else {
if (this.wb.InvokeRequired) {
this.wb.Invoke((MethodInvoker)(() => {
documentText = wb.DocumentText;
}));
} else
documentText = this.wb.DocumentText;
}
return documentText;
}
}

Expand Down Expand Up @@ -271,7 +282,7 @@ public void Update(String moreOutput, Markup? markupPrefix = null, bool newLine
}

//Don't add append line break to Markup that's already wrapped in <div> tags
if (markupPrefix != null && (new Markup[] { Markup.info, Markup.warning, Markup.error }.ToList()).Contains((Markup)markupPrefix))
if (markupPrefix != null && (new Markup[] { Markup.info, Markup.warning, Markup.fail, Markup.error }.ToList()).Contains((Markup)markupPrefix))
newLine = false;
contentInnerHtml += htmlOutput + (newLine ? "<br/>" : "");

Expand Down
12 changes: 11 additions & 1 deletion src/OutlookGoogleCalendarSync/Extensions/Exception.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,17 @@ public static System.Exception LogAsFail(System.Exception ex) {
LogAsFail(ref ex);
return ex;
}


/// <summary>
/// Capture this exception as log4net FAIL (not ERROR) when logged
/// </summary>
public static void LogAsFail(ref Google.GoogleApiException ex) {
if (ex.Data.Contains(LogAs))
ex.Data[LogAs] = OGCSexception.LogLevel.FAIL;
else
ex.Data.Add(LogAs, OGCSexception.LogLevel.FAIL);
}

/// <summary>
/// Capture this exception as log4net FAIL (not ERROR) when logged
/// </summary>
Expand Down
53 changes: 41 additions & 12 deletions src/OutlookGoogleCalendarSync/GoogleOgcs/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static ClientSecrets getCalendarClientSecrets() {
return provider;
}

private async Task getAuthenticated(ClientSecrets cs) {
private async Task<bool> getAuthenticated(ClientSecrets cs) {
log.Debug("Authenticating with Google calendar service...");

FileDataStore tokenStore = new FileDataStore(Program.UserFilePath);
Expand Down Expand Up @@ -141,24 +141,51 @@ private async Task getAuthenticated(ClientSecrets cs) {
log.Debug("Access token needs refreshing.");
//This will happen automatically when using the calendar service
//But we need a valid token before we call getGaccountEmail() which doesn't use the service
try {
GoogleOgcs.Calendar.Instance.Service.Settings.Get("useKeyboardShortcuts").Execute();
} catch (System.Exception ex) {
if (ex is Google.Apis.Auth.OAuth2.Responses.TokenResponseException)
OGCSexception.AnalyseTokenResponse(ex as Google.Apis.Auth.OAuth2.Responses.TokenResponseException, false);
else {
OGCSexception.Analyse(ex);
Forms.Main.Instance.Console.Update("Unable to communicate with Google services. " + (ex.InnerException != null ? ex.InnerException.Message : ex.Message), Console.Markup.warning);
int backoff = 0;
while (backoff < Calendar.BackoffLimit) {
try {
GoogleOgcs.Calendar.Instance.Service.Settings.Get("useKeyboardShortcuts").Execute();
} catch (Google.GoogleApiException ex) {
switch (Calendar.HandleAPIlimits(ex, null)) {
case Calendar.ApiException.throwException: throw;
case Calendar.ApiException.freeAPIexhausted:
OGCSexception.LogAsFail(ref ex);
OGCSexception.Analyse(ex);
System.ApplicationException aex = new System.ApplicationException(Calendar.Instance.SubscriptionInvite);
OGCSexception.LogAsFail(ref aex);
authenticated = false;
return authenticated;
case Calendar.ApiException.backoffThenRetry:
backoff++;
if (backoff == Calendar.BackoffLimit) {
log.Fail("API limit backoff was not successful. Retrieving useKeyboardShortcuts setting failed.");
authenticated = false;
return authenticated;
} else {
log.Warn("API rate limit reached. Backing off " + backoff + "sec before retry.");
System.Threading.Thread.Sleep(backoff * 1000);
}
break;
}

} catch (System.Exception ex) {
if (ex is Google.Apis.Auth.OAuth2.Responses.TokenResponseException)
OGCSexception.AnalyseTokenResponse(ex as Google.Apis.Auth.OAuth2.Responses.TokenResponseException, false);
else {
OGCSexception.Analyse(ex);
Forms.Main.Instance.Console.Update("Unable to communicate with Google services. " + (ex.InnerException != null ? ex.InnerException.Message : ex.Message), Console.Markup.warning);
}
authenticated = false;
return authenticated;
}
authenticated = false;
return;
}
log.Debug("Access token refreshed.");
}

getGaccountEmail(credential.Token.AccessToken);
authenticated = true;
Forms.Main.Instance.Console.Update("Handshake successful.", verbose: true);
return authenticated;
}

public void Reset(Boolean reauthorise = true) {
Expand Down Expand Up @@ -312,7 +339,9 @@ public Boolean UserSubscriptionCheck() {
switch (GoogleOgcs.Calendar.HandleAPIlimits(ex, null)) {
case Calendar.ApiException.throwException: throw;
case Calendar.ApiException.freeAPIexhausted:
System.ApplicationException aex = new System.ApplicationException(GoogleOgcs.Calendar.Instance.SubscriptionInvite, ex);
OGCSexception.LogAsFail(ref ex);
OGCSexception.Analyse(ex);
System.ApplicationException aex = new System.ApplicationException(GoogleOgcs.Calendar.Instance.SubscriptionInvite);
OGCSexception.LogAsFail(ref aex);
GoogleOgcs.Calendar.Instance.Service = null;
throw aex;
Expand Down
Loading

0 comments on commit b70c8e3

Please sign in to comment.