1+ local ffi = require (" ffi" )
2+ local platform = string.lower (jit .os )
3+
4+ local lib_path = os.getenv (" SUPERPOSITION_LIB_PATH" )
5+ if not lib_path then
6+ error (" Environment variable SUPERPOSITION_LIB_PATH is not set" )
7+ end
8+
9+ if platform == " osx" then
10+ lib_path = lib_path .. " /libexperimentation_client.dylib"
11+ elseif platform == " linux" then
12+ lib_path = lib_path .. " /libexperimentation_client.so"
13+ elseif platform == " windows" then
14+ lib_path = lib_path .. " /libexperimentation_client.dll"
15+ else
16+ error (" Unsupported platform: " .. platform )
17+ end
18+
19+ ffi .cdef [[
20+ int expt_new_client (const char * tenant_name , int polling_frequency , const char * cac_host_name );
21+ void expt_start_polling_update (const char * tenant_name );
22+ const char * expt_get_client (const char * tenant_name );
23+ const char * expt_get_applicable_variant (const char * client_ptr , const char * context , int toss );
24+ const char * expt_get_satisfied_experiments (const char * client_ptr , const char * context , const char * filter_prefix );
25+ const char * expt_get_filtered_satisfied_experiments (const char * client_ptr , const char * context , const char * filter_prefix );
26+ const char * expt_get_running_experiments (const char * client_ptr );
27+ void expt_free_string (const char * string );
28+ const char * expt_last_error_message ();
29+ int expt_last_error_length ();
30+ void expt_free_client (const char * client_ptr );
31+ ]]
32+
33+ local rust_lib = ffi .load (lib_path )
34+
35+ local ExperimentationClient = {}
36+ ExperimentationClient .__index = ExperimentationClient
37+
38+ function ExperimentationClient :new (tenant_name , polling_frequency , cac_host_name )
39+ assert (tenant_name and # tenant_name > 0 , " tenantName cannot be null or empty" )
40+ assert (cac_host_name and # cac_host_name > 0 , " cacHostName cannot be null or empty" )
41+
42+ local self = setmetatable ({}, ExperimentationClient )
43+ self .tenant = tenant_name
44+ self .polling_frequency = polling_frequency
45+ self .cac_host_name = cac_host_name
46+ return self
47+ end
48+
49+ function ExperimentationClient :get_experimentation_last_error_message ()
50+ local error_message = rust_lib .expt_last_error_message ();
51+ if error_message == nil then
52+ return " No Error"
53+ end
54+ return ffi .string (rust_lib .expt_last_error_message ())
55+ end
56+
57+ function ExperimentationClient :create_new_experimentation_client ()
58+ local resp_code = rust_lib .expt_new_client (self .tenant , self .polling_frequency , self .cac_host_name )
59+ if resp_code == 1 then
60+ local error_message = self :get_experimentation_last_error_message ()
61+ print (" Some error occurred while creating new experimentation client:" , error_message )
62+ error (" Client Creation Error" )
63+ end
64+ return resp_code
65+ end
66+
67+ function ExperimentationClient :get_experimentation_client ()
68+ return ffi .string (rust_lib .expt_get_client (self .tenant ))
69+ end
70+
71+ function ExperimentationClient :get_running_experiments ()
72+ return ffi .string (rust_lib .expt_get_running_experiments (self :get_experimentation_client ()))
73+ end
74+
75+ function ExperimentationClient :free_string (string )
76+ rust_lib .expt_free_string (string )
77+ end
78+
79+ function ExperimentationClient :start_experimentation_polling_update ()
80+ rust_lib .expt_start_polling_update (self .tenant )
81+ end
82+
83+ function ExperimentationClient :get_experimentation_last_error_length ()
84+ return rust_lib .expt_last_error_length ()
85+ end
86+
87+ function ExperimentationClient :free_experimentation_client ()
88+ rust_lib .expt_free_client (self :get_experimentation_client ())
89+ end
90+
91+ function ExperimentationClient :get_filtered_satisfied_experiments (context , filter_prefix )
92+ local resp = rust_lib .expt_get_filtered_satisfied_experiments (self :get_experimentation_client (), context , filter_prefix )
93+ if resp == nil then
94+ return " "
95+ end
96+ return ffi .string (resp )
97+ end
98+
99+ function ExperimentationClient :get_applicable_variant (context , toss )
100+ return ffi .string (rust_lib .expt_get_applicable_variant (self :get_experimentation_client (), context , toss ))
101+ end
102+
103+ function ExperimentationClient :get_satisfied_experiments (context , filter_prefix )
104+ return ffi .string (rust_lib .expt_get_satisfied_experiments (self :get_experimentation_client (), context , filter_prefix ))
105+ end
106+
107+ return ExperimentationClient
0 commit comments