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

feat: adding php cac and experimentation client wrapper #185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions clients/php/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Set directory path that contains superposition object files in <span style="color: red" > SUPERPOSITION_LIB_PATH </span> env variable;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we mention what version of PHP this supports?

## [<u> CAC Client </u>](./cacclient/client.php)

1. This exports a class that exposes functions that internally call rust functions.
2. For Different platform it read different superposition object files.
* <span style="color: #808080" >For Mac </span> -> libcac_client.dylib
* <span style="color: #357EC7" >For Windows </span> -> libcac_client.so
* <span style="color: orange" >For Linux </span> -> libcac_client.dll
3. This run CAC CLient in two thread one is main thread another is worker thread.
4. Polling updates for config are done on different thread. ([ref](./cacclient/client.php#L63)).


## [<u> Experimentation Client </u>](./expclient/client.php)

1. This exports a class that exposes functions that internally call rust functions.
2. For Different platform it read different superposition object files.
* <span style="color: #808080" >For Mac </span> -> libexperimentation_client.dylib
* <span style="color: #357EC7" >For Windows </span> -> libexperimentation_client.so
* <span style="color: orange" >For Linux </span> -> libexperimentation_client.dll
3. This run Experimentation CLient in two thread one is main thread another is worker thread.
4. Polling updates for experiments are done on different thread. ([ref](./expclient/client.php#L58)).


## [<u> Test </u>](./server.php)

1. To test this sample project follow below steps.
* Run superposition client.
* Run <u> **php main.php** </u>.
2. By Default this sample code uses [dev](./server.php#L5) tenant.
3. By Default this sample code assumes superposition is running on [8080](./server.php#L7) port.
3. By Default this sample code polls superposition every [1 second](./server.php#L6) port.
4. This sample code creates both [CAC CLient](./server.php#L9) and [Experimentation Client](./server.php#L10) with above default values.
118 changes: 118 additions & 0 deletions clients/php/cacclient/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
class CACClient
{
private $tenant = null;
private $polling_frequency = null;
private $cac_host_name = null;
public $rustFFI;

function __construct($tenant_name, $polling_frequency, $cac_host_name)
{
$this->tenant = $tenant_name;
$this->polling_frequency = $polling_frequency;
$this->cac_host_name = $cac_host_name;
$libPath = getenv("SUPERPOSITION_LIB_PATH");
$libFile = $libPath . DIRECTORY_SEPARATOR . "lib" . "cac_client" . (PHP_OS_FAMILY === 'Windows' ? '.dll' : (PHP_OS_FAMILY === 'Darwin' ? '.dylib' : '.so'));
if (!file_exists($libFile)) {
throw new InvalidArgumentException("Library file does not exist:");
}
$this->rustFFI = FFI::cdef("
int cac_last_error_length();
char* cac_last_error_message();
void cac_free_string(char* str);
int cac_new_client(char* tenant, int update_frequency, char* hostname);
void cac_start_polling_update(char* tenant);
void cac_free_client(char* tenantPtr);
char* cac_get_client(char* tenant);
char* cac_get_last_modified(char* tenantPtr);
char* cac_get_config(char* tenantPtr, char* filter_query, char* filter_prefix);
char* cac_get_resolved_config(char* tenantPtr, char* query, char* filter_keys, char* merge_strategy);
char* cac_get_default_config(char* tenantPtr, char* filter_keys);
", "/Users/namit.goel/Desktop/repos/namit_superposition/superposition/target/debug/libcac_client.dylib");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be dynamic right? @namitgoel

}

function get_cac_last_error_length()
{
return $this->rustFFI->cac_last_error_length();
}

function get_cac_last_error_message()
{
try {
$resp = $this->rustFFI->cac_last_error_message();
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function free_cac_string($str)
{
$this->rustFFI->cac_free_string($str);
}

function create_new_cac_client()
{
$resp = $this->rustFFI->cac_new_client($this->tenant, $this->polling_frequency, $this->cac_host_name);
if ($resp == 1) {
echo $this->get_cac_last_error_message();
}
return $resp;
}

function start_cac_polling_update()
{
$this->rustFFI->cac_start_polling_update($this->tenant);
}

function get_cac_client()
{
return $this->rustFFI->cac_get_client($this->tenant);
}

function free_cac_client()
{
$this->rustFFI->cac_free_client($this->get_cac_client());
}

function get_last_cac_modified()
{
try {
$resp = $this->rustFFI->cac_get_last_modified($this->get_cac_client());
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_cac_config($filter_query, $filter_prefix)
{
try {
$resp = $this->rustFFI->cac_get_config($this->get_cac_client(), $filter_query, $filter_prefix);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_cac_resolved_config($tenantPtr, $query, $filter_keys, $merge_strategy)
{
try {
$resp = $this->rustFFI->cac_get_resolved_config($this->get_cac_client(), $query, $filter_keys, $merge_strategy);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_cac_default_config($filter_keys)
{
try {
$resp = $this->rustFFI->cac_get_default_config($this->get_cac_client(), $filter_keys);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}
}
?>
107 changes: 107 additions & 0 deletions clients/php/expclient/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
class ExperimentationClient
{
private $tenant = null;
private $polling_frequency = null;
private $cac_host_name = null;
public $rustFFI;

function __construct($tenant_name, $polling_frequency, $cac_host_name)
{
$this->tenant = $tenant_name;
$this->polling_frequency = $polling_frequency;
$this->cac_host_name = $cac_host_name;
$libPath = getenv("SUPERPOSITION_LIB_PATH");
$libFile = $libPath . DIRECTORY_SEPARATOR . "lib" . "experimentation_client" . (PHP_OS_FAMILY === 'Windows' ? '.dll' : (PHP_OS_FAMILY === 'Darwin' ? '.dylib' : '.so'));
if (!file_exists($libFile)) {
throw new InvalidArgumentException("Library file does not exist:");
}
$this->rustFFI = FFI::cdef("
int expt_last_error_length();
char* expt_last_error_message();
void expt_free_string(char* str);
int expt_new_client(char* tenant, int update_frequency, char* hostname);
void expt_start_polling_update(char* tenant);
void expt_free_client(char* exp_ptr);
char* expt_get_client(char* tenant);
char* expt_get_applicable_variant(char* exp_ptr, char* c_context, int toss);
char* expt_get_satisfied_experiments(char* exp_ptr, char* c_context, char* filter_prefix);
char* expt_get_filtered_satisfied_experiments(char* exp_ptr, char* c_context, char* filter_prefix);
char* expt_get_running_experiments(char* exp_ptr);
", "/Users/namit.goel/Desktop/repos/namit_superposition/superposition/target/debug/libexperimentation_client.dylib");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be $libPath?

}

function get_expt_last_error_length() {
return $this->rustFFI->expt_last_error_length();
}

function get_expt_last_error_message() {
try {
$resp = $this->rustFFI->expt_last_error_message();
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function free_expt_string() {
$this->rustFFI->expt_free_string();
}

function create_expt_new_client() {
$resp = $this->rustFFI->expt_new_client($this->tenant, $this->polling_frequency, $this->cac_host_name);
if ($resp == 1) {
echo $this->get_expt_last_error_message();
}
return $resp;
}

function start_expt_polling_update() {
$this->rustFFI->expt_start_polling_update($this->tenant);
}

function get_expt_client() {
return $this->rustFFI->expt_get_client($this->tenant);
}

function free_exp_client() {
$this->rustFFI->expt_free_client($this->get_expt_client());
}

function get_expt_applicable_variant($context, $toss) {
try {
$resp = $this->rustFFI->expt_get_applicable_variant($this->get_expt_client(), $context, $toss);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function expt_get_satisfied_experiments($context, $filter_prefix) {
try {
$resp = $this->rustFFI->expt_get_satisfied_experiments($this->get_expt_client(), $context, $filter_prefix);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_expt_filtered_satisfied_experiments($context, $filter_prefix) {
try {
$resp = $this->rustFFI->expt_get_filtered_satisfied_experiments($this->get_expt_client(), $context, $filter_prefix);
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}

function get_expt_running_experiments() {
try {
$resp = $this->rustFFI->expt_get_running_experiments($this->get_expt_client());
return FFI::string($resp);
} catch (\Throwable $th) {
return $th;
}
}
}
?>
22 changes: 22 additions & 0 deletions clients/php/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
require_once "./cacclient/client.php";
require_once "./expclient/client.php";

$tenant_name = "dev";
$polling_frequency = 1;
$cac_host_name = "http://localhost:8080";

$cac_client = new CACClient($tenant_name, $polling_frequency, $cac_host_name);
$exp_client = new ExperimentationClient($tenant_name, $polling_frequency, $cac_host_name);

$cac_client->create_new_cac_client();
$exp_client->create_expt_new_client();

$cac_client-> start_cac_polling_update();
sleep(5);
$resp_cac = $cac_client->get_cac_config("{}", "");
$resp_exp = $exp_client->get_expt_running_experiments();
echo $resp_cac;
echo "\n";
echo $resp_exp;
?>