From 1deb92398da57f5fdbd95cbc066a874678728fc0 Mon Sep 17 00:00:00 2001 From: Harry Hodgson Date: Thu, 28 Jun 2018 11:21:40 +0100 Subject: [PATCH] Adding methods for Aborting a HttpRequest --- Http.cs | 30 +++++++++++++++++++++--------- HttpRequest.cs | 19 +++++++++++++++++-- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Http.cs b/Http.cs index 68a147f..40f074c 100644 --- a/Http.cs +++ b/Http.cs @@ -70,10 +70,12 @@ public void SetSuperHeader(string key, string value) { throw new ArgumentException("Key cannot be null or empty."); } + if (string.IsNullOrEmpty(value)) { throw new ArgumentException("Value cannot be null or empty, if you are intending to remove the value, use the RemoveSuperHeader() method."); } + superHeaders[key] = value; } @@ -88,6 +90,7 @@ public bool RemoveSuperHeader(string key) { throw new ArgumentException("Key cannot be null or empty."); } + return superHeaders.Remove(key); } @@ -194,9 +197,9 @@ public static HttpRequest Head(string uri) /// The request to transmit /// The callback for on success response from the server /// The callback for on error with the request or response. - public void Send(HttpRequest request, Action onSuccess = null, Action onError = null) + public Coroutine Send(HttpRequest request, Action onSuccess = null, Action onError = null) { - StartCoroutine(SendCoroutine(request, onSuccess, onError)); + return StartCoroutine(SendCoroutine(request, onSuccess, onError)); } /// @@ -206,10 +209,10 @@ public void Send(HttpRequest request, Action onSuccess = null, Act /// The callback for on success response from the server /// THe callback for on error with the request or response. /// The callback for on network error with the request. - public void Send(UnityWebRequest unityWebRequest, Action onSuccess = null, + public Coroutine Send(UnityWebRequest unityWebRequest, Action onSuccess = null, Action onError = null, Action onNetworkError = null) { - StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError, onNetworkError)); + return StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError, onNetworkError)); } /// @@ -219,10 +222,10 @@ public void Send(UnityWebRequest unityWebRequest, Action onSucc /// The callback for on success response from the server /// The callback for on error with the request or response. /// The callback for on network error with the request. - public void Send(UnityWebRequest unityWebRequest, Action onSuccess = null, + public Coroutine Send(UnityWebRequest unityWebRequest, Action onSuccess = null, Action onError = null, Action onNetworkError = null) { - StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError, onNetworkError)); + return StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError, onNetworkError)); } /// @@ -231,10 +234,19 @@ public void Send(UnityWebRequest unityWebRequest, Action onSuccess = null, /// The request to transmit /// The callback for on success response from the server /// The callback for on error with the request or response. - public void Send(UnityWebRequest unityWebRequest, Action onSuccess = null, + public Coroutine Send(UnityWebRequest unityWebRequest, Action onSuccess = null, Action onError = null) { - StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError)); + return StartCoroutine(SendCoroutine(unityWebRequest, onSuccess, onError)); + } + + #endregion + + #region Abort HttpRequest method + + public void Abort(Coroutine coroutine) + { + StopCoroutine(coroutine); } #endregion @@ -358,4 +370,4 @@ private static IEnumerator SendCoroutine(UnityWebRequest request, #endregion } -} \ No newline at end of file +} diff --git a/HttpRequest.cs b/HttpRequest.cs index 6350ad8..66f4a40 100644 --- a/HttpRequest.cs +++ b/HttpRequest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using UnityEngine; using UnityEngine.Networking; namespace DUCK.Http @@ -9,6 +10,7 @@ public class HttpRequest public UnityWebRequest UnityWebRequest { get; private set; } private readonly Dictionary headers; + private Coroutine coroutine; public HttpRequest(UnityWebRequest unityWebRequest) { @@ -44,7 +46,20 @@ public void Send(Action onSuccess = null, Action onE UnityWebRequest.SetRequestHeader(header.Key, header.Value); } - Http.Instance.Send(this, onSuccess, onError); + coroutine = Http.Instance.Send(this, onSuccess, onError); + } + + public void Abort() + { + if (UnityWebRequest != null && !UnityWebRequest.isDone) + { + UnityWebRequest.Abort(); + } + + if (coroutine != null) + { + Http.Instance.Abort(coroutine); + } } } -} \ No newline at end of file +}