Skip to content

Commit 374d33d

Browse files
Merge pull request #2 from dubit/dev_abort_request
Aborting a HttpRequest
2 parents 3013991 + 1deb923 commit 374d33d

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

Http.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ public void SetSuperHeader(string key, string value)
7070
{
7171
throw new ArgumentException("Key cannot be null or empty.");
7272
}
73+
7374
if (string.IsNullOrEmpty(value))
7475
{
7576
throw new ArgumentException("Value cannot be null or empty, if you are intending to remove the value, use the RemoveSuperHeader() method.");
7677
}
78+
7779
superHeaders[key] = value;
7880
}
7981

@@ -88,6 +90,7 @@ public bool RemoveSuperHeader(string key)
8890
{
8991
throw new ArgumentException("Key cannot be null or empty.");
9092
}
93+
9194
return superHeaders.Remove(key);
9295
}
9396

@@ -194,9 +197,9 @@ public static HttpRequest Head(string uri)
194197
/// <param name="request">The request to transmit</param>
195198
/// <param name="onSuccess">The callback for on success response from the server</param>
196199
/// <param name="onError">The callback for on error with the request or response.</param>
197-
public void Send(HttpRequest request, Action<HttpResponse> onSuccess = null, Action<HttpResponse> onError = null)
200+
public Coroutine Send(HttpRequest request, Action<HttpResponse> onSuccess = null, Action<HttpResponse> onError = null)
198201
{
199-
StartCoroutine(SendCoroutine(request, onSuccess, onError));
202+
return StartCoroutine(SendCoroutine(request, onSuccess, onError));
200203
}
201204

202205
/// <summary>
@@ -206,10 +209,10 @@ public void Send(HttpRequest request, Action<HttpResponse> onSuccess = null, Act
206209
/// <param name="onSuccess">The callback for on success response from the server</param>
207210
/// <param name="onError">THe callback for on error with the request or response.</param>
208211
/// <param name="onNetworkError">The callback for on network error with the request.</param>
209-
public void Send(UnityWebRequest unityWebRequest, Action<UnityWebRequest> onSuccess = null,
212+
public Coroutine Send(UnityWebRequest unityWebRequest, Action<UnityWebRequest> onSuccess = null,
210213
Action<UnityWebRequest> onError = null, Action<UnityWebRequest> onNetworkError = null)
211214
{
212-
StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError, onNetworkError));
215+
return StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError, onNetworkError));
213216
}
214217

215218
/// <summary>
@@ -219,10 +222,10 @@ public void Send(UnityWebRequest unityWebRequest, Action<UnityWebRequest> onSucc
219222
/// <param name="onSuccess">The callback for on success response from the server</param>
220223
/// <param name="onError">The callback for on error with the request or response.</param>
221224
/// <param name="onNetworkError">The callback for on network error with the request.</param>
222-
public void Send(UnityWebRequest unityWebRequest, Action onSuccess = null,
225+
public Coroutine Send(UnityWebRequest unityWebRequest, Action onSuccess = null,
223226
Action onError = null, Action onNetworkError = null)
224227
{
225-
StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError, onNetworkError));
228+
return StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError, onNetworkError));
226229
}
227230

228231
/// <summary>
@@ -231,10 +234,19 @@ public void Send(UnityWebRequest unityWebRequest, Action onSuccess = null,
231234
/// <param name="unityWebRequest">The request to transmit</param>
232235
/// <param name="onSuccess">The callback for on success response from the server</param>
233236
/// <param name="onError">The callback for on error with the request or response.</param>
234-
public void Send(UnityWebRequest unityWebRequest, Action<HttpResponse> onSuccess = null,
237+
public Coroutine Send(UnityWebRequest unityWebRequest, Action<HttpResponse> onSuccess = null,
235238
Action<HttpResponse> onError = null)
236239
{
237-
StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError));
240+
return StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError));
241+
}
242+
243+
#endregion
244+
245+
#region Abort HttpRequest method
246+
247+
public void Abort(Coroutine coroutine)
248+
{
249+
StopCoroutine(coroutine);
238250
}
239251

240252
#endregion
@@ -358,4 +370,4 @@ private static IEnumerator SendCoroutine(UnityWebRequest request,
358370

359371
#endregion
360372
}
361-
}
373+
}

HttpRequest.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using UnityEngine;
34
using UnityEngine.Networking;
45

56
namespace DUCK.Http
@@ -9,6 +10,7 @@ public class HttpRequest
910
public UnityWebRequest UnityWebRequest { get; private set; }
1011

1112
private readonly Dictionary<string, string> headers;
13+
private Coroutine coroutine;
1214

1315
public HttpRequest(UnityWebRequest unityWebRequest)
1416
{
@@ -44,7 +46,20 @@ public void Send(Action<HttpResponse> onSuccess = null, Action<HttpResponse> onE
4446
UnityWebRequest.SetRequestHeader(header.Key, header.Value);
4547
}
4648

47-
Http.Instance.Send(this, onSuccess, onError);
49+
coroutine = Http.Instance.Send(this, onSuccess, onError);
50+
}
51+
52+
public void Abort()
53+
{
54+
if (UnityWebRequest != null && !UnityWebRequest.isDone)
55+
{
56+
UnityWebRequest.Abort();
57+
}
58+
59+
if (coroutine != null)
60+
{
61+
Http.Instance.Abort(coroutine);
62+
}
4863
}
4964
}
50-
}
65+
}

0 commit comments

Comments
 (0)