cz.xprees.event-logging 1.0.22 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Runtime/Api/EventLoggingApi.cs +21 -9
- package/Runtime/Api/EventLoggingApi.cs.meta +1 -2
- package/Runtime/Api/Model/EventLog.cs +1 -1
- package/Runtime/Api/Model/User.cs +0 -2
- package/Runtime/Extensions/EventLoggingApiExtension.cs +1 -1
- package/Runtime/Extensions/JsonUtilityExtensions.cs +24 -0
- package/Runtime/Extensions/JsonUtilityExtensions.cs.meta +3 -0
- package/Runtime/Extensions/UnityWebRequestExtensions.cs +5 -27
- package/package.json +3 -3
|
@@ -2,7 +2,6 @@ using System;
|
|
|
2
2
|
using System.Collections.Generic;
|
|
3
3
|
using System.Threading;
|
|
4
4
|
using Cysharp.Threading.Tasks;
|
|
5
|
-
using Newtonsoft.Json;
|
|
6
5
|
using UnityEngine;
|
|
7
6
|
using UnityEngine.Networking;
|
|
8
7
|
using Xprees.EventLogging.Api.Model;
|
|
@@ -12,13 +11,18 @@ namespace Xprees.EventLogging.Api
|
|
|
12
11
|
{
|
|
13
12
|
public class EventLoggingApi : IEventLoggingApi
|
|
14
13
|
{
|
|
15
|
-
public const string DefaultEndpoint = "https://cf-collector.xprees.com
|
|
14
|
+
public const string DefaultEndpoint = "https://cf-collector.xprees.com";
|
|
16
15
|
|
|
17
16
|
private readonly string _baseUrl;
|
|
18
17
|
private string LogsUri => $"{_baseUrl}/logs";
|
|
19
18
|
private string LogsBatchUri => $"{_baseUrl}/logs/batch";
|
|
20
19
|
private string ScenariosUri => $"{_baseUrl}/logs/scenarios";
|
|
21
20
|
|
|
21
|
+
/// Seconds before a request is aborted. Browsers impose no default timeout, so WebGL relies on these.
|
|
22
|
+
private const int timeoutSeconds = 15;
|
|
23
|
+
|
|
24
|
+
private const int batchTimeoutSeconds = 30;
|
|
25
|
+
|
|
22
26
|
public EventLoggingApi(string baseUrl = DefaultEndpoint)
|
|
23
27
|
{
|
|
24
28
|
if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out _))
|
|
@@ -35,7 +39,8 @@ namespace Xprees.EventLogging.Api
|
|
|
35
39
|
{
|
|
36
40
|
try
|
|
37
41
|
{
|
|
38
|
-
var request = UnityWebRequest.Get(_baseUrl);
|
|
42
|
+
using var request = UnityWebRequest.Get(_baseUrl);
|
|
43
|
+
request.timeout = timeoutSeconds;
|
|
39
44
|
await request.SendWebRequestAsync(cancellationToken);
|
|
40
45
|
return request.result == UnityWebRequest.Result.Success;
|
|
41
46
|
}
|
|
@@ -51,7 +56,8 @@ namespace Xprees.EventLogging.Api
|
|
|
51
56
|
{
|
|
52
57
|
try
|
|
53
58
|
{
|
|
54
|
-
var request = new UnityWebRequest(LogsUri, UnityWebRequest.kHttpVerbPOST);
|
|
59
|
+
using var request = new UnityWebRequest(LogsUri, UnityWebRequest.kHttpVerbPOST);
|
|
60
|
+
request.timeout = timeoutSeconds;
|
|
55
61
|
request.AddJsonBody(log);
|
|
56
62
|
await request.SendWebRequestAsync(cancellationToken);
|
|
57
63
|
return request.result == UnityWebRequest.Result.Success;
|
|
@@ -64,11 +70,15 @@ namespace Xprees.EventLogging.Api
|
|
|
64
70
|
return false;
|
|
65
71
|
}
|
|
66
72
|
|
|
67
|
-
public async UniTask<bool> SendEventLogsBatch(
|
|
73
|
+
public async UniTask<bool> SendEventLogsBatch(
|
|
74
|
+
List<EventLog> logs,
|
|
75
|
+
CancellationToken cancellationToken = default
|
|
76
|
+
)
|
|
68
77
|
{
|
|
69
78
|
try
|
|
70
79
|
{
|
|
71
|
-
var batchRequest = new UnityWebRequest(LogsBatchUri, UnityWebRequest.kHttpVerbPOST);
|
|
80
|
+
using var batchRequest = new UnityWebRequest(LogsBatchUri, UnityWebRequest.kHttpVerbPOST);
|
|
81
|
+
batchRequest.timeout = batchTimeoutSeconds;
|
|
72
82
|
batchRequest.AddJsonBody(logs);
|
|
73
83
|
await batchRequest.SendWebRequestAsync(cancellationToken);
|
|
74
84
|
return batchRequest.result == UnityWebRequest.Result.Success;
|
|
@@ -87,14 +97,16 @@ namespace Xprees.EventLogging.Api
|
|
|
87
97
|
{
|
|
88
98
|
var downloadHandlerBuffer = new DownloadHandlerBuffer();
|
|
89
99
|
var uploadHandlerRaw = new UploadHandlerRaw(Array.Empty<byte>());
|
|
90
|
-
var request = new UnityWebRequest(ScenariosUri, UnityWebRequest.kHttpVerbGET, downloadHandlerBuffer,
|
|
91
|
-
|
|
100
|
+
using var request = new UnityWebRequest(ScenariosUri, UnityWebRequest.kHttpVerbGET, downloadHandlerBuffer,
|
|
101
|
+
uploadHandlerRaw);
|
|
102
|
+
request.timeout = timeoutSeconds;
|
|
103
|
+
request.SetRequestHeader("Accept", "application/json");
|
|
92
104
|
await request.SendWebRequestAsync(cancellationToken);
|
|
93
105
|
var result = request.result;
|
|
94
106
|
if (result != UnityWebRequest.Result.Success) return Array.Empty<string>();
|
|
95
107
|
var rawString = request.downloadHandler.text;
|
|
96
108
|
|
|
97
|
-
return
|
|
109
|
+
return JsonUtilityExtensions.FromJsonArray<string>(rawString);
|
|
98
110
|
}
|
|
99
111
|
catch (Exception e)
|
|
100
112
|
{
|
|
@@ -8,7 +8,7 @@ namespace Xprees.EventLogging.Extensions
|
|
|
8
8
|
{
|
|
9
9
|
public static EventLog GenerateEventLog(this EventSO @event, User user, DateTime timestamp) => new()
|
|
10
10
|
{
|
|
11
|
-
timestamp = timestamp,
|
|
11
|
+
timestamp = timestamp.ToString("O"),
|
|
12
12
|
user = user,
|
|
13
13
|
scenario = @event.scenario?.Trim(),
|
|
14
14
|
@event = @event.eventName.Trim(),
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Collections;
|
|
3
|
+
using System.Linq;
|
|
4
|
+
using UnityEngine;
|
|
5
|
+
|
|
6
|
+
namespace Xprees.EventLogging.Extensions
|
|
7
|
+
{
|
|
8
|
+
public static class JsonUtilityExtensions
|
|
9
|
+
{
|
|
10
|
+
public static T[] FromJsonArray<T>(string rawString) =>
|
|
11
|
+
// JsonUtility can't deserialize a root-level array, so wrap it in an object first.
|
|
12
|
+
JsonUtility.FromJson<ArrayWrapper<T>>($"{{\"items\":{rawString}}}").items;
|
|
13
|
+
|
|
14
|
+
[Serializable]
|
|
15
|
+
private class ArrayWrapper<T>
|
|
16
|
+
{
|
|
17
|
+
public T[] items;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static string ToJsonArray(IEnumerable array) =>
|
|
21
|
+
// JsonUtility can't serialize a root-level collection, so build the array manually for those.
|
|
22
|
+
$"[{string.Join(",", array.Cast<object>().Select(JsonUtility.ToJson))}]";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
using System;
|
|
2
|
+
using System.Collections;
|
|
2
3
|
using System.Text;
|
|
3
4
|
using System.Threading;
|
|
4
5
|
using Cysharp.Threading.Tasks;
|
|
5
|
-
using Newtonsoft.Json;
|
|
6
6
|
using UnityEngine;
|
|
7
7
|
using UnityEngine.Networking;
|
|
8
8
|
|
|
@@ -38,37 +38,15 @@ namespace Xprees.EventLogging.Extensions
|
|
|
38
38
|
|
|
39
39
|
public static void AddJsonBody(this UnityWebRequest request, object body)
|
|
40
40
|
{
|
|
41
|
-
|
|
41
|
+
// JsonUtility can't serialize a root-level collection, so build the array manually for those.
|
|
42
|
+
var json = body is IEnumerable items and not string
|
|
43
|
+
? JsonUtilityExtensions.ToJsonArray(items)
|
|
44
|
+
: JsonUtility.ToJson(body);
|
|
42
45
|
var jsonBytes = Encoding.UTF8.GetBytes(json);
|
|
43
46
|
|
|
44
47
|
request.uploadHandler = new UploadHandlerRaw(jsonBytes);
|
|
45
48
|
request.downloadHandler = new DownloadHandlerBuffer();
|
|
46
49
|
request.SetRequestHeader("Content-Type", "application/json");
|
|
47
50
|
}
|
|
48
|
-
|
|
49
|
-
/// Tries to Request neverssl.com to check that internet connection is working
|
|
50
|
-
public async static UniTask<UnityWebRequest.Result> TestInternetConnection(CancellationToken cancellationToken = default)
|
|
51
|
-
{
|
|
52
|
-
var request = UnityWebRequest.Get("http://neverssl.com");
|
|
53
|
-
try
|
|
54
|
-
{
|
|
55
|
-
await request.SendWebRequestAsync(cancellationToken: cancellationToken);
|
|
56
|
-
}
|
|
57
|
-
catch (OperationCanceledException)
|
|
58
|
-
{
|
|
59
|
-
// ignore
|
|
60
|
-
}
|
|
61
|
-
catch (UnityWebRequestException e)
|
|
62
|
-
{
|
|
63
|
-
Debug.LogError($"Webex: {e.Message}");
|
|
64
|
-
throw;
|
|
65
|
-
}
|
|
66
|
-
catch (Exception e)
|
|
67
|
-
{
|
|
68
|
-
Debug.LogError(e);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return request.result;
|
|
72
|
-
}
|
|
73
51
|
}
|
|
74
52
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cz.xprees.event-logging",
|
|
3
3
|
"displayName": "Event Logging",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.24",
|
|
5
5
|
"unity": "2021.3",
|
|
6
6
|
"description": "This package provides a simple way to log events in your game.",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"category": "Core",
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"com.cysharp.unitask": "2.5.
|
|
11
|
-
"cz.xprees.core": "
|
|
10
|
+
"com.cysharp.unitask": "2.5.11",
|
|
11
|
+
"cz.xprees.core": "2.0.41"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"Unity",
|