Skip to content

Commit f5c546d

Browse files
committed
Added a progress reporting callback
1 parent acb36fa commit f5c546d

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ RestClient.Request(new RequestHelper {
138138
Retries = 3, //Number of retries
139139
RetrySecondsDelay = 2, //Seconds of delay to make a retry
140140
RetryCallback = (err, retries) => {}, //See the error before retrying the request
141+
ProgressCallback = (percent) => {}, //Reports progress of the request from 0 to 1
141142
EnableDebug = true, //See logs of the requests for debug mode
142143
IgnoreHttpException = true, //Prevent to catch http exceptions
143144
ChunkedTransfer = false,
@@ -260,6 +261,14 @@ currentRequest.DownloadedBytes; //The number of bytes of body data the system ha
260261
currentRequest.Abort(); //Abort the request manually
261262
```
262263

264+
Additionally we can run a callback function whenever a progress change happens!
265+
```csharp
266+
RestClient.Get(new RequestHelper {
267+
Uri = "https://jsonplaceholder.typicode.com/users",
268+
ProgressCallback = percent => Debug.Log(percent)
269+
});
270+
```
271+
263272
Later we can clear the default headers and params for all requests
264273
```csharp
265274
RestClient.ClearDefaultHeaders();

src/Proyecto26.RestClient/Helpers/Common.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ private static void ConfigureWebRequestWithOptions(UnityWebRequest request, byte
7878
/// <summary>
7979
/// Send the web request to the server
8080
/// </summary>
81-
/// <returns>An UnityWebRequestAsyncOperation object.</returns>
81+
/// <returns>An AsyncOperation object.</returns>
8282
/// <param name="request">An UnityWebRequest object.</param>
8383
/// <param name="options">An options object.</param>
84-
public static IEnumerator SendWebRequestWithOptions(this UnityWebRequest request, RequestHelper options)
84+
public static AsyncOperation SendWebRequestWithOptions(this UnityWebRequest request, RequestHelper options)
8585
{
8686
byte[] bodyRaw = options.BodyRaw;
8787
string contentType = string.Empty;
@@ -119,9 +119,9 @@ public static IEnumerator SendWebRequestWithOptions(this UnityWebRequest request
119119

120120
ConfigureWebRequestWithOptions(request, bodyRaw, contentType, options);
121121
#if UNITY_2017_2_OR_NEWER
122-
yield return request.SendWebRequest();
122+
return request.SendWebRequest();
123123
#else
124-
yield return request.Send();
124+
return request.Send();
125125
#endif
126126
}
127127
}

src/Proyecto26.RestClient/Helpers/HttpBase.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,22 @@ public static IEnumerator CreateRequestAndRetry(RequestHelper options, Action<Re
1515
{
1616
using (var request = CreateRequest(options))
1717
{
18-
yield return request.SendWebRequestWithOptions(options);
18+
var sendRequest = request.SendWebRequestWithOptions(options);
19+
if (options.ProgressCallback == null)
20+
{
21+
yield return sendRequest;
22+
}
23+
else
24+
{
25+
while (!sendRequest.isDone)
26+
{
27+
options.ProgressCallback(sendRequest.progress);
28+
yield return null;
29+
}
30+
31+
options.ProgressCallback(1);
32+
}
33+
1934
var response = request.CreateWebResponse();
2035
if (request.IsValidRequest(options))
2136
{

src/Proyecto26.RestClient/Helpers/RequestHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,18 @@ public Action<RequestException, int> RetryCallback
106106
get { return _retryCallback; }
107107
set { _retryCallback = value; }
108108
}
109+
110+
private Action<float> _progressCallback;
109111

112+
/// <summary>
113+
/// A callback executed everytime the requests progress changes (From 0 to 1)
114+
/// </summary>
115+
public Action<float> ProgressCallback
116+
{
117+
get { return _progressCallback; }
118+
set { _progressCallback = value; }
119+
}
120+
110121
private bool _enableDebug;
111122
/// <summary>
112123
/// Enable logs of the requests for debug mode

0 commit comments

Comments
 (0)