Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.utilities.rest 2.4.1 #57

Merged
merged 1 commit into from
Dec 28, 2023
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
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
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);
}
}
}

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

18 changes: 10 additions & 8 deletions Utilities.Rest/Packages/com.utilities.rest/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 Utilities.Rest/Packages/com.utilities.rest/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 Utilities.Rest/Packages/com.utilities.rest/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
Loading