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

Fixed GetAsync method in Rest class #10797

Merged
merged 3 commits into from
Oct 4, 2022
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
4 changes: 2 additions & 2 deletions Assets/MRTK/Core/Utilities/WebRequestRest/ResponseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public struct ResponseUtils
/// Static Func for create convert Task
/// </summary>
public static Func<byte[], Task<string>> BytesToString = async (byteArray) => await Task.Run(() =>
System.Text.Encoding.Default.GetString(byteArray)).ConfigureAwait(false);
byteArray != null ? System.Text.Encoding.Default.GetString(byteArray) : string.Empty).ConfigureAwait(false);
}
}
}
5 changes: 3 additions & 2 deletions Assets/MRTK/Core/Utilities/WebRequestRest/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ private static async Task<Response> ProcessRequestAsync(UnityWebRequest webReque
long responseCode = webRequest.responseCode;
Func<byte[]> downloadHandlerDataAction = () => webRequest.downloadHandler?.data;
Func<string> downloadHandlerTextAction = () => webRequest.downloadHandler?.text;
Task<string> downloadHandlerTextTask = ResponseUtils.BytesToString(downloadHandlerDataAction.Invoke());

#if UNITY_2020_1_OR_NEWER
if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
Expand All @@ -378,14 +379,14 @@ private static async Task<Response> ProcessRequestAsync(UnityWebRequest webReque
}

string responseHeaders = webRequest.GetResponseHeaders().Aggregate(string.Empty, (current, header) => $"\n{header.Key}: {header.Value}");
string downloadHandlerText = downloadHandlerTextAction.Invoke();
string downloadHandlerText = await downloadHandlerTextTask;
Debug.LogError($"REST Error: {responseCode}\n{downloadHandlerText}{responseHeaders}");
return new Response(false, $"{responseHeaders}\n{downloadHandlerText}", downloadHandlerDataAction.Invoke(), responseCode);
}

if (readResponseData)
{
return new Response(true, downloadHandlerTextAction.Invoke(), downloadHandlerDataAction.Invoke(), responseCode);
return new Response(true, await downloadHandlerTextTask, downloadHandlerDataAction.Invoke(), responseCode);
}
else // This option can be used only if action will be triggered in the same scope as the webrequest
{
Expand Down