Skip to content

Commit

Permalink
refactor: futer WebGL implementation cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Mar 31, 2022
1 parent 50022ec commit e9f9faf
Showing 1 changed file with 19 additions and 39 deletions.
58 changes: 19 additions & 39 deletions src/Sentry.Unity/WebGL/SentryWebGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal class WebBackgroundWorker : IBackgroundWorker
public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behaviour)
{
_behaviour = behaviour;
_transport = new UnityWebRequestTransport(options, behaviour);
_transport = new UnityWebRequestTransport(options);
}

public bool EnqueueEnvelope(Envelope envelope)
Expand All @@ -72,7 +72,7 @@ internal class UnityWebRequestTransport : HttpTransportBase
{
private readonly SentryUnityOptions _options;

public UnityWebRequestTransport(SentryUnityOptions options, SentryMonoBehaviour behaviour)
public UnityWebRequestTransport(SentryUnityOptions options)
: base(options)
{
_options = options;
Expand All @@ -85,7 +85,8 @@ internal IEnumerator SendEnvelopeAsync(Envelope envelope)
if (processedEnvelope.Items.Count > 0)
{
// Send envelope to ingress
var www = CreateWebRequest(CreateRequest(processedEnvelope));
var httpRequest = CreateRequest(processedEnvelope);
var www = CreateWebRequest(httpRequest, processedEnvelope);
yield return www.SendWebRequest();

var response = GetResponse(www);
Expand All @@ -96,28 +97,32 @@ internal IEnumerator SendEnvelopeAsync(Envelope envelope)
}
}

private UnityWebRequest CreateWebRequest(HttpRequestMessage message)
private UnityWebRequest CreateWebRequest(HttpRequestMessage message, Envelope envelope)
{
var www = new UnityWebRequest();
www.url = message.RequestUri.ToString();
www.method = message.Method.Method.ToUpperInvariant();
// Note: In order to use the synchronous Envelope.Serialize() we ignore the `message.Content`
// which is an `EnvelopeHttpContent` instance and use the actual envelope it wraps.
var stream = new MemoryStream();
envelope.Serialize(stream, _options.DiagnosticLogger);
stream.Flush();

var www = new UnityWebRequest
{
url = message.RequestUri.ToString(),
method = message.Method.Method.ToUpperInvariant(),
uploadHandler = new UploadHandlerRaw(stream.ToArray()),
downloadHandler = new DownloadHandlerBuffer()
};

foreach (var header in message.Headers)
{
www.SetRequestHeader(header.Key, string.Join(",", header.Value));
}

var stream = new MemoryStream();
_ = message.Content.CopyToAsync(stream).Wait(2000);
stream.Flush();
www.uploadHandler = new UploadHandlerRaw(stream.ToArray());
www.downloadHandler = new DownloadHandlerBuffer();
return www;
}

private HttpResponseMessage? GetResponse(UnityWebRequest www)
{

// if (www.result == UnityWebRequest.Result.ConnectionError) // unity 2021+
if (www.isNetworkError) // Unity 2019
{
Expand All @@ -134,7 +139,7 @@ private UnityWebRequest CreateWebRequest(HttpRequestMessage message)
response.Headers.Add(header.Key, header.Value);
}
}
response.Content = new ExposedStringContent(www.downloadHandler.text);
response.Content = new StringContent(www.downloadHandler.text);
return response;
}
}
Expand All @@ -147,29 +152,4 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage _, Can
throw new InvalidOperationException("UnityWebRequestMessageHandler must be unused");
}
}

internal class ExposedStringContent : StringContent
{
internal readonly String Content;
public ExposedStringContent(String data) : base(data) => Content = data;
}

internal static class JsonExtensions
{
public static JsonElement? GetPropertyOrNull(this JsonElement json, string name)
{
if (json.ValueKind != JsonValueKind.Object)
{
return null;
}

if (json.TryGetProperty(name, out var result) &&
result.ValueKind is not JsonValueKind.Undefined and not JsonValueKind.Null)
{
return result;
}

return null;
}
}
}

0 comments on commit e9f9faf

Please sign in to comment.