Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to use RetryCallback to handle token expiration #142

Merged
merged 3 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ RestClient.Request(new RequestHelper {
ContentType = "application/json", //JSON is used by default
Retries = 3, //Number of retries
RetrySecondsDelay = 2, //Seconds of delay to make a retry
RetryCallbackOnlyOnNetworkErrors = true, //Invoke RetryCallack only when the retry is provoked by a network error
RetryCallback = (err, retries) => {}, //See the error before retrying the request
ProgressCallback = (percent) => {}, //Reports progress of the request from 0 to 1
EnableDebug = true, //See logs of the requests for debug mode
Expand Down
Binary file modified doc/RestClient.docx
Binary file not shown.
Binary file modified doc/RestClient.pdf
Binary file not shown.
9 changes: 4 additions & 5 deletions src/Proyecto26.RestClient/Helpers/HttpBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action<Re
callback(null, response);
break;
}
else if (!options.IsAborted && retries < options.Retries && (IsNetworkError))
else if (!options.IsAborted && retries < options.Retries && (!options.RetryCallbackOnlyOnNetworkErrors || IsNetworkError))
{
yield return new WaitForSeconds(options.RetrySecondsDelay);
retries++;
if (options.RetryCallback != null)
{
options.RetryCallback(CreateException(options, request), retries);
}
DebugLog(options.EnableDebug, string.Format("RestClient - Retry Request\nUrl: {0}\nMethod: {1}", options.Uri, options.Method), false);
yield return new WaitForSeconds(options.RetrySecondsDelay);
retries++;
jdnichollsc marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
Expand Down Expand Up @@ -93,8 +93,7 @@ private static RequestException CreateException(RequestHelper options, UnityWebR
IsNetworkError = request.isNetworkError;
IsHttpError = request.isHttpError;
#endif

return new RequestException(request.error, IsHttpError, IsNetworkError, request.responseCode, options.ParseResponseBody ? request.downloadHandler.text : "body not parsed");
return new RequestException(options, request.error, IsHttpError, IsNetworkError, request.responseCode, options.ParseResponseBody ? request.downloadHandler.text : "body not parsed");
}

private static void DebugLog(bool debugEnabled, object message, bool isError)
Expand Down
12 changes: 9 additions & 3 deletions src/Proyecto26.RestClient/Helpers/RequestException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ namespace Proyecto26
{
public class RequestException : Exception
{
private RequestHelper _request;
public RequestHelper Request
{
get { return _request; }
private set { _request = value; }
}

private bool _isHttpError;
public bool IsHttpError
{
Expand Down Expand Up @@ -42,9 +49,8 @@ public RequestException() { }

public RequestException(string message): base(message) { }

public RequestException(string format, params object[] args): base(string.Format(format, args)) { }

public RequestException(string message, bool isHttpError, bool isNetworkError, long statusCode, string response) : base(message) {
public RequestException(RequestHelper request, string message, bool isHttpError, bool isNetworkError, long statusCode, string response) : base(message) {
_request = request;
_isHttpError = isHttpError;
_isNetworkError = isNetworkError;
_statusCode = statusCode;
Expand Down
10 changes: 10 additions & 0 deletions src/Proyecto26.RestClient/Helpers/RequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public float RetrySecondsDelay
set { _retrySecondsDelay = value; }
}

private bool _retryCallbackOnlyOnNetworkErrors = true;
/// <summary>
/// Invoke RetryCallack only when the retry is provoked by a network error. (Default: true).
/// </summary>
public bool RetryCallbackOnlyOnNetworkErrors
{
get { return _retryCallbackOnlyOnNetworkErrors; }
set { _retryCallbackOnlyOnNetworkErrors = value; }
}

private Action<RequestException, int> _retryCallback;
/// <summary>
/// A callback executed before to retry a request
Expand Down
2 changes: 1 addition & 1 deletion src/Proyecto26.RestClient/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion ("2.6.1")]
[assembly: AssemblyVersion ("2.6.2")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
Expand Down
2 changes: 1 addition & 1 deletion src/Proyecto26.RestClient/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Proyecto26
{
/// <summary>
/// RestClient for Unity
/// Version: 2.6.1
/// Version: 2.6.2
/// </summary>
public static partial class RestClient
{
Expand Down
2 changes: 1 addition & 1 deletion src/Proyecto26.RestClient/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.proyecto26.restclient",
"version": "2.6.1",
"version": "2.6.2",
"displayName": "RestClient for Unity",
"description": "Simple HTTP and REST client for Unity based on Promises, also support Callbacks!",
"unity": "2017.1",
Expand Down