Skip to content
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
30 changes: 21 additions & 9 deletions Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -88,6 +90,7 @@ public bool RemoveSuperHeader(string key)
{
throw new ArgumentException("Key cannot be null or empty.");
}

return superHeaders.Remove(key);
}

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

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

/// <summary>
Expand All @@ -219,10 +222,10 @@ public void Send(UnityWebRequest unityWebRequest, Action<UnityWebRequest> onSucc
/// <param name="onSuccess">The callback for on success response from the server</param>
/// <param name="onError">The callback for on error with the request or response.</param>
/// <param name="onNetworkError">The callback for on network error with the request.</param>
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));
}

/// <summary>
Expand All @@ -231,10 +234,19 @@ public void Send(UnityWebRequest unityWebRequest, Action onSuccess = null,
/// <param name="unityWebRequest">The request to transmit</param>
/// <param name="onSuccess">The callback for on success response from the server</param>
/// <param name="onError">The callback for on error with the request or response.</param>
public void Send(UnityWebRequest unityWebRequest, Action<HttpResponse> onSuccess = null,
public Coroutine Send(UnityWebRequest unityWebRequest, Action<HttpResponse> onSuccess = null,
Action<HttpResponse> 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
Expand Down Expand Up @@ -358,4 +370,4 @@ private static IEnumerator SendCoroutine(UnityWebRequest request,

#endregion
}
}
}
19 changes: 17 additions & 2 deletions HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

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

private readonly Dictionary<string, string> headers;
private Coroutine coroutine;

public HttpRequest(UnityWebRequest unityWebRequest)
{
Expand Down Expand Up @@ -44,7 +46,20 @@ public void Send(Action<HttpResponse> onSuccess = null, Action<HttpResponse> 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);
}
}
}
}
}