Skip to content

Commit dc26775

Browse files
authored
Merge pull request #29 from metrik-tech/feature/ENG-174
feature/eng 174: Look into simplifying the Metrik API to remove the .Server call
2 parents 80463b3 + d34f2aa commit dc26775

File tree

3 files changed

+79
-18
lines changed

3 files changed

+79
-18
lines changed

Src/Server.luau

+28-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ local Error = require(script.Parent.Enums.Error)
1313
local ErrorFormats = require(script.Parent.Data.ErrorFormats)
1414
local ApiPaths = require(script.Parent.Data.ApiPaths)
1515

16+
local GetScriptFromFullName = require(script.Parent.Util.GetScriptFromFullName)
17+
1618
local ActionBuilder = require(script.Parent.API.ActionBuilder)
1719

1820
local ApiService = require(script.Parent.Services.ApiService)
@@ -51,6 +53,30 @@ function MetrikSDK.Private.FromError(_: MetrikPrivateAPI, errorEnum: string, ...
5153
return string.format(ErrorFormats[errorEnum], ...)
5254
end
5355

56+
function MetrikSDK.Private.GetCallingScript(_: MetrikPrivateAPI)
57+
local level = 3
58+
59+
while true do
60+
local source = debug.info(level, "s")
61+
62+
if not source then
63+
return nil
64+
end
65+
66+
local instance = GetScriptFromFullName(source)
67+
68+
if not instance then
69+
return nil
70+
end
71+
72+
if instance:IsDescendantOf(script.Parent) or instance == script.Parent then
73+
level += 1
74+
else
75+
return source
76+
end
77+
end
78+
end
79+
5480
--[=[
5581
...
5682
@@ -61,7 +87,7 @@ end
6187
]=]
6288
--
6389
function MetrikSDK.Public.CreateBreadcrumb(self: MetrikPublicAPI, message: string)
64-
BreadcrumbService:CreateBreadcrumbFor(debug.info(2, "s"), message)
90+
BreadcrumbService:CreateBreadcrumbFor(self.Private:GetCallingScript(), message)
6591
end
6692

6793
--[=[
@@ -74,7 +100,7 @@ end
74100
]=]
75101
--
76102
function MetrikSDK.Public.SetContext(self: MetrikPublicAPI, context: { [string]: any })
77-
ContextService:CreateContextFor(debug.info(2, "s"), context)
103+
ContextService:CreateContextFor(self.Private:GetCallingScript(), context)
78104
end
79105

80106
--[=[

Src/init.luau

+50
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
local RunService = game:GetService("RunService")
22

3+
--[=[
4+
@class MetrikSDK
5+
6+
The MetrikSDK class is the main entry point for interacting with the Metrik backend.
7+
]=]
38
local MetrikSDK = {}
49

510
if not RunService:IsRunning() then
@@ -12,4 +17,49 @@ else
1217
end
1318
end
1419

20+
function MetrikSDK.InitializeAsync(self: MetrikSDK, settings: {
21+
projectId: string,
22+
authenticationSecret: Secret
23+
}?)
24+
if MetrikSDK.Server then
25+
MetrikSDK.Server:InitializeAsync(settings)
26+
else
27+
MetrikSDK.Client:InitializeAsync()
28+
end
29+
end
30+
31+
function MetrikSDK.GetFlag(self: MetrikSDK, flagName: string)
32+
if MetrikSDK.Server then
33+
MetrikSDK.Server:GetFlag(flagName)
34+
else
35+
MetrikSDK.Client:GetFlag(flagName)
36+
end
37+
end
38+
39+
function MetrikSDK.IsServerUpToDate(self: MetrikSDK)
40+
if MetrikSDK.Server then
41+
MetrikSDK.Server:IsServerUpToDate()
42+
else
43+
error(`MetrikSDK.IsServerUpToDate() is not available on the client.`)
44+
end
45+
end
46+
47+
function MetrikSDK.CreateBreadcrumb(self: MetrikSDK, message: string)
48+
if MetrikSDK.Server then
49+
MetrikSDK.Server:CreateBreadcrumb(message)
50+
else
51+
MetrikSDK.Client:CreateBreadcrumb(message)
52+
end
53+
end
54+
55+
function MetrikSDK.SetContext(self: MetrikSDK, context: { [string]: any })
56+
if MetrikSDK.Server then
57+
MetrikSDK.Server:SetContext(context)
58+
else
59+
MetrikSDK.Client:SetContext(context)
60+
end
61+
end
62+
63+
export type MetrikSDK = typeof(MetrikSDK)
64+
1565
return MetrikSDK

Tests/Development.server.luau

+1-16
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,12 @@ local RunService = game:GetService("RunService")
55

66
local MetrikSDK = require(ReplicatedStorage.MetrikSDK)
77

8-
MetrikSDK.Server:InitializeAsync({
8+
MetrikSDK:InitializeAsync({
99
projectId = ServerStorage.__Project_Id.Value,
1010
authenticationSecret = RunService:IsStudio() and ServerStorage.__Project_Auth.Value
1111
or HttpService:GetSecret("metrik_token")
1212
}):andThen(function()
1313
warn("Metrik SDK loaded!")
14-
15-
local action = MetrikSDK.Server.ActionBuilder.new()
16-
:SetName("Hello World")
17-
:SetDescription("Prints 'Hello, World' with an option for another message")
18-
:AddArgument("Message", {
19-
Required = true,
20-
Type = "string",
21-
})
22-
:Build()
23-
24-
function action:OnRun(message: string)
25-
print("Hello, World")
26-
27-
print(message)
28-
end
2914
end):catch(function(exception)
3015
warn("Metrik SDK failed: ", exception)
3116
end)

0 commit comments

Comments
 (0)