Skip to content

Commit

Permalink
com.utilities.rest 2.4.1 (#57)
Browse files Browse the repository at this point in the history
- added http method to debug info
- added EnumExtensions to convert enum member values to string
- updated editor to 2022.3.16f1 LTS
  • Loading branch information
StephenHodgson committed Dec 28, 2023
1 parent 80f68ef commit 0c00440
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Runtime/DownloadHandlerCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions Runtime/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<T>(this T value) where T : Enum
{
const string empty = "";
const string quote = "\"";
return JsonConvert.SerializeObject(value, settings).Replace(quote, empty);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Extensions/EnumExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions Runtime/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class Response
/// </summary>
public string Request { get; }

/// <summary>
/// The request method that prompted the response.
/// </summary>
public string Method { get; }

/// <summary>
/// Was the REST call successful?
/// </summary>
Expand Down Expand Up @@ -49,15 +54,17 @@ public class Response
/// Constructor.
/// </summary>
/// <param name="request">The request that prompted the response.</param>
/// <param name="method">The request method that prompted the response.</param>
/// <param name="successful">Was the REST call successful?</param>
/// <param name="body">Response body from the resource.</param>
/// <param name="data">Response data from the resource.</param>
/// <param name="responseCode">Response code from the resource.</param>
/// <param name="headers">Response headers from the resource.</param>
/// <param name="error">Optional, error message from the resource.</param>
public Response(string request, bool successful, string body, byte[] data, long responseCode, IReadOnlyDictionary<string, string> headers, string error = null)
public Response(string request, string method, bool successful, string body, byte[] data, long responseCode, IReadOnlyDictionary<string, string> headers, string error = null)
{
Request = request;
Method = method;
Successful = successful;
Body = body;
Data = data;
Expand All @@ -77,13 +84,8 @@ public string ToString(string methodName)
debugMessage.Append($"{methodName} -> ");
}

debugMessage.Append($"<b>[{(int)Code}]</b> <color=\"cyan\">{Request}</color>");

if (!Successful)
{
debugMessage.Append(" <color=\"red\">Failed!</color>");
}

debugMessage.Append($"<b>[{Method}:{(int)Code}]</b> <color=\"cyan\">{Request}</color>");
debugMessage.Append(!Successful ? " <color=\"red\">Failed!</color>" : " <color=\"green\">Success!</color>");
debugMessage.Append("\n");

if (Headers != null)
Expand Down
30 changes: 15 additions & 15 deletions Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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}")
};
}

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0c00440

Please sign in to comment.