Skip to content

Commit

Permalink
Added a progress reporting callback
Browse files Browse the repository at this point in the history
  • Loading branch information
lyze237 committed Sep 14, 2020
1 parent acb36fa commit f5c546d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ RestClient.Request(new RequestHelper {
Retries = 3, //Number of retries
RetrySecondsDelay = 2, //Seconds of delay to make a retry
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
IgnoreHttpException = true, //Prevent to catch http exceptions
ChunkedTransfer = false,
Expand Down Expand Up @@ -260,6 +261,14 @@ currentRequest.DownloadedBytes; //The number of bytes of body data the system ha
currentRequest.Abort(); //Abort the request manually
```

Additionally we can run a callback function whenever a progress change happens!
```csharp
RestClient.Get(new RequestHelper {
Uri = "https://jsonplaceholder.typicode.com/users",
ProgressCallback = percent => Debug.Log(percent)
});
```

Later we can clear the default headers and params for all requests
```csharp
RestClient.ClearDefaultHeaders();
Expand Down
8 changes: 4 additions & 4 deletions src/Proyecto26.RestClient/Helpers/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ private static void ConfigureWebRequestWithOptions(UnityWebRequest request, byte
/// <summary>
/// Send the web request to the server
/// </summary>
/// <returns>An UnityWebRequestAsyncOperation object.</returns>
/// <returns>An AsyncOperation object.</returns>
/// <param name="request">An UnityWebRequest object.</param>
/// <param name="options">An options object.</param>
public static IEnumerator SendWebRequestWithOptions(this UnityWebRequest request, RequestHelper options)
public static AsyncOperation SendWebRequestWithOptions(this UnityWebRequest request, RequestHelper options)
{
byte[] bodyRaw = options.BodyRaw;
string contentType = string.Empty;
Expand Down Expand Up @@ -119,9 +119,9 @@ public static IEnumerator SendWebRequestWithOptions(this UnityWebRequest request

ConfigureWebRequestWithOptions(request, bodyRaw, contentType, options);
#if UNITY_2017_2_OR_NEWER
yield return request.SendWebRequest();
return request.SendWebRequest();
#else
yield return request.Send();
return request.Send();
#endif
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/Proyecto26.RestClient/Helpers/HttpBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@ public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action<Re
{
using (var request = CreateRequest(options))
{
yield return request.SendWebRequestWithOptions(options);
var sendRequest = request.SendWebRequestWithOptions(options);
if (options.ProgressCallback == null)
{
yield return sendRequest;
}
else
{
while (!sendRequest.isDone)
{
options.ProgressCallback(sendRequest.progress);
yield return null;
}

options.ProgressCallback(1);
}

var response = request.CreateWebResponse();
if (request.IsValidRequest(options))
{
Expand Down
11 changes: 11 additions & 0 deletions src/Proyecto26.RestClient/Helpers/RequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,18 @@ public float RetrySecondsDelay
get { return _retryCallback; }
set { _retryCallback = value; }
}

private Action<float> _progressCallback;

/// <summary>
/// A callback executed everytime the requests progress changes (From 0 to 1)
/// </summary>
public Action<float> ProgressCallback
{
get { return _progressCallback; }
set { _progressCallback = value; }
}

private bool _enableDebug;
/// <summary>
/// Enable logs of the requests for debug mode
Expand Down

0 comments on commit f5c546d

Please sign in to comment.