diff --git a/Runtime/DownloadHandlerCallback.cs b/Runtime/DownloadHandlerCallback.cs index e40f2cf..7de8662 100644 --- a/Runtime/DownloadHandlerCallback.cs +++ b/Runtime/DownloadHandlerCallback.cs @@ -48,7 +48,7 @@ protected override bool ReceiveData(byte[] unprocessedData, int dataLength) var buffer = new byte[bytesToRead]; var bytesRead = stream.Read(buffer, 0, (int)bytesToRead); streamPosition += bytesRead; - OnDataReceived?.Invoke(new Response(webRequest.url, true, null, buffer, webRequest.responseCode, webRequest.GetResponseHeaders())); + OnDataReceived?.Invoke(new Response(webRequest.url, webRequest.method, true, null, buffer, webRequest.responseCode, webRequest.GetResponseHeaders())); } } catch (Exception e) @@ -69,7 +69,7 @@ protected override void CompleteContent() var buffer = new byte[StreamOffset]; var bytesRead = stream.Read(buffer); streamPosition += bytesRead; - OnDataReceived?.Invoke(new Response(webRequest.url, true, null, buffer, webRequest.responseCode, webRequest.GetResponseHeaders())); + OnDataReceived?.Invoke(new Response(webRequest.url, webRequest.method, true, null, buffer, webRequest.responseCode, webRequest.GetResponseHeaders())); } } catch (Exception e) diff --git a/Runtime/Extensions/EnumExtensions.cs b/Runtime/Extensions/EnumExtensions.cs new file mode 100644 index 0000000..a0a98f8 --- /dev/null +++ b/Runtime/Extensions/EnumExtensions.cs @@ -0,0 +1,23 @@ +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using System; + +namespace Utilities.Rest +{ + public static class EnumExtensions + { + private static readonly JsonSerializerSettings settings = new JsonSerializerSettings + { + Converters = { new StringEnumConverter() } + }; + + public static string ToEnumMemberString(this T value) where T : Enum + { + const string empty = ""; + const string quote = "\""; + return JsonConvert.SerializeObject(value, settings).Replace(quote, empty); + } + } +} diff --git a/Runtime/Extensions/EnumExtensions.cs.meta b/Runtime/Extensions/EnumExtensions.cs.meta new file mode 100644 index 0000000..1bb3fdf --- /dev/null +++ b/Runtime/Extensions/EnumExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6686283b885c7244aafe79ecc11b34b8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Response.cs b/Runtime/Response.cs index 9444b8c..262445d 100644 --- a/Runtime/Response.cs +++ b/Runtime/Response.cs @@ -15,6 +15,11 @@ public class Response /// public string Request { get; } + /// + /// The request method that prompted the response. + /// + public string Method { get; } + /// /// Was the REST call successful? /// @@ -49,15 +54,17 @@ public class Response /// Constructor. /// /// The request that prompted the response. + /// The request method that prompted the response. /// Was the REST call successful? /// Response body from the resource. /// Response data from the resource. /// Response code from the resource. /// Response headers from the resource. /// Optional, error message from the resource. - public Response(string request, bool successful, string body, byte[] data, long responseCode, IReadOnlyDictionary headers, string error = null) + public Response(string request, string method, bool successful, string body, byte[] data, long responseCode, IReadOnlyDictionary headers, string error = null) { Request = request; + Method = method; Successful = successful; Body = body; Data = data; @@ -77,13 +84,8 @@ public string ToString(string methodName) debugMessage.Append($"{methodName} -> "); } - debugMessage.Append($"[{(int)Code}] {Request}"); - - if (!Successful) - { - debugMessage.Append(" Failed!"); - } - + debugMessage.Append($"[{Method}:{(int)Code}] {Request}"); + debugMessage.Append(!Successful ? " Failed!" : " Success!"); debugMessage.Append("\n"); if (Headers != null) diff --git a/Runtime/Rest.cs b/Runtime/Rest.cs index b184db4..5a73422 100644 --- a/Runtime/Rest.cs +++ b/Runtime/Rest.cs @@ -1138,7 +1138,7 @@ async void CallbackThread() } catch (Exception e) { - return new Response(webRequest.url, false, $"{nameof(Rest)}.{nameof(SendAsync)}::{nameof(UnityWebRequest.SendWebRequest)} Failed!", null, -1, null, e.ToString()); + return new Response(webRequest.url, webRequest.method, false, $"{nameof(Rest)}.{nameof(SendAsync)}::{nameof(UnityWebRequest.SendWebRequest)} Failed!", null, -1, null, e.ToString()); } parameters?.Progress?.Report(new Progress(webRequest.downloadedBytes, webRequest.downloadedBytes, 100f, 0, Progress.DataUnit.b)); @@ -1151,13 +1151,13 @@ UnityWebRequest.Result.ConnectionError or { return webRequest.downloadHandler switch { - DownloadHandlerFile => new Response(webRequest.url, false, null, null, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), - DownloadHandlerTexture => new Response(webRequest.url, false, null, null, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), - DownloadHandlerAudioClip => new Response(webRequest.url, false, null, null, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), - DownloadHandlerAssetBundle => new Response(webRequest.url, false, null, null, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), - DownloadHandlerBuffer bufferDownloadHandler => new Response(webRequest.url, false, bufferDownloadHandler.text, bufferDownloadHandler.data, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), - DownloadHandlerScript scriptDownloadHandler => new Response(webRequest.url, false, scriptDownloadHandler.text, scriptDownloadHandler.data, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), - _ => new Response(webRequest.url, false, webRequest.responseCode == 401 ? "Invalid Credentials" : webRequest.downloadHandler?.text, webRequest.downloadHandler?.data, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}") + DownloadHandlerFile => new Response(webRequest.url, webRequest.method, false, null, null, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), + DownloadHandlerTexture => new Response(webRequest.url, webRequest.method, false, null, null, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), + DownloadHandlerAudioClip => new Response(webRequest.url, webRequest.method, false, null, null, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), + DownloadHandlerAssetBundle => new Response(webRequest.url, webRequest.method, false, null, null, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), + DownloadHandlerBuffer bufferDownloadHandler => new Response(webRequest.url, webRequest.method, false, bufferDownloadHandler.text, bufferDownloadHandler.data, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), + DownloadHandlerScript scriptDownloadHandler => new Response(webRequest.url, webRequest.method, false, scriptDownloadHandler.text, scriptDownloadHandler.data, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}"), + _ => new Response(webRequest.url, webRequest.method, false, webRequest.responseCode == 401 ? "Invalid Credentials" : webRequest.downloadHandler?.text, webRequest.downloadHandler?.data, webRequest.responseCode, responseHeaders, $"{webRequest.error}\n{webRequest.downloadHandler?.error}") }; } @@ -1168,13 +1168,13 @@ UnityWebRequest.Result.ConnectionError or return webRequest.downloadHandler switch { - DownloadHandlerFile => new Response(webRequest.url, true, null, null, webRequest.responseCode, responseHeaders), - DownloadHandlerTexture => new Response(webRequest.url, true, null, null, webRequest.responseCode, responseHeaders), - DownloadHandlerAudioClip => new Response(webRequest.url, true, null, null, webRequest.responseCode, responseHeaders), - DownloadHandlerAssetBundle => new Response(webRequest.url, true, null, null, webRequest.responseCode, responseHeaders), - DownloadHandlerBuffer bufferDownloadHandler => new Response(webRequest.url, true, bufferDownloadHandler.text, bufferDownloadHandler.data, webRequest.responseCode, responseHeaders), - DownloadHandlerScript scriptDownloadHandler => new Response(webRequest.url, true, scriptDownloadHandler.text, scriptDownloadHandler.data, webRequest.responseCode, responseHeaders), - _ => new Response(webRequest.url, true, webRequest.downloadHandler?.text, webRequest.downloadHandler?.data, webRequest.responseCode, responseHeaders) + DownloadHandlerFile => new Response(webRequest.url, webRequest.method, true, null, null, webRequest.responseCode, responseHeaders), + DownloadHandlerTexture => new Response(webRequest.url, webRequest.method, true, null, null, webRequest.responseCode, responseHeaders), + DownloadHandlerAudioClip => new Response(webRequest.url, webRequest.method, true, null, null, webRequest.responseCode, responseHeaders), + DownloadHandlerAssetBundle => new Response(webRequest.url, webRequest.method, true, null, null, webRequest.responseCode, responseHeaders), + DownloadHandlerBuffer bufferDownloadHandler => new Response(webRequest.url, webRequest.method, true, bufferDownloadHandler.text, bufferDownloadHandler.data, webRequest.responseCode, responseHeaders), + DownloadHandlerScript scriptDownloadHandler => new Response(webRequest.url, webRequest.method, true, scriptDownloadHandler.text, scriptDownloadHandler.data, webRequest.responseCode, responseHeaders), + _ => new Response(webRequest.url, webRequest.method, true, webRequest.downloadHandler?.text, webRequest.downloadHandler?.data, webRequest.responseCode, responseHeaders) }; void SendServerEventCallback(bool isEnd) diff --git a/package.json b/package.json index 6df5897..8765f0d 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "Utilities.Rest", "description": "This package contains useful RESTful utilities for the Unity Game Engine.", "keywords": [], - "version": "2.4.0", + "version": "2.4.1", "unity": "2021.3", "documentationUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest#documentation", "changelogUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest/releases",